/
handle_oauth2.go
574 lines (470 loc) · 15.5 KB
/
handle_oauth2.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
package handlers
import (
"context"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/go-chi/jwtauth"
oauth2errors "github.com/go-oauth2/oauth2/v4/errors"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
"github.com/spf13/cast"
"github.com/cortezaproject/corteza/server/auth/request"
"github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/errors"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/payload"
systemService "github.com/cortezaproject/corteza/server/system/service"
"github.com/cortezaproject/corteza/server/system/types"
oauth2def "github.com/go-oauth2/oauth2/v4"
"go.uber.org/zap"
)
// oauth2 flow authorize step
//
// OA2 server internals first run user check (see SetUserAuthorizationHandler lambda)
// to ensure user is authenticated;
func (h AuthHandlers) oauth2Authorize(req *request.AuthReq) (err error) {
if form := request.GetOAuth2AuthParams(req.Session); form != nil {
req.Request.Form = form
h.Log.Debug("restarting oauth2 authorization flow", zap.Any("params", req.Request.Form))
} else {
h.Log.Debug("starting new oauth2 authorization flow", zap.Any("params", req.Request.Form))
}
request.SetOauth2AuthParams(req.Session, nil)
var (
client *types.AuthClient
)
if client, err = h.loadRequestedClient(req); err != nil {
return err
}
if client != nil {
// No client validation is done at this point;
// first, see if user is able to authenticate.
request.SetOauth2Client(req.Session, client)
// ensure we're dealing with client ID in case someone used handle
req.Request.Form.Set("client_id", strconv.FormatUint(client.ID, 10))
}
// set to -1 to make sure wrapping request handler
// does not send status code!
req.Status = -1
err = h.OAuth2.HandleAuthorizeRequest(req.Response, req.Request)
if err != nil {
req.Status = http.StatusInternalServerError
req.Template = TmplInternalError
req.Data["error"] = err
}
return nil
}
func (h AuthHandlers) oauth2AuthorizeClient(req *request.AuthReq) (err error) {
var (
client = request.GetOauth2Client(req.Session)
)
if client == nil {
return fmt.Errorf("flow broken; client missing")
}
if err = client.Verify(); err != nil {
return
}
if !h.canAuthorizeClient(req.Context(), req.Client) {
h.Log.Error("user's roles do not allow authorization of this client", logger.Uint64("ID", req.Client.ID), zap.String("handle", req.Client.Handle))
request.SetOauth2Client(req.Session, nil)
request.SetOauth2AuthParams(req.Session, nil)
req.RedirectTo = GetLinks().Profile
t := translator(req, "auth")
req.NewAlerts = append(req.NewAlerts, request.Alert{
Type: "danger",
Text: t("oauth2-authorize-client.alerts.denied", "client", req.Client.Meta.Name),
})
return nil
}
if !req.AuthUser.User.EmailConfirmed {
req.Data["invalidUser"] = true
req.Data["disabled"] = true
} else if client.Trusted {
h.Log.Debug("pre-authorized trusted oauth2 client")
// Client is trusted, no need to show this screen
// move forward and authorize oauth2 request
request.SetOauth2ClientAuthorized(req.Session, true)
req.RedirectTo = GetLinks().OAuth2Authorize
return nil
}
h.Log.Debug("showing oauth2 client auth form")
req.Template = TmplOAuth2AuthorizeClient
return nil
}
func (h AuthHandlers) oauth2AuthorizeClientProc(req *request.AuthReq) (err error) {
// permissions are already check ed in the oauth2AuthorizeClient fn,
// just making sure
if h.canAuthorizeClient(req.Context(), req.Client) {
request.SetOauth2Client(req.Session, nil)
if _, allow := req.Request.Form["allow"]; allow {
request.SetOauth2ClientAuthorized(req.Session, true)
req.RedirectTo = GetLinks().OAuth2Authorize
return
}
}
// handle deny client action from authorize-client form
//
// This occurs when user pressed "DENY" button on authorize-client form
// Remove all and redirect to profile
//
request.SetOauth2AuthParams(req.Session, nil)
req.RedirectTo = GetLinks().Profile
t := translator(req, "auth")
req.NewAlerts = append(req.NewAlerts, request.Alert{
Type: "warning",
Text: t("oauth2-authorize-client.alerts.denied", "client", req.Client.Meta.Name),
})
return
}
// Verifies weather current user can authorize this client or not
func (h AuthHandlers) canAuthorizeClient(ctx context.Context, c *types.AuthClient) bool {
return systemService.DefaultAccessControl.CanAuthorizeAuthClient(ctx, c)
}
func (h AuthHandlers) oauth2Token(req *request.AuthReq) (err error) {
// Cleanup
request.SetOauth2ClientAuthorized(req.Session, false)
req.Status = -1
client, err := h.loadRequestedClient(req)
if err != nil {
return h.tokenError(req.Response, err)
}
return h.handleTokenRequest(req, client)
}
// oauth2Info handler validates token and responds with decoded claims
func (h AuthHandlers) oauth2Info(w http.ResponseWriter, r *http.Request) {
var (
jt jwt.Token
claims map[string]interface{}
)
err := func() (err error) {
if jt, claims, err = jwtauth.FromContext(r.Context()); err != nil {
return
}
if err = auth.TokenIssuer.Validate(r.Context(), jt); err != nil {
return
}
return nil
}()
if err != nil {
if errors.Is(err, context.Canceled) {
// Gracefully handle request cancellation
//
// This happens during the login procedure when browser is
// sent through a couple of quick redirects that can
// terminate requests
w.WriteHeader(http.StatusNoContent)
return
}
var (
data = make(map[string]interface{})
code int
header http.Header
)
if errors.Is(err, oauth2errors.ErrInvalidAccessToken) {
code = http.StatusForbidden
data["error"] = err.Error()
} else {
data, code, header = h.OAuth2.GetErrorData(err)
}
_ = header.Write(w)
w.WriteHeader(code)
data["active"] = false
_ = json.NewEncoder(w).Encode(data)
return
}
_ = json.NewEncoder(w).Encode(claims)
}
// oauth2authorizeDefaultClient acts as a proxy for default client
//
// Responsibilities:
// - handles parameterless request to initialize authorization code flow
// - accepts redirect_uri via query string that's used to build the oauth2 authorization URL
//
// (for the rest of the flow, see oauth2authorizeDefaultClientProc)
func (h AuthHandlers) oauth2authorizeDefaultClient(req *request.AuthReq) (err error) {
if err = h.verifyDefaultClient(); err != nil {
return
}
var (
params = url.Values{}
)
params.Set("client_id", fmt.Sprintf("%d", h.DefaultClient.ID))
params.Set("response_type", "code")
params.Set("response_mode", "query")
if redir, has := req.Request.Form["redirect_uri"]; has && len(redir) > 0 {
params.Set("redirect_uri", redir[0])
}
if scope, has := req.Request.Form["scope"]; has && len(scope) > 0 {
params.Set("scope", scope[0])
}
if state, has := req.Request.Form["state"]; has && len(state) > 0 {
params.Set("state", state[0])
}
req.RedirectTo = GetLinks().OAuth2Authorize + "?" + params.Encode()
return
}
// oauth2authorizeDefaultClient acts as a proxy for default client
//
// Responsibilities:
// - handles exchange of authorization-code for token
// - handles issuing of new access token requests
//
// (for the first part of the flow, see oauth2authorizeDefaultClient)
func (h AuthHandlers) oauth2authorizeDefaultClientProc(req *request.AuthReq) (err error) {
if err = h.verifyDefaultClient(); err != nil {
return
}
var (
// Clone of the initial request
// that we'll use for token request validation
r = req.Request.Clone(req.Context())
)
if _, has := r.Form["code"]; has {
r.Form.Set("grant_type", oauth2def.AuthorizationCode.String())
} else if _, has := r.Form["refresh_token"]; has {
r.Form.Set("grant_type", oauth2def.Refreshing.String())
} else {
// make sure we do not get surprised, remove grant_type param
// and let oauth2 internals invalidate the request
r.Form.Del("grant_type")
}
// Add id and secret from the default client
r.SetBasicAuth(
strconv.FormatUint(h.DefaultClient.ID, 10),
h.DefaultClient.Secret,
)
req.Request = r
return h.handleTokenRequest(req, h.DefaultClient)
}
func (h AuthHandlers) verifyDefaultClient() error {
if h.DefaultClient == nil {
return fmt.Errorf("no default client configured")
}
if err := h.DefaultClient.Verify(); err != nil {
return fmt.Errorf("invalid client: %w", err)
}
return nil
}
// loads client from the request params and verifies other request params against client settings
func (h AuthHandlers) loadRequestedClient(req *request.AuthReq) (client *types.AuthClient, err error) {
return client, func() (err error) {
var (
id string
found bool
)
if id, _, found = req.Request.BasicAuth(); !found {
if _, found = req.Request.Form["client_id"]; !found {
return
} else {
id = req.Request.Form.Get("client_id")
}
}
client, err = h.ClientService.Lookup(req.Context(), id)
if err != nil {
return fmt.Errorf("invalid client: %w", err)
}
h.Log.Debug("client loaded from store", logger.Uint64("ID", client.ID))
return
}()
}
func (h AuthHandlers) handleTokenRequest(req *request.AuthReq, client *types.AuthClient) error {
var (
r = req.Request
w = req.Response
ctx = req.Context()
user *types.User
)
req.Status = -1
if err := client.Verify(); err != nil {
return h.tokenError(w, fmt.Errorf("invalid client: %w", err))
}
r = req.Request.Clone(ctx)
gt, tgr, err := h.OAuth2.ValidationTokenRequest(r)
if err != nil {
return h.tokenError(w, err)
}
// Manually set the impersonating user & roles when client credentials
if gt == oauth2def.ClientCredentials {
tgr.UserID = strings.Join(
append([]string{strconv.FormatUint(client.Security.ImpersonateUser, 10)}, client.Security.ForcedRoles...),
" ",
)
}
ti, err := h.OAuth2.GetAccessToken(ctx, gt, tgr)
if err != nil {
return h.tokenError(w, err)
}
suCtx := auth.SetIdentityToContext(ctx, auth.ServiceUser())
switch gt {
case oauth2def.ClientCredentials:
// Authenticated with client credentials!
// First, validate client's security settings
if client.Security == nil || client.Security.ImpersonateUser == 0 {
return h.tokenError(w, errors.Internal("auth client security configuration invalid"))
}
// Load the user
if user, err = h.UserService.FindByAny(suCtx, client.Security.ImpersonateUser); err != nil {
return h.tokenError(w, fmt.Errorf("could not generate token for impersonated user: %v", err))
}
case oauth2def.AuthorizationCode, oauth2def.Refreshing:
userID := ti.GetUserID()
if i := strings.Index(ti.GetUserID(), " "); i > 0 {
// userID field from the token could contain encoded roles
// @todo investigate if role-encoding into user-id field is still needed?
userID = userID[:i]
}
sessionUserExists := req.AuthUser != nil && req.AuthUser.User != nil
user, err = h.UserService.FindByAny(suCtx, userID)
if err != nil {
if !errors.Is(err, systemService.UserErrNotFound()) {
return h.tokenError(w, fmt.Errorf("could not generate token: %v", err))
}
if errors.Is(err, systemService.UserErrNotFound()) && sessionUserExists {
user = req.AuthUser.User
}
}
if sessionUserExists && req.AuthUser.User.ID == cast.ToUint64(userID) {
req.AuthUser.User = user
req.AuthUser.Save(req.Session)
}
default:
return fmt.Errorf("unsupported oauth2 grant type: %v", gt)
}
var (
signed []byte
scope = strings.Split(ti.GetScope(), " ")
)
// Here set roles to signed
signed, err = auth.TokenIssuer.Sign(
auth.WithAccessToken(ti.GetAccess()),
auth.WithIdentity(user),
func(tr *auth.TokenRequest) error {
// Calculate user's roles
roles := user.Roles()
if client.Security != nil {
roles = auth.ApplyRoleSecurity(
payload.ParseUint64s(client.Security.PermittedRoles),
payload.ParseUint64s(client.Security.ProhibitedRoles),
payload.ParseUint64s(client.Security.ForcedRoles),
roles...,
)
}
tr.Roles = roles
return nil
},
auth.WithClientID(client.ID),
auth.WithScope(scope...),
)
if err != nil {
return h.tokenError(w, err)
}
// modify token info with signed JWT
// this will be sent back to the user
ti.SetAccess(string(signed))
response := h.OAuth2.GetTokenData(ti)
// include user's avatarID
if user.Meta.AvatarID != 0 {
response["avatarID"] = strconv.FormatUint(user.Meta.AvatarID, 10)
}
//include user's theme
response["theme"] = user.Meta.Theme
// in case client is configured with "openid" scope,
// we'll add "id_token" with all required (by OIDC) details encoded
if strings.Contains(client.Scope, "openid") {
var idToken []byte
if idToken, err = generateIdToken(user, client, ti, h.Opt.BaseURL); err != nil {
return h.tokenError(w, err)
}
response["id_token"] = string(idToken)
}
return writeResponse(w, response, nil)
}
func (h AuthHandlers) tokenError(w http.ResponseWriter, err error) error {
data, statusCode, header := h.OAuth2.GetErrorData(err)
return writeResponse(w, data, header, statusCode)
}
func (h AuthHandlers) oauth2PublicKeys(w http.ResponseWriter, r *http.Request) {
// handle error
handleErr := func(code int, err error) {
var (
data = make(map[string]interface{})
header http.Header
)
data["error"] = err.Error()
_ = header.Write(w)
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(data)
}
// @todo determine weather to get it form user and save it in settings
raw, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
handleErr(http.StatusBadRequest, fmt.Errorf("failed to generate new RSA privatre key: %w", err))
return
}
key, err := jwk.New(raw)
if err != nil {
handleErr(http.StatusInternalServerError, fmt.Errorf("failed to create symmetric key: %w", err))
return
}
publicKey, err := key.PublicKey()
if err != nil {
handleErr(http.StatusInternalServerError, fmt.Errorf("failed to create public key: %w", err))
return
}
_ = json.NewEncoder(w).Encode(publicKey)
}
func SubSplit(ti oauth2def.TokenInfo, data map[string]interface{}) {
userIdWithRoles := strings.SplitN(ti.GetUserID(), " ", 2)
data["sub"] = userIdWithRoles[0]
if len(userIdWithRoles) > 1 {
data["roles"] = strings.Split(userIdWithRoles[1], " ")
}
}
// Generates ID token that is part of OIDC flow for doing corteza-to-corteza auth
func generateIdToken(user *types.User, client *types.AuthClient, ti oauth2def.TokenInfo, baseURL string) (_ []byte, err error) {
token := jwt.New()
if err = token.Set(jwt.IssuerKey, baseURL); err != nil {
return
}
// we do not know what the admin used for client key value
// on the receiving end, so we'll encode both,
// client's ID, and it's handle
aud := []string{strconv.FormatUint(client.ID, 10)}
if len(client.Handle) > 0 {
aud = append(aud, client.Handle)
}
if err = token.Set("aud", aud); err != nil {
return
}
if err = token.Set("user_id", strconv.FormatUint(user.ID, 10)); err != nil {
return
}
if err = token.Set("email", user.Email); err != nil {
return
}
if err = token.Set(jwt.ExpirationKey, now().Add(ti.GetAccessExpiresIn()).Unix()); err != nil {
return
}
return jwt.Sign(token, jwa.HS512, []byte(client.Secret))
}
func writeResponse(w http.ResponseWriter, data map[string]interface{}, header http.Header, statusCode ...int) error {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
for key := range header {
w.Header().Set(key, header.Get(key))
}
status := http.StatusOK
if len(statusCode) > 0 && statusCode[0] > 0 {
status = statusCode[0]
}
w.WriteHeader(status)
return json.NewEncoder(w).Encode(data)
}