-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_mux.go
141 lines (108 loc) · 3.53 KB
/
serve_mux.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
// Copyright (C) The AetherFS Authors - All Rights Reserved
// See LICENSE for more information.
package oidcauth
import (
"context"
"net/http"
"strings"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"go.uber.org/zap"
"golang.org/x/oauth2"
"github.com/mjpitz/myago/lazy"
"github.com/mjpitz/myago/ulid"
"github.com/mjpitz/myago/zaputil"
)
// TokenCallback is invoked by the OIDCServeMux endpoint when we've successfully received and validated the
// authenticated user session.
type TokenCallback func(token *oauth2.Token)
// ServeMux is some rough code that should allow a command line tool to receive a token and invoke the provided
// callback function when a successful exchange is performed.
func ServeMux(cfg Config, callback TokenCallback) *http.ServeMux {
providerOnce := &lazy.Once{
Loader: cfg.Issuer.Provider,
}
configOnce := &lazy.Once{
Loader: func(ctx context.Context) (*oauth2.Config, error) {
instance, err := providerOnce.Get(ctx)
if err != nil {
return nil, err
}
return &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Endpoint: instance.(*oidc.Provider).Endpoint(),
RedirectURL: cfg.RedirectURL,
Scopes: cfg.Scopes.Value(),
}, nil
},
}
verifierOnce := &lazy.Once{
Loader: func(ctx context.Context) (*oidc.IDTokenVerifier, error) {
provider, err := providerOnce.Get(ctx)
if err != nil {
return nil, err
}
config, err := configOnce.Get(ctx)
if err != nil {
return nil, err
}
return provider.(*oidc.Provider).Verifier(&oidc.Config{
ClientID: config.(*oauth2.Config).ClientID,
}), nil
},
}
mux := http.NewServeMux()
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
generator := ulid.Extract(ctx)
state, stateErr := generator.Generate(ctx, 128)
config, configErr := configOnce.Get(ctx)
if configErr != nil || stateErr != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
url := config.(*oauth2.Config).AuthCodeURL(state.String())
http.Redirect(w, r, url, http.StatusFound)
})
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := zaputil.Extract(ctx)
config, configErr := configOnce.Get(ctx)
verifier, verifierErr := verifierOnce.Get(ctx)
if configErr != nil || verifierErr != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
query := r.URL.Query()
errKind := query.Get("error")
errDescription := query.Get("error_description")
if errKind != "" {
http.Error(w, errKind+": "+errDescription, http.StatusBadRequest)
return
}
oauth2Token, err := config.(*oauth2.Config).Exchange(ctx, query.Get("code"))
if err != nil {
logger.Error("failed to exchange code for auth info", zap.Error(err))
http.Error(w, "", http.StatusInternalServerError)
return
}
rawIDToken := oauth2Token.Extra("id_token")
idToken, err := verifier.(*oidc.IDTokenVerifier).Verify(ctx, rawIDToken.(string))
if err != nil {
logger.Error("failed to verify id token", zap.Error(err))
http.Error(w, "", http.StatusInternalServerError)
return
}
err = idToken.VerifyAccessToken(oauth2Token.AccessToken)
if err != nil {
logger.Error("failed to verify access token token", zap.Error(err))
http.Error(w, "", http.StatusUnauthorized)
return
}
reader := strings.NewReader("You have successfully logged in. You may now close this tab in your browser.")
http.ServeContent(w, r, "", time.Now(), reader)
go callback(oauth2Token)
})
return mux
}