@@ -240,7 +240,6 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
240
240
aReq .New = database.APIKey {UserID : uuid .New ()}
241
241
return
242
242
}
243
-
244
243
aReq .New = key
245
244
246
245
http .SetCookie (rw , cookie )
@@ -276,16 +275,28 @@ type OIDCConfig struct {
276
275
// @Router /users/oidc/callback [get]
277
276
func (api * API ) userOIDC (rw http.ResponseWriter , r * http.Request ) {
278
277
var (
279
- ctx = r .Context ()
280
- state = httpmw .OAuth2 (r )
278
+ ctx = r .Context ()
279
+ state = httpmw .OAuth2 (r )
280
+ auditor = api .Auditor .Load ()
281
+ aReq , commitAudit = audit .InitRequest [database.APIKey ](rw , & audit.RequestParams {
282
+ Audit : * auditor ,
283
+ Log : api .Logger ,
284
+ Request : r ,
285
+ Action : database .AuditActionLogin ,
286
+ })
281
287
)
288
+ aReq .Old = database.APIKey {}
289
+ defer commitAudit ()
282
290
283
291
// See the example here: https://github.com/coreos/go-oidc
284
292
rawIDToken , ok := state .Token .Extra ("id_token" ).(string )
285
293
if ! ok {
286
294
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
287
295
Message : "id_token not found in response payload. Ensure your OIDC callback is configured correctly!" ,
288
296
})
297
+ // We pass a disposable user ID just to force an audit diff
298
+ // and generate a log for a failed login
299
+ aReq .New = database.APIKey {UserID : uuid .New ()}
289
300
return
290
301
}
291
302
@@ -295,6 +306,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
295
306
Message : "Failed to verify OIDC token." ,
296
307
Detail : err .Error (),
297
308
})
309
+ // We pass a disposable user ID just to force an audit diff
310
+ // and generate a log for a failed login
311
+ aReq .New = database.APIKey {UserID : uuid .New ()}
298
312
return
299
313
}
300
314
@@ -308,6 +322,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
308
322
Message : "Failed to extract OIDC claims." ,
309
323
Detail : err .Error (),
310
324
})
325
+ // We pass a disposable user ID just to force an audit diff
326
+ // and generate a log for a failed login
327
+ aReq .New = database.APIKey {UserID : uuid .New ()}
311
328
return
312
329
}
313
330
@@ -326,6 +343,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
326
343
Message : "Failed to unmarshal user info claims." ,
327
344
Detail : err .Error (),
328
345
})
346
+ // We pass a disposable user ID just to force an audit diff
347
+ // and generate a log for a failed login
348
+ aReq .New = database.APIKey {UserID : uuid .New ()}
329
349
return
330
350
}
331
351
for k , v := range userInfoClaims {
@@ -336,6 +356,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
336
356
Message : "Failed to obtain user information claims." ,
337
357
Detail : "The OIDC provider returned no claims as part of the `id_token`. The attempt to fetch claims via the UserInfo endpoint failed: " + err .Error (),
338
358
})
359
+ // We pass a disposable user ID just to force an audit diff
360
+ // and generate a log for a failed login
361
+ aReq .New = database.APIKey {UserID : uuid .New ()}
339
362
return
340
363
}
341
364
@@ -355,6 +378,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
355
378
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
356
379
Message : "No email found in OIDC payload!" ,
357
380
})
381
+ // We pass a disposable user ID just to force an audit diff
382
+ // and generate a log for a failed login
383
+ aReq .New = database.APIKey {UserID : uuid .New ()}
358
384
return
359
385
}
360
386
emailRaw = username
@@ -364,6 +390,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
364
390
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
365
391
Message : fmt .Sprintf ("Email in OIDC payload isn't a string. Got: %t" , emailRaw ),
366
392
})
393
+ // We pass a disposable user ID just to force an audit diff
394
+ // and generate a log for a failed login
395
+ aReq .New = database.APIKey {UserID : uuid .New ()}
367
396
return
368
397
}
369
398
verifiedRaw , ok := claims ["email_verified" ]
@@ -374,6 +403,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
374
403
httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
375
404
Message : fmt .Sprintf ("Verify the %q email address on your OIDC provider to authenticate!" , email ),
376
405
})
406
+ // We pass a disposable user ID just to force an audit diff
407
+ // and generate a log for a failed login
408
+ aReq .New = database.APIKey {UserID : uuid .New ()}
377
409
return
378
410
}
379
411
api .Logger .Warn (ctx , "allowing unverified oidc email %q" )
@@ -404,6 +436,9 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
404
436
httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
405
437
Message : fmt .Sprintf ("Your email %q is not in domains %q !" , email , api .OIDCConfig .EmailDomain ),
406
438
})
439
+ // We pass a disposable user ID just to force an audit diff
440
+ // and generate a log for a failed login
441
+ aReq .New = database.APIKey {UserID : uuid .New ()}
407
442
return
408
443
}
409
444
}
@@ -413,7 +448,22 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
413
448
picture , _ = pictureRaw .(string )
414
449
}
415
450
416
- cookie , _ , err := api .oauthLogin (r , oauthLoginParams {
451
+ user , link , err := findLinkedUser (ctx , api .Database , oidcLinkedID (idToken ), email )
452
+ if err != nil {
453
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
454
+ Message : "Failed to find linked user." ,
455
+ Detail : err .Error (),
456
+ })
457
+ // We pass a disposable user ID just to force an audit diff
458
+ // and generate a log for a failed login
459
+ aReq .New = database.APIKey {UserID : uuid .New ()}
460
+ return
461
+ }
462
+ aReq .UserID = user .ID
463
+
464
+ cookie , key , err := api .oauthLogin (r , oauthLoginParams {
465
+ User : user ,
466
+ Link : link ,
417
467
State : state ,
418
468
LinkedID : oidcLinkedID (idToken ),
419
469
LoginType : database .LoginTypeOIDC ,
@@ -428,15 +478,22 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
428
478
Message : httpErr .msg ,
429
479
Detail : httpErr .detail ,
430
480
})
481
+ // We pass a disposable user ID just to force an audit diff
482
+ // and generate a log for a failed login
483
+ aReq .New = database.APIKey {UserID : uuid .New ()}
431
484
return
432
485
}
433
486
if err != nil {
434
487
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
435
488
Message : "Failed to process OAuth login." ,
436
489
Detail : err .Error (),
437
490
})
491
+ // We pass a disposable user ID just to force an audit diff
492
+ // and generate a log for a failed login
493
+ aReq .New = database.APIKey {UserID : uuid .New ()}
438
494
return
439
495
}
496
+ aReq .New = key
440
497
441
498
http .SetCookie (rw , cookie )
442
499
@@ -490,11 +547,6 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
490
547
491
548
user = params .User
492
549
link = params .Link
493
- //
494
- // user, link, err = findLinkedUser(ctx, tx, params.LinkedID, params.Email)
495
- // if err != nil {
496
- // return xerrors.Errorf("find linked user: %w", err)
497
- // }
498
550
499
551
if user .ID == uuid .Nil && ! params .AllowSignups {
500
552
return httpError {
0 commit comments