@@ -51,6 +51,11 @@ type OAuth2Configs struct {
51
51
Github OAuth2Config
52
52
}
53
53
54
+ const (
55
+ signedOutErrorMessage string = "You are signed out or your session has expired. Please sign in again to continue."
56
+ internalErrorMessage string = "An internal error occurred. Please try again or contact the system administrator."
57
+ )
58
+
54
59
// ExtractAPIKey requires authentication using a valid API key.
55
60
// It handles extending an API key if it comes close to expiry,
56
61
// updating the last used time in the database.
@@ -83,15 +88,17 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
83
88
}
84
89
if cookieValue == "" {
85
90
write (http .StatusUnauthorized , codersdk.Response {
86
- Message : fmt .Sprintf ("Cookie %q or query parameter must be provided." , codersdk .SessionTokenKey ),
91
+ Message : signedOutErrorMessage ,
92
+ Detail : fmt .Sprintf ("Cookie %q or query parameter must be provided." , codersdk .SessionTokenKey ),
87
93
})
88
94
return
89
95
}
90
96
parts := strings .Split (cookieValue , "-" )
91
97
// APIKeys are formatted: ID-SECRET
92
98
if len (parts ) != 2 {
93
99
write (http .StatusUnauthorized , codersdk.Response {
94
- Message : fmt .Sprintf ("Invalid %q cookie API key format." , codersdk .SessionTokenKey ),
100
+ Message : signedOutErrorMessage ,
101
+ Detail : fmt .Sprintf ("Invalid %q cookie API key format." , codersdk .SessionTokenKey ),
95
102
})
96
103
return
97
104
}
@@ -100,27 +107,30 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
100
107
// Ensuring key lengths are valid.
101
108
if len (keyID ) != 10 {
102
109
write (http .StatusUnauthorized , codersdk.Response {
103
- Message : fmt .Sprintf ("Invalid %q cookie API key id." , codersdk .SessionTokenKey ),
110
+ Message : signedOutErrorMessage ,
111
+ Detail : fmt .Sprintf ("Invalid %q cookie API key id." , codersdk .SessionTokenKey ),
104
112
})
105
113
return
106
114
}
107
115
if len (keySecret ) != 22 {
108
116
write (http .StatusUnauthorized , codersdk.Response {
109
- Message : fmt .Sprintf ("Invalid %q cookie API key secret." , codersdk .SessionTokenKey ),
117
+ Message : signedOutErrorMessage ,
118
+ Detail : fmt .Sprintf ("Invalid %q cookie API key secret." , codersdk .SessionTokenKey ),
110
119
})
111
120
return
112
121
}
113
122
key , err := db .GetAPIKeyByID (r .Context (), keyID )
114
123
if err != nil {
115
124
if errors .Is (err , sql .ErrNoRows ) {
116
125
write (http .StatusUnauthorized , codersdk.Response {
117
- Message : "API key is invalid." ,
126
+ Message : signedOutErrorMessage ,
127
+ Detail : "API key is invalid." ,
118
128
})
119
129
return
120
130
}
121
131
write (http .StatusInternalServerError , codersdk.Response {
122
- Message : "Internal error fetching API key by id." ,
123
- Detail : err .Error (),
132
+ Message : internalErrorMessage ,
133
+ Detail : fmt . Sprintf ( "Internal error fetching API key by id. %s" , err .Error () ),
124
134
})
125
135
return
126
136
}
@@ -129,7 +139,8 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
129
139
// Checking to see if the secret is valid.
130
140
if subtle .ConstantTimeCompare (key .HashedSecret , hashed [:]) != 1 {
131
141
write (http .StatusUnauthorized , codersdk.Response {
132
- Message : "API key secret is invalid." ,
142
+ Message : signedOutErrorMessage ,
143
+ Detail : "API key secret is invalid." ,
133
144
})
134
145
return
135
146
}
@@ -146,7 +157,8 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
146
157
oauthConfig = oauth .Github
147
158
default :
148
159
write (http .StatusInternalServerError , codersdk.Response {
149
- Message : fmt .Sprintf ("Unexpected authentication type %q." , key .LoginType ),
160
+ Message : internalErrorMessage ,
161
+ Detail : fmt .Sprintf ("Unexpected authentication type %q." , key .LoginType ),
150
162
})
151
163
return
152
164
}
@@ -174,7 +186,8 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
174
186
// Checking if the key is expired.
175
187
if key .ExpiresAt .Before (now ) {
176
188
write (http .StatusUnauthorized , codersdk.Response {
177
- Message : fmt .Sprintf ("API key expired at %q." , key .ExpiresAt .String ()),
189
+ Message : signedOutErrorMessage ,
190
+ Detail : fmt .Sprintf ("API key expired at %q." , key .ExpiresAt .String ()),
178
191
})
179
192
return
180
193
}
@@ -216,7 +229,8 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
216
229
})
217
230
if err != nil {
218
231
write (http .StatusInternalServerError , codersdk.Response {
219
- Message : fmt .Sprintf ("API key couldn't update: %s." , err .Error ()),
232
+ Message : internalErrorMessage ,
233
+ Detail : fmt .Sprintf ("API key couldn't update: %s." , err .Error ()),
220
234
})
221
235
return
222
236
}
@@ -228,8 +242,8 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs, redirectToLogin bool
228
242
roles , err := db .GetAuthorizationUserRoles (r .Context (), key .UserID )
229
243
if err != nil {
230
244
write (http .StatusUnauthorized , codersdk.Response {
231
- Message : "Internal error fetching user's roles." ,
232
- Detail : err .Error (),
245
+ Message : internalErrorMessage ,
246
+ Detail : fmt . Sprintf ( "Internal error fetching user's roles. %s" , err .Error () ),
233
247
})
234
248
return
235
249
}
0 commit comments