-
Notifications
You must be signed in to change notification settings - Fork 328
/
auth.go
511 lines (429 loc) · 13.2 KB
/
auth.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
// Copyright 2020 H2O.ai, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wave
import (
"context"
"crypto/rand"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"sync"
"time"
"github.com/coreos/go-oidc"
"github.com/google/uuid"
"github.com/h2oai/wave/pkg/keychain"
"golang.org/x/oauth2"
)
const authCookieName = "oidcsession"
var authDefaultScopes = []string{oidc.ScopeOpenID, "profile"}
func connectToProvider(conf *AuthConf) (*oauth2.Config, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
provider, err := oidc.NewProvider(ctx, conf.ProviderURL)
if err != nil {
return nil, err
}
scopes := authDefaultScopes
if len(conf.Scopes) > 0 && conf.Scopes[0] != "" {
scopes = conf.Scopes
}
return &oauth2.Config{
ClientID: conf.ClientID,
ClientSecret: conf.ClientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: conf.RedirectURL,
Scopes: scopes,
}, nil
}
// Session represents an end-user session
type Session struct {
sync.RWMutex
id string
state string
nonce string
subject string
username string
successURL string
token *oauth2.Token
expiry time.Time
}
var errInactivityTimeout = errors.New("timed out due to inactivity")
func (s *Session) touch(timeout time.Duration) error {
if s == anonymous {
return nil
}
now := time.Now()
s.RLock()
expired := s.expiry.Before(now)
s.RUnlock()
if expired {
return errInactivityTimeout
}
s.Lock()
s.expiry = now.Add(timeout)
s.Unlock()
return nil
}
const (
anon = "anon"
)
var anonymous = &Session{
subject: anon,
username: anon,
token: &oauth2.Token{},
expiry: time.Now().Add(365 * 24 * time.Hour),
}
// Auth holds authenticated end-user sessions
type Auth struct {
sync.RWMutex
conf *AuthConf
oauth *oauth2.Config
sessions map[string]*Session
baseURL string
initURL string
loginURL string
}
func newAuth(conf *AuthConf, baseURL, initURL, loginURL string) (*Auth, error) {
oauth, err := connectToProvider(conf)
if err != nil {
return nil, err
}
return &Auth{
conf: conf,
oauth: oauth,
sessions: make(map[string]*Session),
baseURL: baseURL,
initURL: initURL,
loginURL: loginURL,
}, nil
}
func (auth *Auth) get(key string) (*Session, bool) {
auth.RLock()
defer auth.RUnlock()
session, ok := auth.sessions[key]
return session, ok
}
func (auth *Auth) set(session *Session) {
auth.Lock()
defer auth.Unlock()
auth.sessions[session.id] = session
}
func (auth *Auth) remove(key string) {
auth.Lock()
defer auth.Unlock()
delete(auth.sessions, key)
}
func (auth *Auth) identify(r *http.Request) *Session {
cookie, err := r.Cookie(authCookieName)
if err != nil {
echo(Log{"t": "oauth2_cookie_read", "warning": err.Error()})
return nil
}
sessionID := cookie.Value
session, ok := auth.get(sessionID)
if !ok {
echo(Log{"t": "oauth2_session", "error": "invalid session", "session_id": sessionID})
return nil
}
if err := session.touch(auth.conf.InactivityTimeout); err != nil {
echo(Log{"t": "inactivity_timeout", "subject": session.subject})
return nil
}
token, err := auth.ensureValidOAuth2Token(r.Context(), session.token)
if err != nil {
echo(Log{"t": "oauth2_token_refresh", "error": err.Error(), "subject": session.subject})
return nil
}
if session.token != token {
session.token = token
auth.set(session)
}
return session
}
func (auth *Auth) allow(r *http.Request) bool {
return auth.identify(r) != nil
}
func (auth *Auth) wrap(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(path.Ext(r.URL.Path)) > 0 {
h.ServeHTTP(w, r)
return
}
if r.URL.Path == auth.loginURL {
if auth.conf.SkipLogin {
auth.redirectToAuth(w, r)
return
}
h.ServeHTTP(w, r)
return
}
if !auth.allow(r) {
auth.redirectToLogin(w, r)
return
}
h.ServeHTTP(w, r)
})
}
func (auth *Auth) ensureValidOAuth2Token(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
token, err := auth.oauth.TokenSource(ctx, token).Token()
if token == nil {
echo(Log{"t": "ensure_token_refresh", "error": "refresh token is nil"})
echo(Log{"t": "ensure_token_refresh", "error": err.Error()})
}
return token, err
}
func (auth *Auth) redirectToLogin(w http.ResponseWriter, r *http.Request) {
// X -> /_auth/login?next=X
u, _ := url.Parse(auth.loginURL)
q := u.Query()
q.Set("next", r.URL.Path)
u.RawQuery = q.Encode()
http.Redirect(w, r, u.String(), http.StatusFound)
}
func (auth *Auth) redirectToAuth(w http.ResponseWriter, r *http.Request) {
// /_auth/login -> /_auth/init
// /_auth/login?next=X -> /_auth/init?next=X
u, _ := url.Parse(auth.initURL)
next := r.URL.Query().Get("next")
if next != "" {
q := u.Query()
q.Set("next", next)
u.RawQuery = q.Encode()
}
http.Redirect(w, r, u.String(), http.StatusFound)
}
func generateRandomKey(byteCount int) (string, error) {
b := make([]byte, byteCount)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", b), nil
}
// LoginHandler handles auth requests
type LoginHandler struct {
auth *Auth
}
func newLoginHandler(auth *Auth) http.Handler {
return &LoginHandler{auth}
}
func (h *LoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// `state` is to protect from CSRF (OAuth2 part).
state, err := generateRandomKey(4)
if err != nil {
echo(Log{"t": "oidc_random_state_key", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// `nonce` is to protect from replay attacks (OpenID part).
nonce, err := generateRandomKey(4)
if err != nil {
echo(Log{"t": "oidc_random_nonce_key", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
successURL := h.auth.baseURL
if nextValues, ok := r.URL.Query()["next"]; ok {
successURL = nextValues[0]
}
// Session ID stored in cookie.
sessionID := uuid.New().String()
h.auth.set(&Session{id: sessionID, state: state, nonce: nonce, successURL: successURL, expiry: time.Now().Add(h.auth.conf.InactivityTimeout)})
cookie := http.Cookie{Name: authCookieName, Value: sessionID, Path: h.auth.baseURL, Expires: time.Now().Add(h.auth.conf.SessionExpiry)}
http.SetCookie(w, &cookie)
var options []oauth2.AuthCodeOption
options = append(options, oidc.Nonce(nonce))
for _, param := range h.auth.conf.URLParameters {
options = append(options, oauth2.SetAuthURLParam(param[0], param[1]))
}
http.Redirect(w, r, h.auth.oauth.AuthCodeURL(state, options...), http.StatusFound)
}
// AuthHandler handles OAuth2 requests
type AuthHandler struct {
auth *Auth
}
func newAuthHandler(auth *Auth) http.Handler {
return &AuthHandler{auth}
}
func (h *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Retrieve saved session.
cookie, err := r.Cookie(authCookieName)
if err != nil {
echo(Log{"t": "oauth2_cookie", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
sessionID := cookie.Value
session, ok := h.auth.get(sessionID)
if !ok {
echo(Log{"t": "oauth2_session", "error": "not found"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Handle errors from provider.
if err := r.URL.Query().Get("error"); err != "" {
errorDescription := r.URL.Query().Get("error_description")
echo(Log{"t": "oauth2_callback", "error": err, "description": errorDescription})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Compare to stored state.
responseState := r.URL.Query().Get("state")
if session.state != responseState {
echo(Log{"t": "oauth2_state", "error": "failed matching state"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
oAuth2Provider, err := oidc.NewProvider(r.Context(), h.auth.conf.ProviderURL)
if err != nil {
echo(Log{"t": "oauth2_oidc_provider", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
oauth2Token, err := h.auth.oauth.Exchange(r.Context(), r.URL.Query().Get("code"))
if err != nil {
echo(Log{"t": "oauth2_exchange", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
echo(Log{"t": "oauth2_exchange", "error": "failed reading id_token"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
oidcVerifier := oAuth2Provider.Verifier(&oidc.Config{ClientID: h.auth.oauth.ClientID})
idToken, err := oidcVerifier.Verify(r.Context(), rawIDToken)
if err != nil {
echo(Log{"t": "oauth2_oidc_verifier", "error": "failed verifying id_token"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
var claims struct {
PreferredUsername string `json:"preferred_username"`
Nonce string `json:"nonce"`
}
err = idToken.Claims(&claims)
if err != nil {
echo(Log{"t": "oauth2_claim", "error": "failed parsing token claims"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Compare to stored nonce.
if session.nonce != claims.Nonce {
if !ok {
echo(Log{"t": "oauth2_nonce", "error": "failed matching nonce"})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
session.token = oauth2Token
session.subject = idToken.Subject
session.username = claims.PreferredUsername
echo(Log{"t": "login", "subject": session.subject, "username": session.username})
h.auth.set(session)
http.Redirect(w, r, session.successURL, http.StatusFound)
}
// LogoutHandler handles logout requests
type LogoutHandler struct {
auth *Auth
broker *Broker
}
func newLogoutHandler(auth *Auth, broker *Broker) http.Handler {
return &LogoutHandler{auth, broker}
}
func (h *LogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var idToken string
// Retrieve saved session.
cookie, err := r.Cookie(authCookieName)
if err != nil {
echo(Log{"t": "logout_cookie", "error": "not found"})
h.redirect(w, r, idToken)
return
}
sessionID := cookie.Value
session, ok := h.auth.get(sessionID)
// Delete cookie.
cookie.MaxAge = -1
http.SetCookie(w, cookie)
// Purge session
h.auth.remove(sessionID)
if ok {
// Reload all of this user's browser tabs
h.broker.resetClients(session)
// A token may not be present if the oauth2 workflow failed, so check before access.
if session.token != nil {
idToken, _ = session.token.Extra("id_token").(string) // raw id_token (required by Okta)
}
}
h.redirect(w, r, idToken)
}
func (h *LogoutHandler) redirect(w http.ResponseWriter, r *http.Request, idToken string) {
if h.auth.conf.EndSessionURL == "" {
http.Redirect(w, r, h.auth.baseURL, http.StatusFound)
return
}
redirectURL, err := url.Parse(h.auth.conf.EndSessionURL)
if err != nil {
echo(Log{"t": "logout_redirect_parse", "error": err.Error()})
return
}
post_logout_redirect_url := h.auth.conf.PostLogoutRedirectURL
if post_logout_redirect_url == "" {
post_logout_redirect_url = r.Host
}
query := redirectURL.Query()
query.Set("post_logout_redirect_uri", post_logout_redirect_url)
if len(idToken) > 0 {
// required by Okta
// https://developer.okta.com/docs/reference/api/oidc/#logout
query.Set("id_token_hint", idToken)
}
redirectURL.RawQuery = query.Encode()
http.Redirect(w, r, redirectURL.String(), http.StatusFound)
}
// Handles token refreshes.
type RefreshHandler struct {
auth *Auth
keychain *keychain.Keychain
}
func newRefreshHandler(auth *Auth, keychain *keychain.Keychain) http.Handler {
return &RefreshHandler{auth, keychain}
}
func (h *RefreshHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !h.keychain.Guard(w, r) { // API
return
}
sessionID := r.Header.Get("Wave-Session-ID")
session, ok := h.auth.get(sessionID)
if !ok {
echo(Log{"t": "refresh_session", "error": "session unavailable"})
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
token, err := h.auth.ensureValidOAuth2Token(r.Context(), session.token)
if err != nil {
// Purge session and reload clients if refresh not successful?
echo(Log{"t": "refresh_session", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
session.token = token
h.auth.set(session)
w.Header().Set("Wave-Access-Token", token.AccessToken)
w.Header().Set("Wave-Refresh-Token", token.RefreshToken)
w.WriteHeader(http.StatusOK)
}