Skip to content

Commit

Permalink
refactor: mark pkg/strings as deprecated in favor of stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
isegura-eos-eng committed Nov 13, 2024
1 parent 897c720 commit de75928
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
7 changes: 3 additions & 4 deletions pkg/oidc/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"time"

jose "github.com/go-jose/go-jose/v4"

str "github.com/zitadel/oidc/v3/pkg/strings"
)

type Claims interface {
Expand Down Expand Up @@ -84,7 +83,7 @@ type ACRVerifier func(string) error
// if none of the provided values matches the acr claim
func DefaultACRVerifier(possibleValues []string) ACRVerifier {
return func(acr string) error {
if !str.Contains(possibleValues, acr) {
if !slices.Contains(possibleValues, acr) {
return fmt.Errorf("expected one of: %v, got: %q", possibleValues, acr)
}
return nil
Expand Down Expand Up @@ -123,7 +122,7 @@ func CheckIssuer(claims Claims, issuer string) error {
}

func CheckAudience(claims Claims, clientID string) error {
if !str.Contains(claims.GetAudience(), clientID) {
if !slices.Contains(claims.GetAudience(), clientID) {
return fmt.Errorf("%w: Audience must contain client_id %q", ErrAudience, clientID)
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/op/auth_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/bmatcuk/doublestar/v4"
httphelper "github.com/zitadel/oidc/v3/pkg/http"
"github.com/zitadel/oidc/v3/pkg/oidc"
str "github.com/zitadel/oidc/v3/pkg/strings"
)

type AuthRequest interface {
Expand Down Expand Up @@ -156,7 +155,7 @@ func ParseRequestObject(ctx context.Context, authReq *oidc.AuthRequest, storage
if requestObject.Issuer != requestObject.ClientID {
return oidc.ErrInvalidRequest().WithDescription("missing or wrong issuer in request")
}
if !str.Contains(requestObject.Audience, issuer) {
if !slices.Contains(requestObject.Audience, issuer) {

Check warning on line 158 in pkg/op/auth_request.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/auth_request.go#L158

Added line #L158 was not covered by tests
return oidc.ErrInvalidRequest().WithDescription("issuer missing in audience")
}
keySet := &jwtProfileKeySet{storage: storage, clientID: requestObject.Issuer}
Expand All @@ -170,7 +169,7 @@ func ParseRequestObject(ctx context.Context, authReq *oidc.AuthRequest, storage
// CopyRequestObjectToAuthRequest overwrites present values from the Request Object into the auth request
// and clears the `RequestParam` of the auth request
func CopyRequestObjectToAuthRequest(authReq *oidc.AuthRequest, requestObject *oidc.RequestObject) {
if str.Contains(authReq.Scopes, oidc.ScopeOpenID) && len(requestObject.Scopes) > 0 {
if slices.Contains(authReq.Scopes, oidc.ScopeOpenID) && len(requestObject.Scopes) > 0 {

Check warning on line 172 in pkg/op/auth_request.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/auth_request.go#L172

Added line #L172 was not covered by tests
authReq.Scopes = requestObject.Scopes
}
if requestObject.RedirectURI != "" {
Expand Down Expand Up @@ -288,7 +287,7 @@ func ValidateAuthReqScopes(client Client, scopes []string) ([]string, error) {
// checkURIAgainstRedirects just checks aginst the valid redirect URIs and ignores
// other factors.
func checkURIAgainstRedirects(client Client, uri string) error {
if str.Contains(client.RedirectURIs(), uri) {
if slices.Contains(client.RedirectURIs(), uri) {
return nil
}
if globClient, ok := client.(HasRedirectGlobs); ok {
Expand Down
6 changes: 3 additions & 3 deletions pkg/op/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"math/big"
"net/http"
"net/url"
"slices"
"strings"
"time"

httphelper "github.com/zitadel/oidc/v3/pkg/http"
"github.com/zitadel/oidc/v3/pkg/oidc"
strs "github.com/zitadel/oidc/v3/pkg/strings"
)

type DeviceAuthorizationConfig struct {
Expand Down Expand Up @@ -276,7 +276,7 @@ func (r *DeviceAuthorizationState) GetAMR() []string {
}

func (r *DeviceAuthorizationState) GetAudience() []string {
if !strs.Contains(r.Audience, r.ClientID) {
if !slices.Contains(r.Audience, r.ClientID) {
r.Audience = append(r.Audience, r.ClientID)
}
return r.Audience
Expand Down Expand Up @@ -348,7 +348,7 @@ func CreateDeviceTokenResponse(ctx context.Context, tokenRequest TokenRequest, c
}

// TODO(v4): remove type assertion
if idTokenRequest, ok := tokenRequest.(IDTokenRequest); ok && strs.Contains(tokenRequest.GetScopes(), oidc.ScopeOpenID) {
if idTokenRequest, ok := tokenRequest.(IDTokenRequest); ok && slices.Contains(tokenRequest.GetScopes(), oidc.ScopeOpenID) {
response.IDToken, err = CreateIDToken(ctx, IssuerFromContext(ctx), idTokenRequest, client.IDTokenLifetime(), accessToken, "", creator.Storage(), client)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/op/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package op

import (
"context"
"slices"
"time"

"github.com/zitadel/oidc/v3/pkg/crypto"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/strings"
)

type TokenCreator interface {
Expand Down Expand Up @@ -83,13 +83,13 @@ func createTokens(ctx context.Context, tokenRequest TokenRequest, storage Storag
func needsRefreshToken(tokenRequest TokenRequest, client AccessTokenClient) bool {
switch req := tokenRequest.(type) {
case AuthRequest:
return strings.Contains(req.GetScopes(), oidc.ScopeOfflineAccess) && req.GetResponseType() == oidc.ResponseTypeCode && ValidateGrantType(client, oidc.GrantTypeRefreshToken)
return slices.Contains(req.GetScopes(), oidc.ScopeOfflineAccess) && req.GetResponseType() == oidc.ResponseTypeCode && ValidateGrantType(client, oidc.GrantTypeRefreshToken)
case TokenExchangeRequest:
return req.GetRequestedTokenType() == oidc.RefreshTokenType
case RefreshTokenRequest:
return true
case *DeviceAuthorizationState:
return strings.Contains(req.GetScopes(), oidc.ScopeOfflineAccess) && ValidateGrantType(client, oidc.GrantTypeRefreshToken)
return slices.Contains(req.GetScopes(), oidc.ScopeOfflineAccess) && ValidateGrantType(client, oidc.GrantTypeRefreshToken)
default:
return false
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/op/token_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"net/http"
"slices"
"time"

httphelper "github.com/zitadel/oidc/v3/pkg/http"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/strings"
)

type RefreshTokenRequest interface {
Expand Down Expand Up @@ -85,7 +85,7 @@ func ValidateRefreshTokenScopes(requestedScopes []string, authRequest RefreshTok
return nil
}
for _, scope := range requestedScopes {
if !strings.Contains(authRequest.GetScopes(), scope) {
if !slices.Contains(authRequest.GetScopes(), scope) {
return oidc.ErrInvalidScope()
}
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/strings/strings.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package strings

import "slices"

// Deprecated: Use go slices package instead.
func Contains(list []string, needle string) bool {
for _, item := range list {
if item == needle {
return true
}
}
return false
// TODO(v4): remove package.
return slices.Contains(list, needle)
}

0 comments on commit de75928

Please sign in to comment.