-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
774 lines (733 loc) · 21.9 KB
/
main.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io"
"path/filepath"
"text/template"
"github.com/google/uuid"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
"os"
"strings"
"time"
"gopkg.in/boj/redistore.v1"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
_ "golang.org/x/net/context"
"golang.org/x/oauth2"
)
var (
// store contains the session storage driver
store sessions.Store
// cfgs contains all supported OAuth2 application configurations
cfgs []*OAuth2Config
// ssokey contains the encryption key derived from session key
ssokey []byte
)
// SSODomainConfig contains the configuration for SSO Cookies
type SSODomainConfig struct {
Token *OAuth2Token `json:"Token"`
CookieName string `json:"CookieName"`
HttpOnly bool `json:"HttpOnly"`
RedirectURL string `json:"RedirectURL"`
SSODomain string `json:"SSODomain"`
}
// OAuth2Config contains the base OAuth2 config as well as additional information for session
type OAuth2Config struct {
OAuth2 *oauth2.Config `json:"OAuth2"`
ID string `json:"ID"`
Vault *VaultClient `json:"Vault"`
LogoutURL string `json:"LogoutURL"`
CookieName string `json:"CookieName"`
HttpOnly bool `json:"HttpOnly"`
DefaultRedirectURI string `json:"DefaultRedirectURI"`
SSODomain string `json:"SSODomain"`
}
type OAuth2Token struct {
// AccessToken is the token that authorizes and authenticates
// the requests.
AccessToken string `json:"access_token"`
// IDToken is the token that validates the client's identity.
IDToken string `json:"id_token"`
// TokenType is the type of token.
// The Type method returns either this or "Bearer", the default.
TokenType string `json:"token_type,omitempty"`
// RefreshToken is a token that's used by the application
// (as opposed to the user) to refresh the access token
// if it expires.
RefreshToken string `json:"refresh_token,omitempty"`
// Expiry is the optional expiration time of the access token.
//
// If zero, TokenSource implementations will reuse the same
// token forever and RefreshToken or equivalent
// mechanisms for that TokenSource will not be used.
Expiry time.Time `json:"expiry,omitempty"`
// raw optionally contains extra metadata from the server
// when updating a token.
raw interface{}
}
func (o *OAuth2Token) OAuth2Token() *oauth2.Token {
return &oauth2.Token{
AccessToken: o.AccessToken,
TokenType: o.TokenType,
RefreshToken: o.RefreshToken,
Expiry: o.Expiry,
}
}
// SessionState returns the client's session in a base64 encoded string
func SessionState(session *sessions.Session) string {
return base64.StdEncoding.EncodeToString(sha256.New().Sum([]byte(session.ID)))
}
// getRequestID retrieves a request_id for the request and creates one if it does not exist
func getRequestID(r *http.Request) string {
l := log.WithFields(log.Fields{
"action": "getRequestID",
})
l.Print("getRequestID")
// check istio request headers
if r.Header.Get("x-request-id") != "" {
l.Printf("istio x-request-id=%s", r.Header.Get("x-request-id"))
return r.Header.Get("x-request-id")
}
// check our session
session, _ := store.Get(r, getSessionName(r))
if v, ok := session.Values["request_id"].(string); ok {
l.Printf("session request_id=%s", v)
l.Printf("set x-request-id=%s", v)
r.Header.Set("x-request-id", v)
return v
}
// create a new
uid := uuid.New()
us := uid.String()
l.Printf("new uuid=%s", us)
l.Printf("set x-request-id=%s", us)
r.Header.Set("x-request-id", us)
return us
}
func RefreshHandler(w http.ResponseWriter, r *http.Request) {
log.Println("RefreshHandler")
values := mux.Vars(r)
cid := values["ID"]
// retrieve OAuth2 application for client
config, err := getAppByAppID(cid)
if err != nil {
http.Error(w, fmt.Sprintf("get client error: %v", err), http.StatusBadRequest)
return
}
var token *oauth2.Token
// retrieve refresh token from cookies
refreshCookieName := config.CookieName + "_refresh"
c, err := r.Cookie(refreshCookieName)
if err != nil {
log.Printf("RefreshHandler error: %v", err)
http.Error(w, fmt.Sprintf("cookie error: %v", err), http.StatusBadRequest)
return
}
// base64 decode the data
strJson, err := base64.StdEncoding.DecodeString(c.Value)
if err != nil {
log.Printf("RefreshHandler error: %v", err)
http.Error(w, fmt.Sprintf("base64 error: %v", err), http.StatusBadRequest)
return
}
if err := json.Unmarshal([]byte(strJson), &token); err != nil {
log.Printf("RefreshHandler error: %v", err)
http.Error(w, fmt.Sprintf("json error: %v", err), http.StatusBadRequest)
return
}
if token.RefreshToken != "" {
newToken, refreshErr := config.OAuth2.TokenSource(r.Context(), token).Token()
if refreshErr != nil {
log.Printf("RefreshHandler error: %v", refreshErr)
http.Error(w, fmt.Sprintf("refresh error: %v", refreshErr), http.StatusBadRequest)
return
}
token = newToken
nt := &OAuth2Token{
AccessToken: token.AccessToken,
TokenType: token.TokenType,
RefreshToken: token.RefreshToken,
Expiry: token.Expiry,
}
sso := &SSODomainConfig{
CookieName: config.CookieName,
Token: nt,
SSODomain: config.SSODomain,
HttpOnly: config.HttpOnly,
RedirectURL: "",
}
// set SSO cookie on all supported domains
sso.SetCookie(w, r)
} else {
log.Printf("LoginHandler redirect ClientID=%v, redirect_uri=%v\n", cid, config.DefaultRedirectURI)
http.Redirect(w, r, config.DefaultRedirectURI, http.StatusTemporaryRedirect)
}
}
// LoginHandler handles calls to the root to either redirect to IDP or back to application after auth
func LoginHandler(w http.ResponseWriter, r *http.Request) {
l := log.WithFields(log.Fields{
"action": "LoginHandler",
})
l.Print("LoginHandler")
config, err := configFromRequest(r)
if err != nil {
l.Print(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
l.Print("getSession")
session, _ := store.Get(r, getSessionName(r))
session.Options.MaxAge = 0
session.Options.Domain = config.SSODomain
session.Options.Path = "/"
rid := getRequestID(r)
l = l.WithFields(log.Fields{
"request_id": rid,
"action": "LoginHandler",
})
session.Values["redirect_uri"] = config.DefaultRedirectURI
session.Values["ID"] = config.ID
session.Values["client_id"] = config.OAuth2.ClientID
if r.FormValue("redirect") != "" {
l.Printf("redirect_uri=%v", r.FormValue("redirect"))
session.Values["redirect_uri"] = r.FormValue("redirect")
}
sessions.Save(r, w)
u := config.OAuth2.AuthCodeURL(SessionState(session), oauth2.AccessTypeOnline)
log.Printf("LoginHandler auth ClientID=%v, redirect_uri=%v, auth_code_url=%v\n", session.Values["ID"], session.Values["redirect_uri"], u)
http.Redirect(w, r, u, http.StatusTemporaryRedirect)
}
func clearSession(session *sessions.Session, r *http.Request, w http.ResponseWriter) {
var cid string
if v, ok := session.Values["client_id"]; ok && v != nil {
cid = v.(string)
}
var aid string
if v, ok := session.Values["ID"]; ok && v != nil {
aid = v.(string)
}
rid := getRequestID(r)
l := log.WithFields(log.Fields{
"action": "clearSession",
"client_id": cid,
"app_id": aid,
"request_id": rid,
})
l.Printf("clearSession")
session.Options.MaxAge = -1
sessions.Save(r, w)
}
// LogoutHandler handles session removal and redirect to IDP logout
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, getSessionName(r))
var cid string
if v, ok := session.Values["ID"]; ok {
cid = v.(string)
}
a, err := getAppByAppID(cid)
if err != nil {
// no app found just use default logout url
a = cfgs[0]
}
// remove cookie and session
// TODO: iterate through all SSO domains and remove cookies on all
http.SetCookie(w, &http.Cookie{
Name: os.Getenv("SSO_COOKIE_NAME"),
Value: "",
MaxAge: -1,
Path: "/",
Domain: os.Getenv("SSO_COOKIE_DOMAIN"),
HttpOnly: a.HttpOnly,
Secure: true,
})
clearSession(session, r, w)
// Redirect to IDP logout URL
http.Redirect(w, r, a.LogoutURL, http.StatusTemporaryRedirect)
}
// createReq creates a new OAuth login request with the IDP
// defined for the user's session
func createReq(r *http.Request, session *sessions.Session) (*http.Response, error) {
rid := getRequestID(r)
l := log.WithFields(log.Fields{
"action": "createReq",
"request_id": rid,
})
l.Printf("createReq %+v", session.Values)
var cid string
if v, ok := session.Values["ID"]; ok {
cid = v.(string)
}
l.Printf("getAppByAppID(%v)", cid)
config, err := getAppByAppID(cid)
if err != nil {
l.Print(err)
return nil, err
}
// Create new OAuth login request
form := url.Values{}
form.Set("grant_type", "authorization_code")
form.Set("client_id", config.OAuth2.ClientID)
form.Set("client_secret", config.OAuth2.ClientSecret)
form.Set("code", r.FormValue("code"))
form.Set("scope", strings.Join(config.OAuth2.Scopes, " "))
form.Set("redirect_uri", config.OAuth2.RedirectURL)
l.Printf("Login client_id=%v, scope=%v, redirect_uri=%v token_url=%v\n",
config.OAuth2.ClientID,
strings.Join(config.OAuth2.Scopes, " "),
config.OAuth2.RedirectURL,
config.OAuth2.Endpoint.TokenURL,
)
l.Printf("send oauth2 request to %v", config.OAuth2.Endpoint.TokenURL)
req, err := http.NewRequest(http.MethodPost, config.OAuth2.Endpoint.TokenURL, strings.NewReader(form.Encode()))
if err != nil {
l.Printf("send oauth NewRequest error=%v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
l.Printf("send oauth request error=%v", err)
return resp, err
}
return resp, nil
}
// setSSOCookie sets SSO cookie on all supported domains before redirecting user back to original resource
func setSSOCookie(w http.ResponseWriter, r *http.Request, session *sessions.Session, token *OAuth2Token) {
var cid string
if v, ok := session.Values["client_id"]; ok {
cid = v.(string)
}
var aid string
if v, ok := session.Values["ID"]; ok {
aid = v.(string)
}
rid := getRequestID(r)
l := log.WithFields(log.Fields{
"action": "setSSOCookie",
"client_id": cid,
"request_id": rid,
"app_id": aid,
})
l.Print("setSSOCookie")
// retrieve OAuth2 application for client
config, err := getAppByAppID(aid)
if err != nil {
l.Printf("getAppByAppID(%s) error=%v", cid, err)
http.Error(w, fmt.Sprintf("get client error: %v", err), http.StatusBadRequest)
return
}
var redirectURI = "/"
if v, ok := session.Values["redirect_uri"]; ok {
redirectURI = v.(string)
}
// create SSODomainConfig object to set SSO cookies
sso := &SSODomainConfig{
CookieName: config.CookieName,
Token: token,
SSODomain: config.SSODomain,
HttpOnly: config.HttpOnly,
RedirectURL: redirectURI,
}
// set SSO cookie on all supported domains
sso.SetCookie(w, r)
}
// SetCookie sets a SSO cookie on the current SSODomain object
// naively assuming it is running on the proper endpoint for the domain
func (sso *SSODomainConfig) SetCookie(w http.ResponseWriter, r *http.Request) {
var cid string
session, _ := store.Get(r, getSessionName(r))
if v, ok := session.Values["ID"]; ok {
cid = v.(string)
}
rid := getRequestID(r)
l := log.WithFields(log.Fields{
"action": "SetCookie",
"client_id": cid,
"request_id": rid,
})
l.Printf("SetCookie Name=%v, Domain=%v\n", sso.CookieName, sso.SSODomain)
tv := sso.Token.AccessToken
if tv == "" {
tv = sso.Token.IDToken
}
exp := sso.Token.Expiry
if exp.IsZero() {
exp = time.Now().Add(time.Minute * 60)
}
http.SetCookie(w, &http.Cookie{
Name: sso.CookieName,
Value: tv,
Expires: exp,
Domain: sso.SSODomain,
Path: "/",
HttpOnly: sso.HttpOnly,
Secure: true,
SameSite: http.SameSiteNoneMode,
})
if sso.Token.RefreshToken != "" {
jd, err := json.Marshal(sso.Token)
if err != nil {
l.Printf("json.Marshal error: %v", err)
http.Error(w, fmt.Sprintf("json error: %v", err), http.StatusBadRequest)
return
}
// base64 encode the data
jd = []byte(base64.StdEncoding.EncodeToString(jd))
http.SetCookie(w, &http.Cookie{
Name: sso.CookieName + "_refresh",
Value: string(jd),
Expires: exp,
Domain: sso.SSODomain,
Path: "/",
HttpOnly: sso.HttpOnly,
Secure: true,
SameSite: http.SameSiteNoneMode,
})
}
var redirectURI = sso.RedirectURL
if v, ok := session.Values["redirect_uri"]; ok && v != nil {
redirectURI = v.(string)
}
l.Printf("redirect_uri=%v", redirectURI)
session.Options.Domain = sso.SSODomain
session.Options.MaxAge = -1
session.Options.Path = "/"
l.Printf("sessions.Save %+v", session)
err := sessions.Save(r, w)
if err != nil {
l.Printf("sessions.Save error=%+v", err)
}
if redirectURI != "" {
l.Printf("redirect=%v", redirectURI)
http.Redirect(w, r, redirectURI, http.StatusFound)
} else {
w.WriteHeader(http.StatusOK)
}
}
// getTokenFromBody retrieves the OAuth2 token from the HTTP response body
func getTokenFromBody(r *http.Response) (*OAuth2Token, error) {
l := log.WithFields(log.Fields{
"action": "getTokenFromBody",
})
l.Printf("getTokenFromBody")
defer r.Body.Close()
if r.StatusCode >= 400 {
l.Printf("getTokenFromBody StatusCode=%v", r.StatusCode)
bd, err := io.ReadAll(r.Body)
if err != nil {
l.Printf("getTokenFromBody StatusCode=%v body read error=%v", r.StatusCode, err)
return nil, err
}
return nil, fmt.Errorf("error creating token: %v", string(bd))
}
l.Printf("getTokenFromBody JSON Decode")
var token *OAuth2Token
if err := json.NewDecoder(r.Body).Decode(&token); err != nil {
l.Printf("getTokenFromBody JSON Decode error=%v", err)
return nil, fmt.Errorf("json error: %v", err)
}
return token, nil
}
func appIDs() []string {
var ids []string
for _, v := range cfgs {
ids = append(ids, v.ID)
}
return ids
}
func appIDFromCookies(r *http.Request) string {
for _, c := range r.Cookies() {
for _, a := range appIDs() {
if c.Name == a {
return a
}
}
}
return ""
}
func getSessionName(r *http.Request) string {
vars := mux.Vars(r)
sessionName := vars["ID"]
if sessionName == "" {
sessionName = appIDFromCookies(r)
}
if sessionName == "" {
sessionName = "session"
}
l := log.WithFields(log.Fields{
"action": "getSessionName",
})
l.Printf("session_name=%v", sessionName)
return sessionName
}
// CallbackHandler handles responses from IDP
func CallbackHandler(w http.ResponseWriter, r *http.Request) {
rid := getRequestID(r)
l := log.WithFields(log.Fields{
"action": "CallbackHandler",
"request_id": rid,
})
l.Print("CallbackHandler")
session, err := store.Get(r, getSessionName(r))
if err != nil {
l.Printf("CallbackHandler error: %v\n", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
session.Options.Path = "/"
var rurl string
if v, ok := session.Values["redirect_uri"]; ok && v != nil {
rurl = v.(string)
}
l = l.WithFields(log.Fields{
"action": "CallbackHandler",
"request_id": rid,
})
l.Printf("check callback state")
if r.FormValue("state") != SessionState(session) {
l.Println("invalid callback state")
clearSession(session, r, w)
http.Redirect(w, r, "/403?request_id="+rid+"&redirect="+rurl, http.StatusTemporaryRedirect)
return
}
l.Printf("createReq")
resp, err := createReq(r, session)
if err != nil {
l.Printf("createReq error: %v\n", err)
clearSession(session, r, w)
http.Redirect(w, r, "/403?request_id="+rid+"&redirect="+rurl, http.StatusTemporaryRedirect)
return
}
l.Printf("getTokenFromBody")
token, terr := getTokenFromBody(resp)
if terr != nil || token == nil || (token.AccessToken == "" && token.IDToken == "") {
if terr != nil {
l.Printf("getTokenFromBody error: %v\n", terr)
}
if token == nil {
l.Printf("getTokenFromBody token is nil\n")
} else if token.AccessToken == "" || token.IDToken == "" {
l.Printf("getTokenFromBody token is invalid\n")
}
clearSession(session, r, w)
http.Redirect(w, r, "/403?request_id="+rid+"&redirect="+rurl, http.StatusTemporaryRedirect)
return
} else if token == nil {
l.Print("token nil, redirecting /403")
clearSession(session, r, w)
http.Redirect(w, r, "/403?request_id="+rid+"&redirect="+rurl, http.StatusTemporaryRedirect)
return
}
l.Printf("setSSOCookie")
setSSOCookie(w, r, session, token)
}
// getAppByAppID retrieves a configured OAuth2 application by the ClientID
// for configurations which have multiple ClientIDs, a separate `id` field enables
// selecting a specific configuration.
func getAppByAppID(i string) (*OAuth2Config, error) {
if i == "" {
return nil, errors.New("client not provided")
}
log.Printf("getAppByAppID request %v\n", i)
for _, v := range cfgs {
if v.ID == i {
log.Printf("getAppByAppID found %v: %+v\n", v.ID, logConfig(v))
// we found the app config - if there is a .Vault field, we need to
// retrieve the secret from Vault
if v.Vault != nil && v.Vault.Path != "" {
log.Printf("getAppByAppID vault %+v\n", v.Vault)
// we have a vault config, so we need to retrieve the secret
// from Vault
cfg, err := v.Vault.AuthAndGetConfig()
if err != nil {
log.Printf("getAppByAppID vault error: %v\n", err)
return nil, err
}
return cfg, nil
}
return v, nil
}
}
return nil, errors.New("client not found")
}
func logConfig(c *OAuth2Config) OAuth2Config {
return OAuth2Config{
ID: c.ID,
OAuth2: c.OAuth2,
LogoutURL: c.LogoutURL,
CookieName: c.CookieName,
DefaultRedirectURI: c.DefaultRedirectURI,
HttpOnly: c.HttpOnly,
SSODomain: c.SSODomain,
}
}
// configFromRequest retrieves the OAuth2 application configuration
// from the HTTP request, assuming ClientID was provided in the URL.
// If ClientID is not provided, defaults to first application
func configFromRequest(r *http.Request) (*OAuth2Config, error) {
v := mux.Vars(r)
session, _ := store.Get(r, getSessionName(r))
rid := getRequestID(r)
var aid string
if v, ok := session.Values["ID"]; ok {
aid = v.(string)
}
if aid == "" && v["ID"] != "" {
aid = v["ID"]
}
l := log.WithFields(log.Fields{
"action": "configFromRequest",
"request_id": rid,
"app_id": aid,
})
l.Println("configFromRequest")
var config *OAuth2Config
if v["ID"] != "" && rid == "" {
l.Printf("getAppByAppID path id=%v", v["ID"])
// user defined app id and no existing request id
l.Printf("ClientID=%s", v["ID"])
var e error
config, e = getAppByAppID(v["ID"])
if e != nil {
l.Printf("getAppByAppID path id=%v error=%v", v["ID"], e)
return config, e
}
l.Printf("appID found %+v\n", logConfig(config))
} else if rid != "" {
l.Printf("getAppByAppID request id=%v", rid)
// request id already defined
var e error
config, e = getAppByAppID(aid)
if e != nil {
l.Printf("getAppByAppID request id=%v error=%v", rid, e)
return config, e
}
l.Printf("appID with request_id found %+v\n", logConfig(config))
} else {
config = cfgs[0]
l.Printf("default appID %+v\n", logConfig(config))
}
return config, nil
}
// readCfgs reads the OAuth2 application configuration file
// and parses into object in memory
func readCfgs(f string) error {
l := log.WithFields(log.Fields{
"action": "readCfgs",
})
l.Printf("readCfgs")
bd, berr := os.ReadFile(f)
if berr != nil {
l.Printf("ReadFile error=%v", berr)
return berr
}
bd = []byte(os.ExpandEnv(string(bd)))
type cfg struct {
Configs []*OAuth2Config `json:"configs"`
}
var c cfg
jerr := json.Unmarshal(bd, &c)
if jerr != nil {
l.Printf("json.Unmarshal error=%v", jerr)
return jerr
}
cfgs = c.Configs
if len(cfgs) < 1 {
l.Printf("default oauth2 provider required")
return errors.New("default oauth2 provider required")
}
l.Printf("%v configs loaded", len(cfgs))
return nil
}
// redisStore instantiates a redis session store
func redisStore() {
s, err := redistore.NewRediStore(100, "tcp", os.Getenv("SESSION_STORE_REDIS"), "", []byte(os.Getenv("SESSION_KEY")), nil)
if err != nil {
log.Fatal(err)
}
store = s
}
// fsStore instantiates a filesystem session store
func fsStore() {
f := sessions.NewFilesystemStore("", []byte(os.Getenv("SESSION_KEY")), nil)
f.MaxLength(0)
store = f
}
// cookieStore instantiates a cookie session store
func cookieStore() {
store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
}
func HandleUnauthorized(w http.ResponseWriter, r *http.Request) {
rid := r.FormValue("request_id")
ruri := r.FormValue("redirect")
l := log.WithFields(log.Fields{
"action": "HandleUnauthorized",
"request_id": rid,
"redirect_uri": ruri,
})
l.Print("HandleUnauthorized")
lp := filepath.Join("web", "403.html")
tmpl, _ := template.ParseFiles(lp)
data := make(map[string]string)
if rid != "" {
data["request_id"] = rid
}
if ruri != "" {
data["redirect_uri"] = ruri
}
tmpl.ExecuteTemplate(w, "403", data)
}
func init() {
// parse configuration
cerr := readCfgs(os.Getenv("OAUTH2_CONFIG_FILE"))
if cerr != nil {
log.Fatal(cerr)
}
// configure session storage driver
switch os.Getenv("SESSION_STORE_TYPE") {
case "redis":
redisStore()
case "filesystem":
fsStore()
case "cookie":
cookieStore()
default:
cookieStore()
}
gob.Register(&oauth2.Token{})
// Create ssokey from the first 32 bytes from SESSION_KEY
if len(os.Getenv("SESSION_KEY")) < 32 {
log.Fatal("SESSION_KEY must be 32 bytes or larger")
}
ssokey = []byte(os.Getenv("SESSION_KEY")[:32])
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", LoginHandler)
r.HandleFunc("/refresh/{ID}", RefreshHandler)
r.HandleFunc("/oauth2/{ID}", LoginHandler)
r.HandleFunc("/logout/{ID}", LogoutHandler)
r.HandleFunc("/callback", CallbackHandler)
r.HandleFunc("/callback/{ID}", CallbackHandler)
r.HandleFunc("/403", HandleUnauthorized)
r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
})
c := cors.New(cors.Options{
AllowOriginFunc: func(origin string) bool {
return true
},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
Debug: false,
})
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), c.Handler(r)))
}