From 896d13f0d17aba8bed9699665bd9a783e6d18f3c Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Thu, 3 Aug 2023 21:56:35 -0700 Subject: Use slices --- app/middleware/token_auth.go | 11 ++--------- app/models/auth_session.go | 9 +++------ app/models/user.go | 8 ++------ go.mod | 1 + go.sum | 2 ++ 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/app/middleware/token_auth.go b/app/middleware/token_auth.go index 6454ddb..08e302e 100644 --- a/app/middleware/token_auth.go +++ b/app/middleware/token_auth.go @@ -7,6 +7,7 @@ import ( "code.crute.us/mcrute/ssh-proxy/app/models" "github.com/labstack/echo/v4" + "golang.org/x/exp/slices" ) const authorizedSession = "__ssh-proxy_authorized_session" @@ -49,15 +50,7 @@ func (m *TokenAuthMiddleware) Middleware(next echo.HandlerFunc) echo.HandlerFunc }) } - foundScope := false - for _, s := range session.Scope { - if s == m.RequiredScope { - foundScope = true - break - } - } - - if !foundScope { + if !slices.Contains(session.Scope, m.RequiredScope) { return c.JSON(http.StatusUnauthorized, models.Oauth2Error{ Type: models.ErrAccessDenied, }) diff --git a/app/models/auth_session.go b/app/models/auth_session.go index 0b86b16..9e36295 100644 --- a/app/models/auth_session.go +++ b/app/models/auth_session.go @@ -4,6 +4,8 @@ import ( "context" "strings" "time" + + "golang.org/x/exp/slices" ) type AuthSession struct { @@ -55,12 +57,7 @@ func (s *AuthSession) HasAnyScopes() bool { } func (s *AuthSession) HasScope(scope string) bool { - for _, c := range s.Scope { - if c == scope { - return true - } - } - return false + return slices.Contains(s.Scope, scope) } type AuthSessionStore interface { diff --git a/app/models/user.go b/app/models/user.go index 5c9ec90..8145aed 100644 --- a/app/models/user.go +++ b/app/models/user.go @@ -5,6 +5,7 @@ import ( "time" "github.com/go-webauthn/webauthn/webauthn" + "golang.org/x/exp/slices" ) type User struct { @@ -46,12 +47,7 @@ func (u *User) WebAuthnIcon() string { } func (u *User) AuthorizedForHost(host string) bool { - for _, c := range u.AllowedHosts { - if host == c { - return true - } - } - return false + return slices.Contains(u.AllowedHosts, host) } type UserStore interface { diff --git a/go.mod b/go.mod index 95c9e8d..daaa54a 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/spf13/cobra v1.7.0 go.mongodb.org/mongo-driver v1.7.4 golang.org/x/crypto v0.11.0 + golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b ) require ( diff --git a/go.sum b/go.sum index 5c20a64..a9ff448 100644 --- a/go.sum +++ b/go.sum @@ -492,6 +492,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI= +golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -- cgit v1.2.3