forked from mdklein/candid
/
openid-connect.go
376 lines (339 loc) · 11.9 KB
/
openid-connect.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package openid provides identity providers that use OpenID to
// determine the identity.
package openid
import (
"fmt"
"net/http"
"time"
"github.com/coreos/go-oidc"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"gopkg.in/CanonicalLtd/candidclient.v1/params"
"gopkg.in/errgo.v1"
"gopkg.in/juju/names.v2"
"gopkg.in/macaroon-bakery.v2/httpbakery"
"github.com/CanonicalLtd/candid/idp"
"github.com/CanonicalLtd/candid/idp/idputil"
"github.com/CanonicalLtd/candid/idp/idputil/secret"
"github.com/CanonicalLtd/candid/store"
)
func init() {
idp.Register("openid-connect", func(unmarshal func(interface{}) error) (idp.IdentityProvider, error) {
var p OpenIDConnectParams
if err := unmarshal(&p); err != nil {
return nil, errgo.Notef(err, "cannot unmarshal openid-connect parameters")
}
if p.Name == "" {
return nil, errgo.Newf("name not specified")
}
if p.Issuer == "" {
return nil, errgo.Newf("issuer not specified")
}
if p.ClientID == "" {
return nil, errgo.Newf("client-id not specified")
}
if p.ClientSecret == "" {
return nil, errgo.Newf("client-secret not specified")
}
return NewOpenIDConnectIdentityProvider(p), nil
})
}
type OpenIDConnectParams struct {
// Name is the name that will be given to the identity provider.
Name string `yaml:"name"`
// Description is the description that will be used with the
// identity provider. If this is not set then Name will be used.
Description string `yaml:"description"`
// Domain is the domain with which all identities created by this
// identity provider will be tagged (not including the @ separator).
Domain string `yaml:"domain"`
// Issuer is the OpenID connect issuer for the identity provider.
// Discovery will be performed for this issuer.
Issuer string `yaml:"issuer"`
// Scopes contains the OAuth scopes to request.
Scopes []string `yaml:"scopes"`
// ClientID is the ID of the client as registered with the issuer.
ClientID string `yaml:"client-id"`
// ClientSecret is a client specific secret agreed with the issuer.
ClientSecret string `yaml:"client-secret"`
}
// NewOpenIDConnectIdentityProvider creates a new identity provider using
// OpenID connect.
func NewOpenIDConnectIdentityProvider(params OpenIDConnectParams) idp.IdentityProvider {
if params.Description == "" {
params.Description = params.Name
}
if len(params.Scopes) == 0 {
params.Scopes = []string{oidc.ScopeOpenID}
}
return &openidConnectIdentityProvider{
params: params,
}
}
type openidConnectIdentityProvider struct {
params OpenIDConnectParams
initParams idp.InitParams
provider *oidc.Provider
config *oauth2.Config
codec *secret.Codec
}
// Name implements idp.IdentityProvider.Name.
func (idp *openidConnectIdentityProvider) Name() string {
return idp.params.Name
}
// Domain implements idp.IdentityProvider.Domain.
func (idp *openidConnectIdentityProvider) Domain() string {
return idp.params.Domain
}
// Description implements idp.IdentityProvider.Description.
func (idp *openidConnectIdentityProvider) Description() string {
return idp.params.Description
}
// Interactive implements idp.IdentityProvider.Interactive.
func (*openidConnectIdentityProvider) Interactive() bool {
return true
}
// Init implements idp.IdentityProvider.Init by performing discovery on
// the issuer and set up the identity provider.
func (idp *openidConnectIdentityProvider) Init(ctx context.Context, params idp.InitParams) error {
idp.initParams = params
var err error
idp.provider, err = oidc.NewProvider(ctx, idp.params.Issuer)
if err != nil {
return errgo.Mask(err)
}
idp.config = &oauth2.Config{
ClientID: idp.params.ClientID,
ClientSecret: idp.params.ClientSecret,
Endpoint: idp.provider.Endpoint(),
RedirectURL: idp.initParams.URLPrefix + "/callback",
Scopes: idp.params.Scopes,
}
idp.codec = secret.NewCodec(idp.initParams.Key)
return nil
}
// URL implements idp.IdentityProvider.URL.
func (idp *openidConnectIdentityProvider) URL(dischargeID string) string {
return idputil.URL(idp.initParams.URLPrefix, "/login", dischargeID)
}
// SetInteraction implements idp.IdentityProvider.SetInteraction.
func (idp *openidConnectIdentityProvider) SetInteraction(ierr *httpbakery.Error, dischargeID string) {
}
// GetGroups implements idp.IdentityProvider.GetGroups.
func (*openidConnectIdentityProvider) GetGroups(context.Context, *store.Identity) ([]string, error) {
return nil, nil
}
// Handle implements idp.IdentityProvider.Handle.
func (idp *openidConnectIdentityProvider) Handle(ctx context.Context, w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/callback":
if dischargeID, err := idp.callback(ctx, w, req); err != nil {
idp.initParams.VisitCompleter.Failure(ctx, w, req, dischargeID, err)
}
case "/register":
if dischargeID, err := idp.register(ctx, w, req); err != nil {
idp.initParams.VisitCompleter.Failure(ctx, w, req, dischargeID, err)
}
default:
if err := idp.login(ctx, w, req); err != nil {
idp.initParams.VisitCompleter.Failure(ctx, w, req, idputil.DischargeID(req), err)
}
}
}
func (idp *openidConnectIdentityProvider) login(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
dischargeID := idputil.DischargeID(req)
err := idp.newSession(ctx, w, dischargeID)
if err != nil {
return errgo.Mask(err)
}
url := idp.config.AuthCodeURL(dischargeID)
http.Redirect(w, req, url, http.StatusFound)
return nil
}
func (idp *openidConnectIdentityProvider) callback(ctx context.Context, w http.ResponseWriter, req *http.Request) (string, error) {
dischargeID, err := idp.getSession(ctx, req)
if err != nil {
return dischargeID, errgo.WithCausef(err, params.ErrBadRequest, "")
}
if dischargeID != req.Form.Get("state") {
return dischargeID, errgo.WithCausef(nil, params.ErrBadRequest, "invalid session")
}
tok, err := idp.config.Exchange(ctx, req.Form.Get("code"))
if err != nil {
return dischargeID, errgo.Mask(err)
}
idtok := tok.Extra("id_token")
if idtok == nil {
return dischargeID, errgo.Newf("no id_token in OpenID response")
}
idtoks, ok := idtok.(string)
if !ok {
return dischargeID, errgo.Newf("invalid id_token in OpenID response")
}
id, err := idp.provider.Verifier(&oidc.Config{ClientID: idp.config.ClientID}).Verify(ctx, idtoks)
if err != nil {
return dischargeID, errgo.Mask(err)
}
user := store.Identity{
ProviderID: store.MakeProviderIdentity(idp.Name(), fmt.Sprintf("%s:%s", id.Issuer, id.Subject)),
}
err = idp.initParams.Store.Identity(ctx, &user)
if err == nil {
idp.deleteSession(ctx, w)
idp.initParams.VisitCompleter.Success(ctx, w, req, dischargeID, &user)
return "", nil
}
if errgo.Cause(err) != store.ErrNotFound {
return dischargeID, errgo.Mask(err)
}
var claims claims
if err := id.Claims(&claims); err != nil {
return dischargeID, errgo.Mask(err)
}
state, err := idp.codec.Encode(registrationState{
WaitID: dischargeID,
ProviderID: user.ProviderID,
})
preferredUsername := ""
if names.IsValidUserName(claims.PreferredUsername) {
preferredUsername = claims.PreferredUsername
}
return dischargeID, errgo.Mask(idputil.RegistrationForm(ctx, w, idputil.RegistrationParams{
State: state,
Username: preferredUsername,
Domain: idp.params.Domain,
FullName: claims.FullName,
Email: claims.Email,
}, idp.initParams.Template))
}
func (idp *openidConnectIdentityProvider) register(ctx context.Context, w http.ResponseWriter, req *http.Request) (string, error) {
dischargeID, err := idp.getSession(ctx, req)
if err != nil {
return dischargeID, errgo.WithCausef(err, params.ErrBadRequest, "")
}
var state registrationState
if err := idp.codec.Decode(req.Form.Get("state"), &state); err != nil {
return dischargeID, errgo.WithCausef(err, params.ErrBadRequest, "invalid registration state")
}
if state.WaitID != dischargeID {
return dischargeID, errgo.WithCausef(err, params.ErrBadRequest, "invalid registration state")
}
u := &store.Identity{
ProviderID: state.ProviderID,
Name: req.Form.Get("fullname"),
Email: req.Form.Get("email"),
}
err = idp.registerUser(ctx, req.Form.Get("username"), u)
if err == nil {
idp.deleteSession(ctx, w)
idp.initParams.VisitCompleter.Success(ctx, w, req, dischargeID, u)
return dischargeID, nil
}
if errgo.Cause(err) != errInvalidUser {
return dischargeID, errgo.Mask(err)
}
return dischargeID, errgo.Mask(idputil.RegistrationForm(ctx, w, idputil.RegistrationParams{
State: req.Form.Get("state"),
Error: err.Error(),
Username: req.Form.Get("username"),
Domain: idp.params.Domain,
FullName: req.Form.Get("fullname"),
Email: req.Form.Get("email"),
}, idp.initParams.Template))
}
var errInvalidUser = errgo.New("invalid user")
func (idp *openidConnectIdentityProvider) registerUser(ctx context.Context, username string, u *store.Identity) error {
if !names.IsValidUserName(username) {
return errgo.WithCausef(nil, errInvalidUser, "invalid user name. The username must contain only A-Z, a-z, 0-9, '.', '-', & '+', and must start and end with a letter or number.")
}
if idputil.ReservedUsernames[username] {
return errgo.WithCausef(nil, errInvalidUser, "username %s is not allowed, please choose another.", username)
}
u.Username = joinDomain(username, idp.params.Domain)
err := idp.initParams.Store.UpdateIdentity(ctx, u, store.Update{
store.Username: store.Set,
store.Name: store.Set,
store.Email: store.Set,
})
if err == nil {
return nil
}
if errgo.Cause(err) != store.ErrDuplicateUsername {
return errgo.Mask(err)
}
return errgo.WithCausef(nil, errInvalidUser, "Username already taken, please pick a different one.")
}
// newSession stores the state data for this login session in an
// encrypted session cookie.
func (idp *openidConnectIdentityProvider) newSession(ctx context.Context, w http.ResponseWriter, dischargeID string) error {
sessionCookie := sessionCookie{
WaitID: dischargeID,
Expires: time.Now().Add(15 * time.Minute),
}
value, err := idp.codec.Encode(sessionCookie)
if err != nil {
return errgo.Mask(err)
}
http.SetCookie(w, &http.Cookie{
Name: idp.sessionCookieName(),
Value: value,
})
return nil
}
// getSession retrieves and validats the current session cookie for the
// login session and returns the associated dischargeID.
func (idp *openidConnectIdentityProvider) getSession(ctx context.Context, req *http.Request) (string, error) {
c, err := req.Cookie(idp.sessionCookieName())
if err == http.ErrNoCookie {
return "", errgo.Notef(err, "no login session")
}
if err != nil {
return "", err
}
var sessionCookie sessionCookie
if err = idp.codec.Decode(c.Value, &sessionCookie); err != nil {
return "", errgo.Notef(err, "invalid session")
}
if sessionCookie.Expires.Before(time.Now()) {
return "", errgo.New("expired session")
}
return sessionCookie.WaitID, nil
}
// deleteSession removes the session cookie for the current login
// session.
func (idp *openidConnectIdentityProvider) deleteSession(ctx context.Context, w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: idp.sessionCookieName(),
})
}
func (idp *openidConnectIdentityProvider) sessionCookieName() string {
return "idp-login-" + idp.params.Name
}
// sessionCookie contains the stored state for the OpenID login process.
type sessionCookie struct {
WaitID string `json:"wid"`
Expires time.Time `json:"exp"`
}
// claims contains the set of claims possibly returned in the OpenID
// token.
type claims struct {
FullName string `json:"name"`
Email string `json:"email"`
PreferredUsername string `json:"preferred_username"`
}
// joinDomain creates a new params.Username with the given name and
// (optional) domain.
func joinDomain(name, domain string) string {
if domain == "" {
return name
}
return fmt.Sprintf("%s@%s", name, domain)
}
// registrationState holds state information about a registration that is
// in progress.
type registrationState struct {
WaitID string `json:"wid"`
ProviderID store.ProviderIdentity `json:"pid"`
}