-
Notifications
You must be signed in to change notification settings - Fork 3
/
oidc.go
217 lines (160 loc) · 4.35 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
package oidc
import (
"github.com/pkg/errors"
"net/http"
"time"
"context"
"encoding/json"
"log"
"os"
"strings"
"github.com/coreos/go-oidc"
"github.com/gorilla/securecookie"
"golang.org/x/oauth2"
)
// ResponseMode ..
type ResponseMode = string
const (
// ResponseModeFormPost ...
ResponseModeFormPost ResponseMode = "form_post"
// ResponseModeQuery ..
ResponseModeQuery ResponseMode = "query"
)
// Options is the configuration required for running the oidc server
type Options struct {
Issuer string
CookieOptions CookieOptions
Prefix string
SignInPath string
SignOutPath string
SignInCallbackPath string
SignOutCallbackPath string
PostSignInRedirect string
PostSignOutRedirect string
ResponseType string
ResponseMode ResponseMode
PostSignInRedirectHandler http.Handler
PostSignOutRedirectHandler http.Handler
Provider oidc.Provider
Config oauth2.Config
NotFoundHandler http.Handler
Client http.Client
ErrorLogger *log.Logger
TempCodec securecookie.Codec
RedirectionMaxAge int
IDTokenVerifier *oidc.IDTokenVerifier
LogoutURI string
AuthHandler AuthHandler
}
// CookieOptions is the various cookie options that are configurable for the identity cookie
type CookieOptions struct {
Name string // Default: oidc
Expires time.Time
MaxAge int
Domain string
Path string
SameSite http.SameSite
}
// Option is the type used to modify the default Options
type Option func(*Options)
// DefaultOptions ...
func DefaultOptions() Options {
return Options{
Prefix: "/oidc",
SignInPath: "/oidc/sign-in",
SignOutPath: "/oidc/sign-out",
PostSignOutRedirect: "/",
PostSignInRedirect: "/",
SignInCallbackPath: "/oidc/sign-in-oidc",
SignOutCallbackPath: "/oidc/sign-out-oidc",
TempCodec: securecookie.New(
[]byte("development-credentials-hash----"),
nil,
),
RedirectionMaxAge: 25 * 60,
ResponseMode: ResponseModeFormPost,
ResponseType: "code id_token",
CookieOptions: DefaultCookieOptions(),
Config: oauth2.Config{
Scopes: []string{oidc.ScopeOpenID, "profile"},
},
NotFoundHandler: http.NotFoundHandler(),
Client: http.Client{
Timeout: 10 * time.Second,
},
ErrorLogger: log.New(os.Stderr, "oidc: ", 0),
}
}
func DefaultCookieOptions() CookieOptions {
return CookieOptions{
Name: "oidc",
Path: "/",
SameSite: http.SameSiteNoneMode,
}
}
// OpenIDConnect ...
func OpenIDConnect(iss, clientID, clientSecret string, opts ...Option) (func(http.Handler) http.Handler, error) {
o := DefaultOptions()
o.Issuer = iss
o.Config.ClientID = clientID
o.Config.ClientSecret = clientSecret
for _, f := range opts {
f(&o)
}
if err := prepareOptions(&o); err != nil {
return nil, err
}
h := handlerFromOptions(&o)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(r.RequestURI) >= len(o.Prefix) && r.RequestURI[:len(o.Prefix)] == o.Prefix {
h.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
}, nil
}
func Must(h func(http.Handler) http.Handler, err error) func(http.Handler) http.Handler {
if err != nil {
panic(err)
}
return h
}
func prepareOptions(o *Options) error {
provider, err := oidc.NewProvider(context.Background(), o.Issuer)
if err != nil {
return errors.Wrap(err, "Provider error")
}
o.Config.Endpoint = provider.Endpoint()
o.IDTokenVerifier = provider.Verifier(&oidc.Config{ClientID: o.Config.ClientID})
if o.AuthHandler == nil {
o.AuthHandler = o
}
if o.PostSignInRedirectHandler == nil {
o.PostSignInRedirectHandler = http.RedirectHandler(o.PostSignInRedirect, http.StatusFound)
}
if o.PostSignOutRedirectHandler == nil {
o.PostSignOutRedirectHandler = http.RedirectHandler(o.PostSignOutRedirect, http.StatusFound)
}
if o.RedirectionMaxAge < 0 {
o.RedirectionMaxAge = 0
}
if s, ok := o.TempCodec.(*securecookie.SecureCookie); ok {
s.MaxAge(o.RedirectionMaxAge)
}
discoveryURI := strings.TrimSuffix(o.Issuer, "/") + "/.well-known/openid-configuration"
res, err := o.Client.Get(discoveryURI)
if err != nil {
return errors.Wrap(err, "Discovery Error")
}
defer res.Body.Close()
var discoResp struct {
EndSessionURI string `json:"end_session_endpoint"`
}
if err := json.NewDecoder(res.Body).Decode(&discoResp); err != nil {
return errors.Wrap(err, "Decode Error")
}
o.LogoutURI = discoResp.EndSessionURI
return nil
}