-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-org-authorizer.go
51 lines (45 loc) · 1.07 KB
/
github-org-authorizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package go_simple_oauth2_middleware
import (
"github.com/markbates/goth"
)
type GithubOrgAuthorizer struct {
orgEntries []*githubOrgEntry
}
type githubOrgEntry struct {
name string
authorities []string
}
func NewGithubOrgAuthorizer() *GithubOrgAuthorizer {
return &GithubOrgAuthorizer{
orgEntries: make([]*githubOrgEntry, 0),
}
}
func (authorizer *GithubOrgAuthorizer) AddOrg(name string, authorities []string) {
authorizer.orgEntries = append(authorizer.orgEntries, &githubOrgEntry{
name: name,
authorities: authorities,
})
}
func (authorizer *GithubOrgAuthorizer) Authorize(user *goth.User) (*Authorization, error) {
if user.Provider != githubProviderName {
return nil, nil
}
userOrgs, err := GetGithubOrgs(user)
if err != nil {
return nil, err
}
if userOrgs == nil {
return nil, nil
}
for _, userOrg := range userOrgs {
for _, orgEntry := range authorizer.orgEntries {
if orgEntry.name == userOrg.Login {
return &Authorization{
User: user,
Authorities: orgEntry.authorities,
}, nil
}
}
}
return nil, nil
}