-
Notifications
You must be signed in to change notification settings - Fork 157
/
oidc.go
420 lines (365 loc) · 12.4 KB
/
oidc.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package authentication
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/mux"
"go.uber.org/zap"
"golang.org/x/oauth2"
)
type OpenIDConnectFlavor int
const (
OpenIDConnectFlavorDefault OpenIDConnectFlavor = iota
OpenIDConnectFlavorAuth0
)
func createOIDCProvider(ctx context.Context, config OpenIDConnectConfig) (*oidc.Provider, error) {
provider, err := oidc.NewProvider(ctx, config.Issuer)
if err != nil {
originalErr := err
if strings.HasSuffix(config.Issuer, "/") {
config.Issuer = strings.TrimSuffix(config.Issuer, "/")
} else {
config.Issuer += "/"
}
provider, err = oidc.NewProvider(ctx, config.Issuer)
if err != nil {
return nil, originalErr
}
}
return provider, nil
}
func validateOIDCIssuer(issuer string) error {
_, err := url.ParseRequestURI(issuer)
return err
}
type OpenIDConnectCookieHandler struct {
claims ClaimsInfo
provider *oidc.Provider
auth2 *OAuth2AuthenticationHandler
}
func NewOpenIDConnectCookieHandler(config OpenIDConnectConfig, hooks Hooks, log *zap.Logger) (*OpenIDConnectCookieHandler, error) {
if err := validateOIDCIssuer(config.Issuer); err != nil {
return nil, err
}
ctx := context.Background()
provider, err := createOIDCProvider(ctx, config)
if err != nil {
return nil, fmt.Errorf("creating OIDC provider: %w", err)
}
handler := &OpenIDConnectCookieHandler{
provider: provider,
}
if err := provider.Claims(&handler.claims); err != nil {
return nil, fmt.Errorf("retrieving OIDC provider claims: %w", err)
}
handler.auth2 = NewOAuth2AuthenticationHandler(OAuth2AuthenticationConfig{
Provider: config.Provider,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
QueryParameters: config.QueryParameters,
Hooks: hooks,
Log: log,
}, handler)
return handler, nil
}
type QueryParameter struct {
Name string
Value string
}
type OpenIDConnectConfig struct {
Provider ProviderConfig
Issuer string
ClientID string
ClientSecret string
QueryParameters []QueryParameter
}
type ClaimsInfo struct {
ScopesSupported []string `json:"scopes_supported"`
ClaimsSupported []string `json:"claims_supported"`
}
// Claims decodes JWT claims. See https://www.iana.org/assignments/jwt/jwt.xhtml.
type Claims struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
MiddleName string `json:"middle_name"`
NickName string `json:"nickname"`
PreferredUsername string `json:"preferred_username"`
Profile string `json:"profile"`
Picture string `json:"picture"`
Website string `json:"website"`
Email string `json:"email"`
EmailVerified bool `json:"-"`
EmailVerifiedRaw interface{} `json:"email_verified"`
Gender string `json:"gender"`
BirthDate string `json:"birthdate"`
ZoneInfo string `json:"zoneinfo"`
Locale string `json:"locale"`
Location string `json:"location"`
Raw map[string]interface{} `json:"-"`
}
func (c *Claims) ToUser() *User {
return &User{
UserID: c.Subject,
Name: c.Name,
FirstName: c.GivenName,
LastName: c.FamilyName,
MiddleName: c.MiddleName,
NickName: c.NickName,
PreferredUsername: c.PreferredUsername,
Profile: c.Profile,
Picture: c.Picture,
Website: c.Website,
Email: c.Email,
EmailVerified: c.EmailVerified,
Gender: c.Gender,
BirthDate: c.BirthDate,
ZoneInfo: c.ZoneInfo,
Locale: c.Locale,
Location: c.Location,
CustomClaims: c.Custom(),
}
}
// isCustomClaim true if claim represents a token claim that we're not explicitly
// handling in type Claim
func isCustomClaim(claim string) bool {
// XXX: Keep this list in sync with Claim's fields
switch claim {
case "iss", "sub", "name", "given_name", "family_name", "middle_name", "nickname", "preferred_username", "profile", "picture", "website", "email", "email_verified", "gender", "birthdate", "zoneinfo", "location", "locale":
return false
}
return true
}
// Custom returns a non-nil map with claims from c.Raw that we do not
// parse explicitly
func (c *Claims) Custom() map[string]interface{} {
custom := make(map[string]interface{})
for k, v := range c.Raw {
if isCustomClaim(k) {
custom[k] = v
}
}
return custom
}
func (h *OpenIDConnectCookieHandler) Register(authorizeRouter, callbackRouter *mux.Router) {
authorizeRouter.Path(fmt.Sprintf("/%s", h.auth2.config.Provider.ID)).Methods(http.MethodGet).HandlerFunc(h.auth2.Authorize)
callbackRouter.Path(fmt.Sprintf("/%s", h.auth2.config.Provider.ID)).Methods(http.MethodGet).HandlerFunc(h.auth2.Callback)
}
func (h *OpenIDConnectCookieHandler) User(ctx context.Context, log *zap.Logger, token *oauth2.Token) (*User, error) {
var idToken string
accessToken := token.AccessToken
if maybeIdToken := token.Extra("id_token"); maybeIdToken != nil {
idToken = maybeIdToken.(string)
}
userInfo, err := h.provider.UserInfo(ctx, oauth2.StaticTokenSource(token))
if err != nil {
log.Error("oidc.provider.UserInfo", zap.Error(err))
return nil, err
}
var claims Claims
if err := userInfo.Claims(&claims); err != nil {
log.Error("oidc.userInfo.Claims", zap.Error(err))
return nil, err
}
if err := userInfo.Claims(&claims.Raw); err != nil {
log.Error("oidc.userInfo.Claims.Raw", zap.Error(err))
return nil, err
}
accessTokenJSON := tryParseJWT(accessToken)
idTokenJSON := tryParseJWT(idToken)
user := claims.ToUser()
user.ProviderName = "oidc"
user.ExpiresAt = token.Expiry
user.AccessToken = accessTokenJSON
user.RawAccessToken = accessToken
user.RawIDToken = idToken
user.IdToken = idTokenJSON
user.RefreshToken = token.RefreshToken
return user, nil
}
type openIDConnectConfiguration struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
UserinfoEndpoint string `json:"userinfo_endpoint"`
JwksUri string `json:"jwks_uri"`
EndSessionEndpoint string `json:"end_session_endpoint"`
}
type openIDConnectConfigurationMissingFieldError string
func (e openIDConnectConfigurationMissingFieldError) Error() string {
return fmt.Sprintf("missing field %q", string(e))
}
func (c *openIDConnectConfiguration) Validate() error {
// See https://openid.net/specs/openid-connect-discovery-1_0.html
//
// Note that our implementation uses userinfo_endpoint and hence we
// make it required, but can work without jwks_uri so we make it optional
if c.Issuer == "" {
return openIDConnectConfigurationMissingFieldError("issuer")
}
if c.AuthorizationEndpoint == "" {
return openIDConnectConfigurationMissingFieldError("authorization_endpoint")
}
if c.TokenEndpoint == "" {
// TODO: This might be optional with Implicit Flow?
return openIDConnectConfigurationMissingFieldError("token_endpoint")
}
if c.UserinfoEndpoint == "" {
return openIDConnectConfigurationMissingFieldError("userinfo_endpoint")
}
return nil
}
type OpenIDDisconnectResult struct {
// Redirect indicates an URL that must be visited by the client to complete the logout
Redirect string `json:"redirect,omitempty"`
}
func (r *OpenIDDisconnectResult) RequiresClientCooperation() bool {
return r != nil && r.Redirect != ""
}
type OpenIDConnectProviderOptions struct {
Flavor OpenIDConnectFlavor
HTTPClient *http.Client
Logger *zap.Logger
}
func (o *OpenIDConnectProviderOptions) flavor() OpenIDConnectFlavor {
if o != nil {
return o.Flavor
}
return OpenIDConnectFlavorDefault
}
func (o *OpenIDConnectProviderOptions) httpClient() *http.Client {
if o != nil && o.HTTPClient != nil {
return o.HTTPClient
}
return http.DefaultClient
}
func (o *OpenIDConnectProviderOptions) logger() *zap.Logger {
if o != nil {
return o.Logger
}
return nil
}
type OpenIDConnectProvider struct {
clientID string
clientSecret string
opts *OpenIDConnectProviderOptions
config *openIDConnectConfiguration
}
func NewOpenIDConnectProvider(issuer string, clientID string, clientSecret string, opts *OpenIDConnectProviderOptions) (*OpenIDConnectProvider, error) {
if issuer == "" {
return nil, errors.New("OIDC issuer must not be empty")
}
if clientID == "" {
return nil, errors.New("OIDC client ID must not be empty")
}
if clientSecret == "" {
return nil, errors.New("OIDC client secret must not be empty")
}
_, err := url.ParseRequestURI(issuer)
if err != nil {
return nil, fmt.Errorf("OIDC issuer must be a valid URL: %w", err)
}
issuer = strings.TrimSuffix(issuer, "/")
introspectionURL := issuer + "/.well-known/openid-configuration"
req, err := http.NewRequest(http.MethodGet, introspectionURL, nil)
if err != nil {
return nil, fmt.Errorf("could not request OIDC configuration: %w", err)
}
resp, err := opts.httpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("could not fetch OIDC configuration: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("OIDC configuration returned an HTTP error code: %d", resp.StatusCode)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var config openIDConnectConfiguration
if err := dec.Decode(&config); err != nil {
return nil, fmt.Errorf("could not decode OIDC configuration: %w", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid OIDC configuration: %w", err)
}
// Auth0 doesn't provide EndSessionEndpoint, but we handle it separately
if config.EndSessionEndpoint == "" && opts.flavor() != OpenIDConnectFlavorAuth0 {
if logger := opts.logger(); logger != nil {
logger.Debug("issuer doesn't support end_session_endpoint", zap.String("issuer", issuer))
}
}
return &OpenIDConnectProvider{
clientID: clientID,
clientSecret: clientSecret,
config: &config,
opts: opts,
}, nil
}
func (p *OpenIDConnectProvider) Disconnect(ctx context.Context, user *User) (*OpenIDDisconnectResult, error) {
switch p.opts.flavor() {
case OpenIDConnectFlavorDefault:
return p.disconnectDefault(ctx, user)
case OpenIDConnectFlavorAuth0:
return p.disconnectAuth0(ctx, user)
}
panic("unreachable - unhandled OpenIDConnectFlavor")
}
func (p *OpenIDConnectProvider) disconnectDefault(ctx context.Context, user *User) (*OpenIDDisconnectResult, error) {
logoutURL := p.config.EndSessionEndpoint
if logoutURL == "" {
return nil, errors.New("no end_session_endpoint")
}
req, err := http.NewRequestWithContext(ctx, "GET", logoutURL, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Set("id_token_hint", user.RawIDToken)
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := p.opts.httpClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return nil, nil
}
func (p *OpenIDConnectProvider) disconnectAuth0(ctx context.Context, _ *User) (*OpenIDDisconnectResult, error) {
params, ok := ctx.Value(ForwardedQueryParamsKey).(url.Values)
if params != nil && ok {
return &OpenIDDisconnectResult{
Redirect: fmt.Sprintf("%sv2/logout?client_id=%s&%s", p.config.Issuer, p.clientID, params.Encode()),
}, nil
}
return &OpenIDDisconnectResult{
Redirect: fmt.Sprintf("%sv2/logout?client_id=%s", p.config.Issuer, p.clientID),
}, nil
}
type OpenIDConnectProviderSet struct {
providers map[string]*OpenIDConnectProvider
}
func (s *OpenIDConnectProviderSet) Add(id string, p *OpenIDConnectProvider) error {
if s.providers == nil {
s.providers = make(map[string]*OpenIDConnectProvider)
}
if s.providers[id] != nil {
return fmt.Errorf("duplicate OIDC provider ID %q", id)
}
s.providers[id] = p
return nil
}
func (s *OpenIDConnectProviderSet) ByID(id string) (*OpenIDConnectProvider, error) {
provider := s.providers[id]
if provider == nil {
return nil, fmt.Errorf("no authentication provider with ID %q", id)
}
return provider, nil
}