Skip to content

Commit c54afc5

Browse files
committed
userauth: create API key as user instead of as system
1 parent 2c34f6d commit c54afc5

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

coderd/userauth.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,32 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
112112
return
113113
}
114114

115+
//nolint:gocritic // System needs to fetch user roles in order to login user.
116+
roles, err := api.Database.GetAuthorizationUserRoles(dbauthz.AsSystem(ctx), user.ID)
117+
if err != nil {
118+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
119+
Message: "Internal error.",
120+
})
121+
return
122+
}
123+
115124
// If the user logged into a suspended account, reject the login request.
116-
if user.Status != database.UserStatusActive {
125+
if roles.Status != database.UserStatusActive {
117126
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
118127
Message: "Your account is suspended. Contact an admin to reactivate your account.",
119128
})
120129
return
121130
}
122131

123-
cookie, key, err := api.createAPIKey(dbauthz.AsSystem(ctx), createAPIKeyParams{
132+
userSubj := rbac.Subject{
133+
ID: user.ID.String(),
134+
Roles: rbac.RoleNames(roles.Roles),
135+
Groups: roles.Groups,
136+
Scope: rbac.ScopeAll,
137+
}
138+
139+
//nolint:gocritic // Creating the API key as the user instead of as system.
140+
cookie, key, err := api.createAPIKey(dbauthz.As(ctx, userSubj), createAPIKeyParams{
124141
UserID: user.ID,
125142
LoginType: database.LoginTypePassword,
126143
RemoteAddr: r.RemoteAddr,

0 commit comments

Comments
 (0)