Skip to content

Commit 9f22682

Browse files
bnjmnt4ncemalkilic
andauthored
feat: use slices.Contains instead of for loops (#2111)
Use `slices.Contains` instead of manual loops. Some of these were suggested by the gopls language server, and I did a quick search of the codebase for similar patterns which could use the same. Co-authored-by: Cemal Kılıç <[email protected]>
1 parent c0b75f6 commit 9f22682

File tree

6 files changed

+26
-51
lines changed

6 files changed

+26
-51
lines changed

internal/api/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"slices"
78
"strings"
89

910
"github.com/gofrs/uuid"
@@ -51,7 +52,7 @@ func (a *API) requireAdmin(ctx context.Context) (context.Context, error) {
5152

5253
adminRoles := a.config.JWT.AdminRoles
5354

54-
if isStringInSlice(claims.Role, adminRoles) {
55+
if slices.Contains(adminRoles, claims.Role) {
5556
// successful authentication
5657
return withAdminUser(ctx, &models.User{Role: claims.Role, Email: storage.NullString(claims.Role)}), nil
5758
}

internal/api/helpers.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"net/http"
7+
"slices"
78

89
"github.com/supabase/auth/internal/api/apierrors"
910
"github.com/supabase/auth/internal/api/shared"
@@ -34,7 +35,7 @@ func (a *API) requestAud(ctx context.Context, r *http.Request) string {
3435

3536
// ignore the JWT's aud claim if the role is admin
3637
// this is because anon, service_role never had an aud claim to begin with
37-
if claims != nil && !isStringInSlice(claims.Role, config.JWT.AdminRoles) {
38+
if claims != nil && !slices.Contains(config.JWT.AdminRoles, claims.Role) {
3839
aud, _ := claims.GetAudience()
3940
if len(aud) != 0 && aud[0] != "" {
4041
return aud[0]
@@ -45,15 +46,6 @@ func (a *API) requestAud(ctx context.Context, r *http.Request) string {
4546
return config.JWT.Aud
4647
}
4748

48-
func isStringInSlice(checkValue string, list []string) bool {
49-
for _, val := range list {
50-
if val == checkValue {
51-
return true
52-
}
53-
}
54-
return false
55-
}
56-
5749
type RequestParams interface {
5850
AdminUserParams |
5951
CreateSSOProviderParams |

internal/api/middleware.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"slices"
1011
"strings"
1112
"sync"
1213
"time"
@@ -214,18 +215,12 @@ func (a *API) isValidExternalHost(w http.ResponseWriter, req *http.Request) (con
214215
protocol := "https"
215216

216217
if xForwardedHost != "" {
217-
for _, host := range config.Mailer.ExternalHosts {
218-
if host == xForwardedHost {
219-
hostname = host
220-
break
221-
}
218+
if slices.Contains(config.Mailer.ExternalHosts, xForwardedHost) {
219+
hostname = xForwardedHost
222220
}
223221
} else if reqHost != "" {
224-
for _, host := range config.Mailer.ExternalHosts {
225-
if host == reqHost {
226-
hostname = host
227-
break
228-
}
222+
if slices.Contains(config.Mailer.ExternalHosts, reqHost) {
223+
hostname = reqHost
229224
}
230225
}
231226

internal/api/token_oidc.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"fmt"
77
"net/http"
8+
"slices"
89

910
"github.com/coreos/go-oidc/v3/oidc"
1011
"github.com/supabase/auth/internal/api/apierrors"
@@ -112,14 +113,11 @@ func (p *IdTokenGrantParams) getProvider(ctx context.Context, config *conf.Globa
112113
log.WithField("issuer", p.Issuer).WithField("client_id", p.ClientID).Warn("Use of POST /token with arbitrary issuer and client_id is deprecated for security reasons. Please switch to using the API with provider only!")
113114

114115
allowed := false
115-
for _, allowedIssuer := range config.External.AllowedIdTokenIssuers {
116-
if p.Issuer == allowedIssuer {
117-
allowed = true
118-
providerType = allowedIssuer
119-
acceptableClientIDs = []string{p.ClientID}
120-
issuer = allowedIssuer
121-
break
122-
}
116+
if slices.Contains(config.External.AllowedIdTokenIssuers, p.Issuer) {
117+
allowed = true
118+
providerType = p.Issuer
119+
acceptableClientIDs = []string{p.ClientID}
120+
issuer = p.Issuer
123121
}
124122

125123
if !allowed {
@@ -213,14 +211,8 @@ func (a *API) IdTokenGrant(ctx context.Context, w http.ResponseWriter, r *http.R
213211
continue
214212
}
215213

216-
for _, aud := range idToken.Audience {
217-
if aud == clientID {
218-
correctAudience = true
219-
break
220-
}
221-
}
222-
223-
if correctAudience {
214+
if slices.Contains(idToken.Audience, clientID) {
215+
correctAudience = true
224216
break
225217
}
226218
}

internal/conf/jwk.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conf
33
import (
44
"encoding/json"
55
"fmt"
6+
"slices"
67

78
"github.com/golang-jwt/jwt/v5"
89
"github.com/lestrrat-go/jwx/v2/jwk"
@@ -97,11 +98,8 @@ func (j *JwtKeysDecoder) Validate() error {
9798
}
9899
}
99100

100-
for _, op := range key.PrivateKey.KeyOps() {
101-
if op == jwk.KeyOpSign {
102-
signingKeys = append(signingKeys, key.PrivateKey)
103-
break
104-
}
101+
if slices.Contains(key.PrivateKey.KeyOps(), jwk.KeyOpSign) {
102+
signingKeys = append(signingKeys, key.PrivateKey)
105103
}
106104
}
107105

@@ -117,11 +115,9 @@ func (j *JwtKeysDecoder) Validate() error {
117115

118116
func GetSigningJwk(config *JWTConfiguration) (jwk.Key, error) {
119117
for _, key := range config.Keys {
120-
for _, op := range key.PrivateKey.KeyOps() {
121-
// the private JWK with key_ops "sign" should be used as the signing key
122-
if op == jwk.KeyOpSign {
123-
return key.PrivateKey, nil
124-
}
118+
// the private JWK with key_ops "sign" should be used as the signing key
119+
if slices.Contains(key.PrivateKey.KeyOps(), jwk.KeyOpSign) {
120+
return key.PrivateKey, nil
125121
}
126122
}
127123
return nil, fmt.Errorf("no signing key found")

internal/models/sessions.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package models
33
import (
44
"database/sql"
55
"fmt"
6+
"slices"
67
"sort"
78
"strings"
89
"time"
@@ -174,10 +175,8 @@ func (s *Session) DetermineTag(tags []string) string {
174175
return tags[0]
175176
}
176177

177-
for _, t := range tags {
178-
if t == tag {
179-
return tag
180-
}
178+
if slices.Contains(tags, tag) {
179+
return tag
181180
}
182181

183182
return tags[0]

0 commit comments

Comments
 (0)