This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support for group restriction for Gitlab provider #312
Open
JacobAae
wants to merge
5
commits into
bitly:master
Choose a base branch
from
JacobAae:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19eaf7d
Allow gitlab-group as input parameter
d429722
Respect gitlab-group in provider
4f637a0
Add documentation for gitlab-group configuration
6b5a334
Minor updates based on review comments and gofmt
27643bc
providers: gitlab: support pagination for groups
JacobAae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,15 @@ import ( | |
"net/http" | ||
"net/url" | ||
|
||
"encoding/json" | ||
"fmt" | ||
"github.com/bitly/oauth2_proxy/api" | ||
"io/ioutil" | ||
) | ||
|
||
type GitLabProvider struct { | ||
*ProviderData | ||
Group string | ||
} | ||
|
||
func NewGitLabProvider(p *ProviderData) *GitLabProvider { | ||
|
@@ -41,8 +45,56 @@ func NewGitLabProvider(p *ProviderData) *GitLabProvider { | |
return &GitLabProvider{ProviderData: p} | ||
} | ||
|
||
func (p *GitLabProvider) SetGroup(group string) { | ||
p.Group = group | ||
} | ||
|
||
func (p *GitLabProvider) hasGroup(accessToken string) (bool, error) { | ||
|
||
var groups []struct { | ||
Group string `json:"name"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a serious security issue since the Consider this scenario: [
{
"id": 1,
"web_url": "https://gitlab.com/groups/admins",
"name": "admins",
"path": "admins",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "admins",
"full_path": "admins",
"parent_id": null
},
{
"id": 2,
"web_url": "https://gitlab.com/groups/myfunnyproject",
"name": "myfunnyproject",
"path": "myfunnyproject",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "myfunnyproject",
"full_path": "myfunnyproject",
"parent_id": null
},
{
"id": 3,
"web_url": "https://gitlab.com/groups/myfunnyproject/admins",
"name": "admins",
"path": "admins",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "myfunnyproject / myfunnyproject",
"full_path": "myfunnyproject/myfunnyproject",
"parent_id": 2
}
] There are three groups: I suggest using |
||
} | ||
|
||
endpoint := p.ValidateURL.Scheme + "://" + p.ValidateURL.Host + "/api/v3/groups?access_token=" + accessToken | ||
req, _ := http.NewRequest("GET", endpoint, nil) | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if err != nil { | ||
return false, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return false, fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body) | ||
} | ||
|
||
if err := json.Unmarshal(body, &groups); err != nil { | ||
return false, err | ||
} | ||
|
||
for _, group := range groups { | ||
if p.Group == group.Group { | ||
// Found the group | ||
return true, nil | ||
} | ||
} | ||
|
||
log.Printf("Group %s not found in %s", p.Group, groups) | ||
return false, nil | ||
} | ||
|
||
func (p *GitLabProvider) GetEmailAddress(s *SessionState) (string, error) { | ||
|
||
// if we require a group, check that first | ||
if p.Group != "" { | ||
if ok, err := p.hasGroup(s.AccessToken); err != nil || !ok { | ||
return "", err | ||
} | ||
} | ||
|
||
req, err := http.NewRequest("GET", | ||
p.ValidateURL.String()+"?access_token="+s.AccessToken, nil) | ||
if err != nil { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if multiple groups could be specified, just like multiple
GoogleGroups
can be specified.