-
Notifications
You must be signed in to change notification settings - Fork 4
/
oidc.go
472 lines (409 loc) · 13.8 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Package oidc provides OpenID Connect wrapper that implements message level encryption using the optional
// encrypted request object.
package oidc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-jose/go-jose/v3"
"github.com/go-jose/go-jose/v3/jwt"
"golang.org/x/oauth2"
)
// OIDCInterface defines the functions that the clients must implement.
type OIDCInterface interface {
Exchange(string, map[string]string) (*Tokens, error)
ExchangeWithNonce(string, string, map[string]string) (*Tokens, error)
AuthRequestURL(string, map[string]interface{}) (string, error)
Verify(string) (*oidc.IDToken, error)
UserInfo(oauth2.TokenSource, interface{}) error
HandleCallback(string, string, url.Values, interface{}) error
}
// Must is a convenience function to make sure that the OIDC client is
// successfully initialised. If the client initialization fails the function
// panics.
func Must(client OIDCInterface, err error) OIDCInterface {
if err != nil {
panic(err)
}
return client
}
// NewClient initializes and returns OIDClient that can be used to implement
// OIDC authorization code flow. Discovery is used to read the provider's oidc
// configuration.
func NewClient(ctx context.Context, config *Config) (*OIDCClient, error) {
config.Scopes = addOpenIdToScope(config.Scopes)
oidcProvider, err := oidc.NewProvider(ctx, config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to initialize client: %v", err.Error())
}
oidcConfig := &oidc.Config{
ClientID: config.ClientId,
}
oauth2Config := oauth2.Config{
ClientID: config.ClientId,
ClientSecret: config.ClientSecret,
Endpoint: oidcProvider.Endpoint(),
RedirectURL: config.RedirectUri,
Scopes: config.Scopes,
}
return &OIDCClient{
oauth2Config: &oauth2Config,
oidcConfig: oidcConfig,
provider: oidcProvider,
config: config,
ctx: ctx,
}, nil
}
// NewClientMLE initialises and returns OIDClientEncrypted which can be used
// to implement OIDC authorization code flow with message level encryption.
// Discovery is used to read the provider's oidc configuration.
func NewClientMLE(ctx context.Context, config *Config) (*OIDCClientEncrypted, error) {
config.Scopes = addOpenIdToScope(config.Scopes)
oidcProvider, err := oidc.NewProvider(ctx, config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to initialize client: %v", err.Error())
}
var endpoints providerEndpoints
err = oidcProvider.Claims(&endpoints)
if err != nil {
return nil, err
}
remoteKeySet, err := providerRemoteKeys(ctx, endpoints.JwksURL)
if err != nil {
return nil, err
}
oidcConfig := &oidc.Config{
ClientID: config.ClientId,
}
oauth2Config := oauth2.Config{
ClientID: config.ClientId,
ClientSecret: config.ClientSecret,
Endpoint: oidcProvider.Endpoint(),
RedirectURL: config.RedirectUri,
Scopes: config.Scopes,
}
var localKey jose.JSONWebKey
err = json.Unmarshal([]byte(config.LocalJWK), &localKey)
if err != nil {
return nil, err
}
return &OIDCClientEncrypted{
oauth2Config: &oauth2Config,
oidcConfig: oidcConfig,
provider: oidcProvider,
decryptionKey: &localKey,
remoteKeySet: remoteKeySet,
config: config,
ctx: ctx,
}, nil
}
// Tokens combines both the oauth2 token and the oidc specific idToken
type Tokens struct {
Oauth2Token *oauth2.Token
IdToken *oidc.IDToken
}
// OIDCClient wraps oauth2 and go-oidc libraries and provides
// convenience functions for implementing OIDC authorization code flow.
type OIDCClient struct {
oauth2Config *oauth2.Config
oidcConfig *oidc.Config
provider *oidc.Provider
config *Config
ctx context.Context
}
// Exchange exchanges the authorization code to a token.
func (o *OIDCClient) Exchange(code string, options map[string]string) (*Tokens, error) {
var opts []oauth2.AuthCodeOption
for key, value := range options {
opts = append(opts, oauth2.SetAuthURLParam(key, value))
}
oauth2Token, err := o.oauth2Config.Exchange(o.ctx, code, opts...)
if err != nil {
return nil, errors.New("unable to exchange code for token")
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
return nil, fmt.Errorf("no id_token field in oauth2 token")
}
idToken, err := o.Verify(rawIDToken)
if err != nil {
return nil, fmt.Errorf("unable to verify signature")
}
return &Tokens{
Oauth2Token: oauth2Token,
IdToken: idToken,
}, nil
}
// ExchangeWithNonce exchanges the authorization code to a token and verifies the nonce
func (o *OIDCClient) ExchangeWithNonce(code, nonce string, options map[string]string) (*Tokens, error) {
tokens, err := o.Exchange(code, options)
if err != nil {
return nil, err
}
if tokens.IdToken.Nonce != nonce {
return nil, fmt.Errorf("nonce is not the one specified. expected '%s', got '%s'", nonce, tokens.IdToken.Nonce)
}
return tokens, nil
}
// AuthRequestURL returns the URL for authorization request.
func (o *OIDCClient) AuthRequestURL(state string, options map[string]interface{}) (string, error) {
var opts []oauth2.AuthCodeOption
for key, value := range options {
switch valueType := value.(type) {
case []string:
list := strings.Join(value.([]string), " ")
opts = append(opts, oauth2.SetAuthURLParam(key, list))
case string:
opts = append(opts, oauth2.SetAuthURLParam(key, value.(string)))
default:
return "", errors.New(fmt.Sprintf("unknown request option type: '%T'", valueType))
}
}
return o.oauth2Config.AuthCodeURL(state, opts...), nil
}
// Verify verifies the signature of the token. Note that the possible nonce in
func (o *OIDCClient) Verify(token string) (*oidc.IDToken, error) {
verifier := o.provider.Verifier(o.oidcConfig)
return verifier.Verify(o.ctx, token)
}
// UserInfo fetches user information from provider's user info endpoint.
func (o *OIDCClient) UserInfo(token oauth2.TokenSource, user interface{}) error {
userInfo, err := o.provider.UserInfo(o.ctx, token)
if err != nil {
return fmt.Errorf("unable to fetch user info: %v", err.Error())
}
err = userInfo.Claims(&user)
if err != nil {
return fmt.Errorf("unable to marshal claims: %v", err.Error())
}
return nil
}
// HandleCallback is a convenience function which exchanges the authorization
// code to token and then uses the token to request user information from user
// info endpoint. The implementation does not use message level encryption.
func (o *OIDCClient) HandleCallback(state, nonce string, queryParams url.Values, user interface{}) error {
if len(queryParams.Get("error")) > 0 {
err := queryParams.Get("error")
errDescription := queryParams.Get("error_description")
return fmt.Errorf("authorization failed. error: %s, description: %s", err, errDescription)
}
if queryParams.Get("state") != state {
return errors.New("received state does not match the defined state")
}
code := queryParams.Get("code")
if code == "" {
return errors.New("authorization code missing")
}
options := map[string]string{
"client_id": o.oauth2Config.ClientID,
}
var (
tokens *Tokens
err error
)
if nonce != "" {
tokens, err = o.ExchangeWithNonce(code, nonce, options)
} else {
tokens, err = o.Exchange(code, options)
}
if err != nil {
return err
}
return o.UserInfo(oauth2.StaticTokenSource(tokens.Oauth2Token), &user)
}
// OIDCClientEncrypted wraps oauth2 and oidc libraries and provides
// convenience functions for implementing OIDC authentication. The difference
// between OIDCClientEncrypted and OIDCClient is that the former uses message
// level encryption when communicating with the service provider.
type OIDCClientEncrypted struct {
oauth2Config *oauth2.Config
oidcConfig *oidc.Config
provider *oidc.Provider
decryptionKey *jose.JSONWebKey
remoteKeySet *RemoteKeyStore
config *Config
ctx context.Context
}
// AuthRequestURL returns the authorization request URL with encrypted request object.
func (o *OIDCClientEncrypted) AuthRequestURL(state string, opts map[string]interface{}) (string, error) {
mandatoryParams := map[string]string{
"response_type": "code",
"client_id": o.oauth2Config.ClientID,
"redirect_uri": o.oauth2Config.RedirectURL,
"scope": strings.Join(o.oauth2Config.Scopes, " "),
"state": state,
}
// This will override any options that conflict with mandatory params
for key, value := range mandatoryParams {
opts[key] = value
}
requestPayload, err := json.Marshal(opts)
if err != nil {
return "", err
}
encryptionKey, err := o.remoteKeySet.ByUse("enc")
if err != nil {
return "", err
}
// TODO: take encryption algorithm from openid-configuration
enc := jose.ContentEncryption("A256CBC-HS512")
alg := jose.KeyAlgorithm(encryptionKey.Algorithm)
options := jose.EncrypterOptions{
Compression: "",
ExtraHeaders: nil,
}
options.WithType("JWE")
encrypter, err := newEncrypter(o.ctx, encryptionKey, enc, alg, options)
if err != nil {
return "", err
}
data, err := encrypter.Encrypt(requestPayload)
if err != nil {
return "", err
}
serializedRequestObject, err := data.CompactSerialize()
if err != nil {
return "", err
}
authRequestURL := fmt.Sprintf("%s?request=%s", o.oauth2Config.Endpoint.AuthURL, serializedRequestObject)
return authRequestURL, nil
}
// Verify verifies the signature of a token.
func (o *OIDCClientEncrypted) Verify(token string) (*oidc.IDToken, error) {
verifier := o.provider.Verifier(o.oidcConfig)
return verifier.Verify(o.ctx, token)
}
// Exchange exchanges the authorization code to a token.
func (o *OIDCClientEncrypted) Exchange(code string, options map[string]string) (*Tokens, error) {
var opts []oauth2.AuthCodeOption
for key, value := range options {
opts = append(opts, oauth2.SetAuthURLParam(key, value))
}
oauth2Token, err := o.oauth2Config.Exchange(o.ctx, code, opts...)
if err != nil {
return nil, errors.New("unable to exchange authorization code to token")
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
return nil, errors.New("no id_token field in oauth2 token")
}
parsedJWE, err := jose.ParseEncrypted(rawIDToken)
if err != nil {
return nil, errors.New("unable to parse encrypted id_token")
}
decryptedIdToken, err := parsedJWE.Decrypt(o.decryptionKey)
if err != nil {
return nil, errors.New("unable to decrypt id_token")
}
idToken, err := o.Verify(string(decryptedIdToken))
if err != nil {
return nil, errors.New("unable to verify id_token signature")
}
return &Tokens{
Oauth2Token: oauth2Token,
IdToken: idToken,
}, nil
}
// ExchangeWithNonce Exchanges the authorization code to a token and verifies nonce
func (o *OIDCClientEncrypted) ExchangeWithNonce(code, nonce string, options map[string]string) (*Tokens, error) {
tokens, err := o.Exchange(code, options)
if err != nil {
return nil, err
}
if tokens.IdToken.Nonce != nonce {
return nil, fmt.Errorf("nonce is not the one specified. expected '%s', got '%s'", nonce, tokens.IdToken.Nonce)
}
return tokens, nil
}
// Represents the providers endpoints
type providerEndpoints struct {
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JwksURL string `json:"jwks_uri"`
UserInfoURL string `json:"userinfo_endpoint"`
}
// UserInfo fetches user information from provider's user info endpoint.
func (o *OIDCClientEncrypted) UserInfo(tokenSource oauth2.TokenSource, destination interface{}) error {
var endpoints providerEndpoints
err := o.provider.Claims(&endpoints)
if err != nil {
return err
}
req, err := http.NewRequest("GET", endpoints.UserInfoURL, nil)
if err != nil {
return fmt.Errorf("unbale to create userinfo request: %v", err)
}
token, err := tokenSource.Token()
if err != nil {
return fmt.Errorf("unable to get token: %v", err)
}
token.SetAuthHeader(req)
response, err := doRequest(o.ctx, req)
if err != nil {
return fmt.Errorf("unable to execute request: %v", err)
}
body, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("unable to read response body: %v", err)
}
defer response.Body.Close()
if !statusCodeIs2xx(response.StatusCode) {
return fmt.Errorf("unable to fetch user info, received status: %s", response.Status)
}
webToken, err := jwt.ParseSignedAndEncrypted(string(body))
if err != nil {
return fmt.Errorf("unable to parse encrypted response: %v", err)
}
decryptedData, err := webToken.Decrypt(o.decryptionKey)
if err != nil {
return fmt.Errorf("unable to decrypt payload: %v", err)
}
verificationKey, err := o.remoteKeySet.ById(decryptedData.Headers[0].KeyID)
if err != nil {
return fmt.Errorf("unable to find key with keyId '%s'", decryptedData.Headers[0].KeyID)
}
err = decryptedData.Claims(verificationKey, destination)
if err != nil {
return err
}
return nil
}
// HandleCallback is a convenience function which exchanges the authorization
// code to token and then uses the token to request user information from
// user info endpoint. The implementation uses message level encryption.
func (o *OIDCClientEncrypted) HandleCallback(state, nonce string, queryParams url.Values, user interface{}) error {
if len(queryParams.Get("error")) > 0 {
err := queryParams.Get("error")
errDescription := queryParams.Get("error_description")
return fmt.Errorf("authorization failed. error: %s, description: %s", err, errDescription)
}
if queryParams.Get("state") != state {
return errors.New("received state does not match the defined state")
}
code := queryParams.Get("code")
if len(code) == 0 {
return errors.New("authorization code missing")
}
options := map[string]string{
"client_id": o.oauth2Config.ClientID,
}
var (
tokens *Tokens
err error
)
if nonce != "" {
tokens, err = o.ExchangeWithNonce(code, nonce, options)
} else {
tokens, err = o.Exchange(code, options)
}
if err != nil {
return err
}
return o.UserInfo(oauth2.StaticTokenSource(tokens.Oauth2Token), &user)
}