Skip to content

Commit cc3394b

Browse files
committed
improvements from code review
1 parent eaad519 commit cc3394b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

coderd/httpmw/apikey.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,7 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
232232
return optionalWrite(http.StatusUnauthorized, resp)
233233
}
234234

235-
var (
236-
link database.UserLink
237-
now = dbtime.Now()
238-
// Tracks if the API key has properties updated
239-
changed = false
240-
)
241-
235+
now := dbtime.Now()
242236
if key.ExpiresAt.Before(now) {
243237
return optionalWrite(http.StatusUnauthorized, codersdk.Response{
244238
Message: SignedOutErrorMessage,
@@ -252,7 +246,7 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
252246
if key.LoginType == database.LoginTypeGithub || key.LoginType == database.LoginTypeOIDC {
253247
var err error
254248
//nolint:gocritic // System needs to fetch UserLink to check if it's valid.
255-
link, err = cfg.DB.GetUserLinkByUserIDLoginType(dbauthz.AsSystemRestricted(ctx), database.GetUserLinkByUserIDLoginTypeParams{
249+
link, err := cfg.DB.GetUserLinkByUserIDLoginType(dbauthz.AsSystemRestricted(ctx), database.GetUserLinkByUserIDLoginTypeParams{
256250
UserID: key.UserID,
257251
LoginType: key.LoginType,
258252
})
@@ -309,7 +303,7 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
309303
if link.OAuthRefreshToken == "" {
310304
return optionalWrite(http.StatusUnauthorized, codersdk.Response{
311305
Message: SignedOutErrorMessage,
312-
Detail: fmt.Sprintf("%s session expired at %q.", friendlyName, link.OAuthExpiry.String()),
306+
Detail: fmt.Sprintf("%s session expired at %q. Try signing in again.", friendlyName, link.OAuthExpiry.String()),
313307
})
314308
}
315309
// We have a refresh token, so let's try it
@@ -351,6 +345,9 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
351345
}
352346
}
353347

348+
// Tracks if the API key has properties updated
349+
changed := false
350+
354351
// Only update LastUsed once an hour to prevent database spam.
355352
if now.Sub(key.LastUsed) > time.Hour {
356353
key.LastUsed = now

0 commit comments

Comments
 (0)