forked from vmware-archive/atc
/
provider.go
186 lines (154 loc) · 5.29 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
185
186
package genericoauth
import (
"errors"
"fmt"
"net/http"
"code.cloudfoundry.org/lager"
"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"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
const ProviderName = "oauth"
type Provider struct {
verifier.Verifier
Config ConfigOverride
}
type ConfigOverride struct {
oauth2.Config
AuthURLParams map[string]string
}
type NoopVerifier struct{}
func init() {
provider.Register(ProviderName, GenericTeamProvider{})
}
type GenericOAuthConfig struct {
DisplayName string `json:"display_name" long:"display-name" description:"Name for this auth method on the web UI."`
ClientID string `json:"client_id" long:"client-id" description:"Application client ID for enabling generic OAuth."`
ClientSecret string `json:"client_secret" long:"client-secret" description:"Application client secret for enabling generic OAuth."`
AuthURL string `json:"auth_url,omitempty" long:"auth-url" description:"Generic OAuth provider AuthURL endpoint."`
AuthURLParams map[string]string `json:"auth_url_params,omitempty" long:"auth-url-param" description:"Parameter to pass to the authentication server AuthURL. Can be specified multiple times."`
Scope string `json:"scope,omitempty" long:"scope" description:"Optional scope required to authorize user"`
TokenURL string `json:"token_url,omitempty" long:"token-url" description:"Generic OAuth provider TokenURL endpoint."`
}
func (config *GenericOAuthConfig) 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: config.DisplayName,
AuthURL: oauthBaseURL + path,
}
}
func (config *GenericOAuthConfig) IsConfigured() bool {
return config.AuthURL != "" ||
config.TokenURL != "" ||
config.ClientID != "" ||
config.ClientSecret != "" ||
config.DisplayName != ""
}
func (config *GenericOAuthConfig) Validate() error {
var errs *multierror.Error
if config.ClientID == "" || config.ClientSecret == "" {
errs = multierror.Append(
errs,
errors.New("must specify --generic-oauth-client-id and --generic-oauth-client-secret to use Generic OAuth."),
)
}
if config.AuthURL == "" || config.TokenURL == "" {
errs = multierror.Append(
errs,
errors.New("must specify --generic-oauth-auth-url and --generic-oauth-token-url to use Generic OAuth."),
)
}
if config.DisplayName == "" {
errs = multierror.Append(
errs,
errors.New("must specify --generic-oauth-display-name to use Generic OAuth."),
)
}
return errs.ErrorOrNil()
}
type GenericTeamProvider struct{}
func (GenericTeamProvider) AddAuthGroup(group *flags.Group) provider.AuthConfig {
flags := &GenericOAuthConfig{}
goGroup, err := group.AddGroup("Generic OAuth Authentication (allows access to ALL authenticated users)", "", flags)
if err != nil {
panic(err)
}
goGroup.Namespace = "generic-oauth"
return flags
}
func (GenericTeamProvider) UnmarshalConfig(config *json.RawMessage) (provider.AuthConfig, error) {
flags := &GenericOAuthConfig{}
if config != nil {
err := json.Unmarshal(*config, &flags)
if err != nil {
return nil, err
}
}
return flags, nil
}
func (GenericTeamProvider) ProviderConstructor(
config provider.AuthConfig,
redirectURL string,
) (provider.Provider, bool) {
genericOAuth := config.(*GenericOAuthConfig)
endpoint := oauth2.Endpoint{}
if genericOAuth.AuthURL != "" && genericOAuth.TokenURL != "" {
endpoint.AuthURL = genericOAuth.AuthURL
endpoint.TokenURL = genericOAuth.TokenURL
}
var oauthVerifier verifier.Verifier
if genericOAuth.Scope != "" {
oauthVerifier = NewScopeVerifier(genericOAuth.Scope)
} else {
oauthVerifier = NoopVerifier{}
}
return Provider{
Verifier: oauthVerifier,
Config: ConfigOverride{
Config: oauth2.Config{
ClientID: genericOAuth.ClientID,
ClientSecret: genericOAuth.ClientSecret,
Endpoint: endpoint,
RedirectURL: redirectURL,
},
AuthURLParams: genericOAuth.AuthURLParams,
},
}, true
}
func (v NoopVerifier) Verify(logger lager.Logger, client *http.Client) (bool, error) {
return true, nil
}
func (provider Provider) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string {
for key, value := range provider.Config.AuthURLParams {
opts = append(opts, oauth2.SetAuthURLParam(key, value))
}
return provider.Config.AuthCodeURL(state, opts...)
}
func (provider Provider) Exchange(ctx context.Context, code string) (*oauth2.Token, error) {
return provider.Config.Exchange(ctx, code)
}
func (provider Provider) Client(ctx context.Context, t *oauth2.Token) *http.Client {
return provider.Config.Client(ctx, t)
}
func (Provider) PreTokenClient() (*http.Client, error) {
return &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}, nil
}