@@ -231,7 +231,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
231
231
return
232
232
}
233
233
234
- user , roles , ok := api .loginRequest (ctx , rw , loginWithPassword )
234
+ user , actor , ok := api .loginRequest (ctx , rw , loginWithPassword )
235
235
// 'user.ID' will be empty, or will be an actual value. Either is correct
236
236
// here.
237
237
aReq .UserID = user .ID
@@ -240,21 +240,8 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
240
240
return
241
241
}
242
242
243
- roleNames , err := roles .RoleNames ()
244
- if err != nil {
245
- httpapi .InternalServerError (rw , err )
246
- return
247
- }
248
-
249
- userSubj := rbac.Subject {
250
- ID : user .ID .String (),
251
- Roles : rbac .RoleIdentifiers (roleNames ),
252
- Groups : roles .Groups ,
253
- Scope : rbac .ScopeAll ,
254
- }
255
-
256
243
//nolint:gocritic // Creating the API key as the user instead of as system.
257
- cookie , key , err := api .createAPIKey (dbauthz .As (ctx , userSubj ), apikey.CreateParams {
244
+ cookie , key , err := api .createAPIKey (dbauthz .As (ctx , actor ), apikey.CreateParams {
258
245
UserID : user .ID ,
259
246
LoginType : database .LoginTypePassword ,
260
247
RemoteAddr : r .RemoteAddr ,
@@ -284,7 +271,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
284
271
//
285
272
// The user struct is always returned, even if authentication failed. This is
286
273
// to support knowing what user attempted to login.
287
- func (api * API ) loginRequest (ctx context.Context , rw http.ResponseWriter , req codersdk.LoginWithPasswordRequest ) (database.User , database. GetAuthorizationUserRolesRow , bool ) {
274
+ func (api * API ) loginRequest (ctx context.Context , rw http.ResponseWriter , req codersdk.LoginWithPasswordRequest ) (database.User , rbac. Subject , bool ) {
288
275
logger := api .Logger .Named (userAuthLoggerName )
289
276
290
277
//nolint:gocritic // In order to login, we need to get the user first!
@@ -296,7 +283,7 @@ func (api *API) loginRequest(ctx context.Context, rw http.ResponseWriter, req co
296
283
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
297
284
Message : "Internal error." ,
298
285
})
299
- return user , database. GetAuthorizationUserRolesRow {}, false
286
+ return user , rbac. Subject {}, false
300
287
}
301
288
302
289
// If the user doesn't exist, it will be a default struct.
@@ -306,7 +293,7 @@ func (api *API) loginRequest(ctx context.Context, rw http.ResponseWriter, req co
306
293
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
307
294
Message : "Internal error." ,
308
295
})
309
- return user , database. GetAuthorizationUserRolesRow {}, false
296
+ return user , rbac. Subject {}, false
310
297
}
311
298
312
299
if ! equal {
@@ -315,7 +302,7 @@ func (api *API) loginRequest(ctx context.Context, rw http.ResponseWriter, req co
315
302
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
316
303
Message : "Incorrect email or password." ,
317
304
})
318
- return user , database. GetAuthorizationUserRolesRow {}, false
305
+ return user , rbac. Subject {}, false
319
306
}
320
307
321
308
// If password authentication is disabled and the user does not have the
@@ -324,14 +311,14 @@ func (api *API) loginRequest(ctx context.Context, rw http.ResponseWriter, req co
324
311
httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
325
312
Message : "Password authentication is disabled." ,
326
313
})
327
- return user , database. GetAuthorizationUserRolesRow {}, false
314
+ return user , rbac. Subject {}, false
328
315
}
329
316
330
317
if user .LoginType != database .LoginTypePassword {
331
318
httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
332
319
Message : fmt .Sprintf ("Incorrect login type, attempting to use %q but user is of login type %q" , database .LoginTypePassword , user .LoginType ),
333
320
})
334
- return user , database. GetAuthorizationUserRolesRow {}, false
321
+ return user , rbac. Subject {}, false
335
322
}
336
323
337
324
if user .Status == database .UserStatusDormant {
@@ -346,29 +333,28 @@ func (api *API) loginRequest(ctx context.Context, rw http.ResponseWriter, req co
346
333
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
347
334
Message : "Internal error occurred. Try again later, or contact an admin for assistance." ,
348
335
})
349
- return user , database. GetAuthorizationUserRolesRow {}, false
336
+ return user , rbac. Subject {}, false
350
337
}
351
338
}
352
339
353
- //nolint:gocritic // System needs to fetch user roles in order to login user.
354
- roles , err := api .Database .GetAuthorizationUserRoles (dbauthz .AsSystemRestricted (ctx ), user .ID )
340
+ subject , userStatus , err := httpmw .UserRBACSubject (ctx , api .Database , user .ID , rbac .ScopeAll )
355
341
if err != nil {
356
342
logger .Error (ctx , "unable to fetch authorization user roles" , slog .Error (err ))
357
343
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
358
344
Message : "Internal error." ,
359
345
})
360
- return user , database. GetAuthorizationUserRolesRow {}, false
346
+ return user , rbac. Subject {}, false
361
347
}
362
348
363
349
// If the user logged into a suspended account, reject the login request.
364
- if roles . Status != database .UserStatusActive {
350
+ if userStatus != database .UserStatusActive {
365
351
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
366
- Message : fmt .Sprintf ("Your account is %s. Contact an admin to reactivate your account." , roles . Status ),
352
+ Message : fmt .Sprintf ("Your account is %s. Contact an admin to reactivate your account." , userStatus ),
367
353
})
368
- return user , database. GetAuthorizationUserRolesRow {}, false
354
+ return user , rbac. Subject {}, false
369
355
}
370
356
371
- return user , roles , true
357
+ return user , subject , true
372
358
}
373
359
374
360
// Clear the user's session cookie.
0 commit comments