-
Notifications
You must be signed in to change notification settings - Fork 5
/
impl.go
609 lines (576 loc) · 17.8 KB
/
impl.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package oidc
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/Cloud-Foundations/golib/pkg/auth/authinfo"
"github.com/Cloud-Foundations/golib/pkg/auth/authinfo/x509util"
"github.com/Cloud-Foundations/golib/pkg/constants"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
const (
cookieNamePrefix = "cf_golib_oidc_authn_cookie"
secondsBetweenCleanup = 60
cookieExpirationHours = 3
maxAgeSecondsRedirCookie = 120
redirCookieName = "cf_golib_oidc_oauth2_redir"
oauth2redirectPath = "/oauth2/redirectendpoint"
authNCookieExpirationDuration = 12 * time.Hour
)
type accessToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
IDToken string `json:"id_token"`
}
type authNHandler struct {
authCookieName string
config Config
netClient *http.Client
params Params
sharedSecrets []string
mutex sync.Mutex // Protect everything below.
cachedUserGroups map[string]expiringGroups // Key: username.
}
type authInfo struct {
Username string `json:"username,omitempty"`
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience []string `json:"aud,omitempty"`
Expiration int64 `json:"exp,omitempty"` // Seconds since the Epoch.
NotBefore int64 `json:"nbf,omitempty"`
}
type expiringGroups struct {
expires time.Time
groups []string
}
type oauth2StateJWT struct {
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience []string `json:"aud,omitempty"`
Expiration int64 `json:"exp,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
ReturnURL string `json:"return_url,omitempty"`
}
type openidConnectUserInfo struct {
Subject string `json:"sub"`
Name string `json:"name"`
Login string `json:"login,omitempty"`
Username string `json:"username,omitempty"`
PreferredUsername string `json:"preferred_username,omitempty"`
Email string `json:"email,omitempty"`
Groups []string `json:"groups,omitempty"`
}
type openidConnectProviderConfiguration struct {
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
UserinfoURL string `json:"userinfo_endpoint"`
}
type usernameSetter interface {
SetUsername(username string)
}
func getAuthInfoFromRequest(req *http.Request) *authinfo.AuthInfo {
return authinfo.GetAuthInfoFromContext(req.Context())
}
func getUsernameFromUserinfo(userInfo openidConnectUserInfo) string {
username := userInfo.Username
if len(username) < 1 {
username = userInfo.Login
}
if len(username) < 1 {
username = userInfo.PreferredUsername
}
if len(username) < 1 {
username = userInfo.Email
}
return username
}
func newAuthNHandler(config Config, params Params) (*authNHandler, error) {
h := &authNHandler{
authCookieName: cookieNamePrefix,
config: config,
netClient: &http.Client{Timeout: time.Second * 15},
params: params,
cachedUserGroups: make(map[string]expiringGroups),
}
if err := h.loadSharedSecrets(); err != nil {
return nil, err
}
if err := h.populateEndpoints(); err != nil {
return nil, err
}
return h, nil
}
// Generates a valid auth cookie that can be used by clients, should only be
// used by users of the lib in their test functions
func (h *authNHandler) GenValidAuthCookie(userInfo *openidConnectUserInfo) (
*http.Cookie, error) {
expires := time.Now().Add(time.Hour * cookieExpirationHours)
return h.genValidAuthCookieExpiration(userInfo, expires, "localhost")
}
func (h *authNHandler) genValidAuthCookieExpiration(
userInfo *openidConnectUserInfo, expires time.Time, issuer string) (
*http.Cookie, error) {
key := []byte(h.sharedSecrets[0])
sig, err := jose.NewSigner(jose.SigningKey{
Algorithm: jose.HS256,
Key: key,
},
(&jose.SignerOptions{}).WithType("JWT"))
if err != nil {
return nil, fmt.Errorf("genValidAuthCookieExpiration: %s", err)
}
subject := "state:" + h.authCookieName
authToken := authInfo{
Issuer: issuer,
Subject: subject,
Audience: []string{issuer},
Username: userInfo.Username,
Expiration: expires.Unix(),
}
// TODO: add IssuedAt and NotBefore?
authToken.NotBefore = time.Now().Unix()
//stateToken.IssuedAt = stateToken.NotBefore
cookieValue, err := jwt.Signed(sig).Claims(authToken).CompactSerialize()
if err != nil {
return nil, err
}
userCookie := http.Cookie{
Name: h.authCookieName,
Value: cookieValue,
Path: "/",
Expires: expires,
HttpOnly: true,
Secure: true,
}
return &userCookie, nil
}
// Returns cached group information for the user. If there is no valid cached
// information, returns false.
func (h *authNHandler) getCachedUserGroups(username string) (bool, []string) {
h.mutex.Lock()
defer h.mutex.Unlock()
eg, ok := h.cachedUserGroups[username]
if !ok {
return false, nil
}
if time.Since(eg.expires) > 0 {
delete(h.cachedUserGroups, username)
return false, nil
}
return true, eg.groups
}
func (h *authNHandler) setCachedUserGroups(username string, groups []string,
expires time.Time) {
h.mutex.Lock()
defer h.mutex.Unlock()
if len(groups) < 1 {
delete(h.cachedUserGroups, username)
return
}
h.cachedUserGroups[username] = expiringGroups{
expires: expires,
groups: groups,
}
}
func (h *authNHandler) setAndStoreAuthCookie(w http.ResponseWriter,
r *http.Request, userInfo *openidConnectUserInfo, expires time.Time) error {
userCookie, err := h.genValidAuthCookieExpiration(userInfo, expires, r.Host)
if err != nil {
return err
}
h.params.Logger.Debugf(2,
"setAndStoreAuthCookie: %s host: %s query: %s name: %s\n",
r.URL.Path, r.Host, r.URL.RawQuery, userCookie.Name)
http.SetCookie(w, userCookie)
h.setCachedUserGroups(userInfo.Username, userInfo.Groups, expires)
return nil
}
func (h *authNHandler) getRedirURL(r *http.Request) string {
return "https://" + r.Host + oauth2redirectPath
}
func (h *authNHandler) generateAuthCodeURL(state string,
r *http.Request) string {
h.params.Logger.Debugf(2, "generateAuthCodeURL: %s query: %s\n",
r.URL.Path, r.URL.RawQuery)
var buf bytes.Buffer
buf.WriteString(h.config.AuthURL)
redirectURL := h.getRedirURL(r)
v := url.Values{
"response_type": {"code"},
"client_id": {h.config.ClientID},
"scope": {h.config.Scopes},
"redirect_uri": {redirectURL},
}
if state != "" {
// TODO(light): Docs say never to omit state; don't allow empty.
v.Set("state", state)
}
if strings.Contains(h.config.AuthURL, "?") {
buf.WriteByte('&')
} else {
buf.WriteByte('?')
}
buf.WriteString(v.Encode())
return buf.String()
}
func (h *authNHandler) generateSharedSecrets() error {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return err
}
h.sharedSecrets = []string{base64.StdEncoding.EncodeToString(buf)}
h.params.Logger.Println("Generated shared secrets")
return nil
}
func (h *authNHandler) generateValidStateString(r *http.Request) (
string, error) {
key := []byte(h.sharedSecrets[0])
sig, err := jose.NewSigner(jose.SigningKey{
Algorithm: jose.HS256,
Key: key,
}, (&jose.SignerOptions{}).WithType("JWT"))
if err != nil {
h.params.Logger.Printf("err=%s", err)
return "", err
}
issuer := r.Host
subject := "state:" + redirCookieName
stateToken := oauth2StateJWT{
Issuer: issuer,
Subject: subject,
Audience: []string{issuer},
ReturnURL: r.URL.String(),
}
h.params.Logger.Debugf(2,
"generateValidStateString: issuer: %s subject: %s\n", issuer, subject)
stateToken.NotBefore = time.Now().Unix()
stateToken.IssuedAt = stateToken.NotBefore
stateToken.Expiration = stateToken.IssuedAt + maxAgeSecondsRedirCookie
return jwt.Signed(sig).Claims(stateToken).CompactSerialize()
}
// This is where the redirect to the oath2 provider is computed.
func (h *authNHandler) oauth2DoRedirectoToProviderHandler(w http.ResponseWriter,
r *http.Request) {
h.params.Logger.Debugf(2,
"oauth2DoRedirectoToProviderHandler: %s query: %s\n",
r.URL.Path, r.URL.RawQuery)
stateString, err := h.generateValidStateString(r)
if err != nil {
h.params.Logger.Printf("err=%s", err)
http.Error(w, "Internal Error ", http.StatusInternalServerError)
return
}
redirectURL := h.generateAuthCodeURL(stateString, r)
h.params.Logger.Debugf(2,
"oauth2DoRedirectoToProviderHandler: redirecting to: %s\n", redirectURL)
http.Redirect(w, r, redirectURL, http.StatusFound)
}
// Next are the functions fo checking the callback
func (h *authNHandler) JWTClaims(t *jwt.JSONWebToken, dest ...interface{}) (
err error) {
for _, key := range h.sharedSecrets {
binkey := []byte(key)
err = t.Claims(binkey, dest...)
if err == nil {
return nil
}
}
if err != nil {
return err
}
err = errors.New("No valid key found")
return err
}
func (h *authNHandler) getBytesFromSuccessfullPost(url string,
data url.Values) ([]byte, error) {
response, err := h.netClient.PostForm(url, data)
if err != nil {
h.params.Logger.Printf("err=%s", err)
return nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
h.params.Logger.Printf("err=%s", err)
return nil, err
}
if response.StatusCode >= 300 {
h.params.Logger.Printf(string(responseBody))
return nil, errors.New("invalid status code")
}
return responseBody, nil
}
func (h *authNHandler) getVerifyReturnStateJWT(r *http.Request) (
oauth2StateJWT, error) {
inboundJWT := oauth2StateJWT{}
serializedState := r.URL.Query().Get("state")
if len(serializedState) < 1 {
return inboundJWT, errors.New("null inbound state")
}
tok, err := jwt.ParseSigned(serializedState)
if err != nil {
return inboundJWT, err
}
if err := h.JWTClaims(tok, &inboundJWT); err != nil {
h.params.Logger.Printf("error parsing claims err=%s", err)
return inboundJWT, err
}
// At this point we know the signature is valid, but now we must
// validate the contents of the JWT token
issuer := r.Host
subject := "state:" + redirCookieName
if inboundJWT.Issuer != issuer || inboundJWT.Subject != subject ||
inboundJWT.NotBefore > time.Now().Unix() ||
inboundJWT.Expiration < time.Now().Unix() {
return inboundJWT, errors.New("invalid JWT values")
}
h.params.Logger.Debugf(1,
"getVerifyReturnStateJWT: inbound JWT expiration: %s\n",
time.Unix(inboundJWT.Expiration, 0))
return inboundJWT, nil
}
func (h *authNHandler) loadSharedSecrets() error {
if h.config.SharedSecretFilename == "" {
return h.generateSharedSecrets()
}
if file, err := os.Open(h.config.SharedSecretFilename); err != nil {
if !os.IsNotExist(err) {
return err
}
if err := h.generateSharedSecrets(); err != nil {
return err
}
h.params.Logger.Printf("Writing shared secrets to file: %s\n",
h.config.SharedSecretFilename)
return ioutil.WriteFile(h.config.SharedSecretFilename,
[]byte(h.sharedSecrets[0]+"\n"), 0600)
} else {
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
h.sharedSecrets = append(h.sharedSecrets, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
h.params.Logger.Printf("Read shared secrets from file: %s\n",
h.config.SharedSecretFilename)
return nil
}
}
func (h *authNHandler) oauth2RedirectPathHandler(w http.ResponseWriter,
r *http.Request) {
h.params.Logger.Debugf(2, "oauth2RedirectPathHandler: %s query: %s\n",
r.URL.Path, r.URL.RawQuery)
if r.Method != "GET" {
h.params.Logger.Printf("Bad method on redirect, should only be GET")
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
authCode := r.URL.Query().Get("code")
if len(authCode) < 1 {
h.params.Logger.Println("null code")
http.Error(w, "null code", http.StatusUnauthorized)
return
}
inboundJWT, err := h.getVerifyReturnStateJWT(r)
if err != nil {
h.params.Logger.Printf("error processing state err=%s", err)
http.Error(w, "null or bad inboundState", http.StatusUnauthorized)
return
}
h.params.Logger.Debugf(1,
"oauth2RedirectPathHandler: inboundJWT expires: %s\n",
time.Unix(inboundJWT.Expiration, 0))
// OK state is valid.. now we perform the token exchange
redirectURL := h.getRedirURL(r)
tokenRespBody, err := h.getBytesFromSuccessfullPost(h.config.TokenURL,
url.Values{"redirect_uri": {redirectURL},
"code": {authCode},
"grant_type": {"authorization_code"},
"client_id": {h.config.ClientID},
"client_secret": {h.config.ClientSecret},
})
if err != nil {
h.params.Logger.Printf("err=%s", err)
http.Error(w, "bad transaction with openic context ",
http.StatusInternalServerError)
return
}
var oauth2AccessToken accessToken
err = json.Unmarshal(tokenRespBody, &oauth2AccessToken)
if err != nil {
h.params.Logger.Printf(string(tokenRespBody))
http.Error(w, "cannot decode oath2 response for token ",
http.StatusInternalServerError)
return
}
// TODO: tolower
if oauth2AccessToken.TokenType != "Bearer" ||
len(oauth2AccessToken.AccessToken) < 1 {
h.params.Logger.Printf(string(tokenRespBody))
http.Error(w, "invalid accessToken ", http.StatusInternalServerError)
return
}
h.params.Logger.Debugf(1,
"oauth2RedirectPathHandler: access token expires: %s\n",
time.Now().Add(time.Second*time.Duration(oauth2AccessToken.ExpiresIn)))
// Now we use the access_token (from token exchange) to get userinfo
userInfoRespBody, err := h.getBytesFromSuccessfullPost(h.config.UserinfoURL,
url.Values{"access_token": {oauth2AccessToken.AccessToken}})
if err != nil {
h.params.Logger.Printf("err=%s", err)
http.Error(w, "bad transaction with openic context ",
http.StatusInternalServerError)
return
}
var userInfo openidConnectUserInfo
err = json.Unmarshal(userInfoRespBody, &userInfo)
if err != nil {
h.params.Logger.Printf(string(tokenRespBody))
http.Error(w, "cannot decode oath2 userinfo token ",
http.StatusInternalServerError)
return
}
userInfo.Username = getUsernameFromUserinfo(userInfo)
sort.Strings(userInfo.Groups)
err = h.setAndStoreAuthCookie(w, r, &userInfo,
time.Now().Add(authNCookieExpirationDuration))
if err != nil {
h.params.Logger.Println(err)
http.Error(w, "cannot set auth Cookie", http.StatusInternalServerError)
return
}
destinationPath := inboundJWT.ReturnURL
http.Redirect(w, r, destinationPath, http.StatusFound)
}
func (h *authNHandler) populateEndpoints() error {
if h.config.AuthURL != "" &&
h.config.TokenURL != "" &&
h.config.UserinfoURL != "" {
return nil
}
resp, err := h.netClient.Get(
h.config.ProviderURL + constants.OpenIDCConfigurationDocumentPath)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
ioutil.ReadAll(resp.Body)
return errors.New(resp.Status)
}
var providerConfig openidConnectProviderConfiguration
if err := json.NewDecoder(resp.Body).Decode(&providerConfig); err != nil {
return err
}
if h.config.AuthURL == "" {
h.config.AuthURL = providerConfig.AuthURL
}
if h.config.TokenURL == "" {
h.config.TokenURL = providerConfig.TokenURL
}
if h.config.UserinfoURL == "" {
h.config.UserinfoURL = providerConfig.UserinfoURL
}
return nil
}
func (h *authNHandler) verifyAuthnCookie(cookieValue string, issuer string) (
authInfo, bool, error) {
if len(cookieValue) < 1 {
return authInfo{}, false, nil
}
inboundJWT := authInfo{}
tok, err := jwt.ParseSigned(cookieValue)
if err != nil {
return inboundJWT, false, nil
}
if err := h.JWTClaims(tok, &inboundJWT); err != nil {
h.params.Logger.Printf("error parsing claims err=%s", err)
return inboundJWT, false, nil
}
subject := "state:" + h.authCookieName
if inboundJWT.Issuer != issuer || inboundJWT.Subject != subject ||
inboundJWT.NotBefore > time.Now().Unix() ||
inboundJWT.Expiration < time.Now().Unix() {
err = errors.New("invalid JWT values")
return inboundJWT, false, nil
}
authInfo := inboundJWT
return authInfo, true, nil
}
func (h *authNHandler) getRemoteAuthInfo(w http.ResponseWriter,
r *http.Request) (*authinfo.AuthInfo, error) {
h.params.Logger.Debugf(2, "getRemoteAuthInfo: %s query: %s\n",
r.URL.Path, r.URL.RawQuery)
// If you have a verified cert, no need for cookies
if r.TLS != nil && len(r.TLS.VerifiedChains) > 0 {
authInfo, err := x509util.GetAuthInfo(r.TLS.VerifiedChains[0][0])
if err == nil {
return authInfo, nil
}
h.params.Logger.Println(err)
}
remoteCookie, err := r.Cookie(h.authCookieName)
if err != nil {
h.oauth2DoRedirectoToProviderHandler(w, r)
return nil, fmt.Errorf("error getting cookie: %s: %s",
h.authCookieName, err)
}
authInfo, ok, err := h.verifyAuthnCookie(remoteCookie.Value, r.Host)
if err != nil {
return nil, err
}
if !ok {
h.oauth2DoRedirectoToProviderHandler(w, r)
return nil, errors.New("Cookie not found")
}
valid, groups := h.getCachedUserGroups(authInfo.Username)
if !valid {
h.oauth2DoRedirectoToProviderHandler(w, r)
return nil, errors.New("No valid cached group data")
}
return &authinfo.AuthInfo{
Groups: groups,
Username: authInfo.Username,
}, nil
}
func (h *authNHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.params.Logger.Debugf(0, "Inside the handler path=%s query=%s",
r.URL.Path, r.URL.RawQuery)
if strings.HasPrefix(r.URL.Path, oauth2redirectPath) {
h.oauth2RedirectPathHandler(w, r)
return
}
authInfo, err := h.getRemoteAuthInfo(w, r)
if err != nil {
h.params.Logger.Debugln(1, err)
return
}
if h.params.AddHeaders {
r.Header.Set("X-Remote-User", authInfo.Username)
r.Header.Set("X-Forwarded-User", authInfo.Username)
}
if us, ok := w.(usernameSetter); ok {
us.SetUsername(authInfo.Username)
}
h.params.Logger.Debugf(1, "authenticated user: %s for path: %s\n",
authInfo.Username, r.URL.Path)
h.params.Handler.ServeHTTP(w,
r.WithContext(authinfo.ContextWithAuthInfo(r.Context(), *authInfo)))
}