forked from vmware-archive/atc
/
provider.go
184 lines (152 loc) · 5.19 KB
/
provider.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package github
import (
"errors"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"fmt"
"strings"
"encoding/json"
"github.com/concourse/atc"
"github.com/concourse/atc/auth/provider"
"github.com/concourse/atc/auth/routes"
"github.com/concourse/atc/auth/verifier"
"github.com/hashicorp/go-multierror"
flags "github.com/jessevdk/go-flags"
"github.com/tedsuo/rata"
)
const ProviderName = "github"
const DisplayName = "GitHub"
var Scopes = []string{"read:org"}
type GitHubAuthConfig struct {
ClientID string `json:"client_id" long:"client-id" description:"Application client ID for enabling GitHub OAuth."`
ClientSecret string `json:"client_secret" long:"client-secret" description:"Application client secret for enabling GitHub OAuth."`
Organizations []string `json:"organizations,omitempty" long:"organization" description:"GitHub organization whose members will have access." value-name:"ORG"`
Teams []GitHubTeamConfig `json:"teams,omitempty" long:"team" description:"GitHub team whose members will have access." value-name:"ORG/TEAM"`
Users []string `json:"users,omitempty" long:"user" description:"GitHub user to permit access." value-name:"LOGIN"`
AuthURL string `json:"auth_url,omitempty" long:"auth-url" description:"Override default endpoint AuthURL for Github Enterprise."`
TokenURL string `json:"token_url,omitempty" long:"token-url" description:"Override default endpoint TokenURL for Github Enterprise."`
APIURL string `json:"api_url,omitempty" long:"api-url" description:"Override default API endpoint URL for Github Enterprise."`
}
func (*GitHubAuthConfig) AuthMethod(oauthBaseURL string, teamName string) atc.AuthMethod {
path, err := routes.OAuthRoutes.CreatePathForRoute(
routes.OAuthBegin,
rata.Params{"provider": ProviderName},
)
if err != nil {
panic("failed to construct oauth begin handler route: " + err.Error())
}
path = path + fmt.Sprintf("?team_name=%s", teamName)
return atc.AuthMethod{
Type: atc.AuthTypeOAuth,
DisplayName: DisplayName,
AuthURL: oauthBaseURL + path,
}
}
func (auth *GitHubAuthConfig) IsConfigured() bool {
return auth.ClientID != "" ||
auth.ClientSecret != "" ||
len(auth.Organizations) > 0 ||
len(auth.Teams) > 0 ||
len(auth.Users) > 0
}
func (auth *GitHubAuthConfig) Validate() error {
var errs *multierror.Error
if auth.ClientID == "" || auth.ClientSecret == "" {
errs = multierror.Append(
errs,
errors.New("must specify --github-auth-client-id and --github-auth-client-secret to use GitHub OAuth."),
)
}
if len(auth.Organizations) == 0 && len(auth.Teams) == 0 && len(auth.Users) == 0 {
errs = multierror.Append(
errs,
errors.New("at least one of the following is required for github-auth: organizations, teams, users."),
)
}
return errs.ErrorOrNil()
}
type GitHubTeamConfig struct {
OrganizationName string `json:"organization_name,omitempty"`
TeamName string `json:"team_name,omitempty"`
}
func (flag *GitHubTeamConfig) UnmarshalFlag(value string) error {
s := strings.SplitN(value, "/", 2)
if len(s) != 2 {
return fmt.Errorf("malformed GitHub team specification: '%s'", value)
}
flag.OrganizationName = s[0]
flag.TeamName = s[1]
return nil
}
type GitHubProvider struct {
*oauth2.Config
verifier.Verifier
}
func init() {
provider.Register(ProviderName, GitHubTeamProvider{})
}
type GitHubTeamProvider struct {
}
func (GitHubTeamProvider) AddAuthGroup(group *flags.Group) provider.AuthConfig {
flags := &GitHubAuthConfig{}
ghGroup, err := group.AddGroup("Github Authentication", "", flags)
if err != nil {
panic(err)
}
ghGroup.Namespace = "github-auth"
return flags
}
func (GitHubTeamProvider) UnmarshalConfig(config *json.RawMessage) (provider.AuthConfig, error) {
flags := &GitHubAuthConfig{}
if config != nil {
err := json.Unmarshal(*config, &flags)
if err != nil {
return nil, err
}
}
return flags, nil
}
func (GitHubTeamProvider) ProviderConstructor(
config provider.AuthConfig,
redirectURL string,
) (provider.Provider, bool) {
githubAuth := config.(*GitHubAuthConfig)
client := NewClient(githubAuth.APIURL)
endpoint := github.Endpoint
if githubAuth.AuthURL != "" && githubAuth.TokenURL != "" {
endpoint.AuthURL = githubAuth.AuthURL
endpoint.TokenURL = githubAuth.TokenURL
}
return GitHubProvider{
Verifier: verifier.NewVerifierBasket(
NewTeamVerifier(teamConfigsToTeam(githubAuth.Teams), client),
NewOrganizationVerifier(githubAuth.Organizations, client),
NewUserVerifier(githubAuth.Users, client),
),
Config: &oauth2.Config{
ClientID: githubAuth.ClientID,
ClientSecret: githubAuth.ClientSecret,
Endpoint: endpoint,
Scopes: Scopes,
RedirectURL: redirectURL,
},
}, true
}
func (GitHubProvider) PreTokenClient() (*http.Client, error) {
return &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}, nil
}
func teamConfigsToTeam(dbteams []GitHubTeamConfig) []Team {
teams := []Team{}
for _, team := range dbteams {
teams = append(teams, Team{
Name: team.TeamName,
Organization: team.OrganizationName,
})
}
return teams
}