Skip to content

Commit 36c14a6

Browse files
committed
refactor(auth): simplify secret key validation return values
1 parent 237c0a7 commit 36c14a6

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

internal/features/auth/service.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ func (s *Service) GetAuthorizationFromBearerToken(_ context.Context, headerToken
102102

103103
token := parts[1]
104104

105-
if err := s.validateSecretKey(token); err != nil {
106-
return "", err
107-
}
108-
109-
return token, nil
105+
return s.validateSecretKey(token)
110106
}
111107

112108
func (s *Service) GetAuthorizationFromQuery(_ context.Context, queryToken string) (string, error) {
@@ -117,11 +113,7 @@ func (s *Service) GetAuthorizationFromQuery(_ context.Context, queryToken string
117113
}
118114
}
119115

120-
if err := s.validateSecretKey(queryToken); err != nil {
121-
return "", err
122-
}
123-
124-
return queryToken, nil
116+
return s.validateSecretKey(queryToken)
125117
}
126118

127119
func (s *Service) GetAuthorizationFromBearerOrQuery(ctx context.Context, headerToken, queryToken string) (string, error) {
@@ -156,16 +148,16 @@ func (s *Service) IsAdminFromOauth(ctx context.Context) (bool, error) {
156148
//
157149
// Rate limiting (Cloudflare + API Gateway) is the primary defense against
158150
// brute-force/timing attacks; this is defense-in-depth in case those fail open.
159-
func (s *Service) validateSecretKey(token string) error {
151+
func (s *Service) validateSecretKey(token string) (string, error) {
160152
tokenHash := sha256.Sum256([]byte(token))
161153
secretKeyHash := sha256.Sum256([]byte(s.secretKey))
162154

163155
if subtle.ConstantTimeCompare(tokenHash[:], secretKeyHash[:]) != 1 {
164-
return &api.HTTPError{
156+
return "", &api.HTTPError{
165157
Message: "Invalid token",
166158
StatusCode: http.StatusUnauthorized,
167159
}
168160
}
169161

170-
return nil
162+
return token, nil
171163
}

0 commit comments

Comments
 (0)