-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoidc.go
354 lines (305 loc) · 9.89 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
package oidc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
// OIDCInterface is a common interface for OIDC clients to implement.
type OIDCInterface interface {
Exchange(string, map[string]string) (*oauth2.Token, error)
AuthRequestURL(string, map[string]string) (string, error)
Verify(string) (*oidc.IDToken, error)
UserInfo(oauth2.TokenSource, interface{}) error
HandleCallback(string, interface{}) error
}
// Must is a convenience function to make sure that the OIDC client is successfully initialised or it panics.
func Must(client OIDCInterface, err error) OIDCInterface {
if err != nil {
panic(err)
}
return client
}
// NewClient initialises OIDClient and returns a pointer to the instance.
func NewClient(ctx context.Context, config *Config) (*OIDCClient, error) {
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 OIDClientEncrypted and returns a pointer to the instance.
func NewClientMLE(ctx context.Context, config *Config) (*OIDCClientEncrypted, error) {
oidcProvider, err := oidc.NewProvider(ctx, config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to initialize client: %v", err.Error())
}
type jwksUriJSON struct {
Uri string `json:"jwks_uri"`
}
var jwksUri jwksUriJSON
err = oidcProvider.Claims(&jwksUri)
if err != nil {
return nil, err
}
remoteKeySet, err := providerRemoteKeys(ctx, jwksUri.Uri)
if err != nil {
return nil, err
}
parsedRemotePubEncKey, err := remoteKeySet.ByUse("enc")
if err != nil {
return nil, errors.New("unable to find provider's public key for encryption")
}
parsedRemotePubSignKey, err := remoteKeySet.ByUse("sig")
if err != nil {
return nil, errors.New("unable to find provider's public key for signing")
}
encrypter, err := newEncrypter(parsedRemotePubEncKey)
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.MleKey), &localKey)
if err != nil {
return nil, err
}
return &OIDCClientEncrypted{
oauth2Config: &oauth2Config,
oidcConfig: oidcConfig,
provider: oidcProvider,
encrypter: encrypter,
decryptionKey: &localKey,
verificationKey: parsedRemotePubSignKey,
config: config,
ctx: ctx,
}, nil
}
// OIDCClient wraps oaut2 and oidc libraries and provides convenience functions for implementing OIDC authentication.
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) (*oauth2.Token, 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")
}
_, err = o.Verify(rawIDToken)
if err != nil {
return nil, fmt.Errorf("unable to verify signature")
}
return oauth2Token, nil
}
// AuthRequestURL returns the URL for authorization request.
func (o *OIDCClient) AuthRequestURL(state string, options map[string]string) (string, error) {
var opts []oauth2.AuthCodeOption
for key, value := range options {
opts = append(opts, oauth2.SetAuthURLParam(key, value))
}
return o.oauth2Config.AuthCodeURL(state, opts...), nil
}
// Verify verifies the signature of the token.
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.
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(code string, user interface{}) error {
options := map[string]string{
"client_id": o.oauth2Config.ClientID,
}
oauth2Token, err := o.Exchange(code, options)
if err != nil {
return err
}
return o.UserInfo(oauth2.StaticTokenSource(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
encrypter jose.Encrypter
decryptionKey *jose.JSONWebKey
verificationKey *jose.JSONWebKey
config *Config
ctx context.Context
}
// AuthCodeURLEncrypted returns the URL of the authorization request with encrypted request object.
func (o *OIDCClientEncrypted) AuthRequestURL(state string, opts map[string]string) (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
}
requestJSON, err := json.Marshal(opts)
if err != nil {
return "", err
}
data, err := o.encrypter.Encrypt(requestJSON)
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) (*oauth2.Token, 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, fmt.Errorf("unable to fetch oauth2 token: %v", err.Error())
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
return nil, errors.New("no id_token field in oauth2 token")
}
obj, err := jose.ParseEncrypted(rawIDToken)
if err != nil {
return nil, errors.New("unable to parse encrypted id_token")
}
decryptedIdToken, err := obj.Decrypt(o.decryptionKey)
if err != nil {
return nil, errors.New("unable to decrypt id_token")
}
_, err = o.Verify(string(decryptedIdToken))
if err != nil {
return nil, fmt.Errorf("unable to verify id_token: %v", err.Error())
}
return oauth2Token, nil
}
type userInfoURL struct {
Endpoint string `json:"userinfo_endpoint"`
}
// UserInfo fetches user information from client provider.
func (o *OIDCClientEncrypted) UserInfo(tokenSource oauth2.TokenSource, dest interface{}) error {
var userInfoURL userInfoURL
err := o.provider.Claims(&userInfoURL)
if err != nil {
return err
}
req, err := http.NewRequest("GET", userInfoURL.Endpoint, nil)
if err != nil {
return err
}
token, err := tokenSource.Token()
if err != nil {
return err
}
token.SetAuthHeader(req)
response, err := doRequest(o.ctx, req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
defer response.Body.Close()
if !statusCodeIs2xx(response.StatusCode) {
return fmt.Errorf("unable to fetch authorization token. received status code: %d", response.StatusCode)
}
webToken, err := jwt.ParseSignedAndEncrypted(string(body))
if err != nil {
return err
}
decryptedData, err := webToken.Decrypt(o.decryptionKey)
if err != nil {
return err
}
err = decryptedData.Claims(o.verificationKey, dest)
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(code string, user interface{}) error {
options := map[string]string{
"client_id": o.oauth2Config.ClientID,
}
oauth2Token, err := o.Exchange(code, options)
if err != nil {
return err
}
return o.UserInfo(oauth2.StaticTokenSource(oauth2Token), &user)
}