summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2023-08-03 21:56:35 -0700
committerMike Crute <mike@crute.us>2023-08-03 21:56:35 -0700
commit896d13f0d17aba8bed9699665bd9a783e6d18f3c (patch)
tree55a39f55f0109ee7da862189791443b07fbf40c4
parenta09b5eb2a787fc6671ea1d2452452b1e58df6a96 (diff)
downloadwebsocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.tar.bz2
websocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.tar.xz
websocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.zip
Use slices
-rw-r--r--app/middleware/token_auth.go11
-rw-r--r--app/models/auth_session.go9
-rw-r--r--app/models/user.go8
-rw-r--r--go.mod1
-rw-r--r--go.sum2
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 (
7 7
8 "code.crute.us/mcrute/ssh-proxy/app/models" 8 "code.crute.us/mcrute/ssh-proxy/app/models"
9 "github.com/labstack/echo/v4" 9 "github.com/labstack/echo/v4"
10 "golang.org/x/exp/slices"
10) 11)
11 12
12const authorizedSession = "__ssh-proxy_authorized_session" 13const authorizedSession = "__ssh-proxy_authorized_session"
@@ -49,15 +50,7 @@ func (m *TokenAuthMiddleware) Middleware(next echo.HandlerFunc) echo.HandlerFunc
49 }) 50 })
50 } 51 }
51 52
52 foundScope := false 53 if !slices.Contains(session.Scope, m.RequiredScope) {
53 for _, s := range session.Scope {
54 if s == m.RequiredScope {
55 foundScope = true
56 break
57 }
58 }
59
60 if !foundScope {
61 return c.JSON(http.StatusUnauthorized, models.Oauth2Error{ 54 return c.JSON(http.StatusUnauthorized, models.Oauth2Error{
62 Type: models.ErrAccessDenied, 55 Type: models.ErrAccessDenied,
63 }) 56 })
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 (
4 "context" 4 "context"
5 "strings" 5 "strings"
6 "time" 6 "time"
7
8 "golang.org/x/exp/slices"
7) 9)
8 10
9type AuthSession struct { 11type AuthSession struct {
@@ -55,12 +57,7 @@ func (s *AuthSession) HasAnyScopes() bool {
55} 57}
56 58
57func (s *AuthSession) HasScope(scope string) bool { 59func (s *AuthSession) HasScope(scope string) bool {
58 for _, c := range s.Scope { 60 return slices.Contains(s.Scope, scope)
59 if c == scope {
60 return true
61 }
62 }
63 return false
64} 61}
65 62
66type AuthSessionStore interface { 63type 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 (
5 "time" 5 "time"
6 6
7 "github.com/go-webauthn/webauthn/webauthn" 7 "github.com/go-webauthn/webauthn/webauthn"
8 "golang.org/x/exp/slices"
8) 9)
9 10
10type User struct { 11type User struct {
@@ -46,12 +47,7 @@ func (u *User) WebAuthnIcon() string {
46} 47}
47 48
48func (u *User) AuthorizedForHost(host string) bool { 49func (u *User) AuthorizedForHost(host string) bool {
49 for _, c := range u.AllowedHosts { 50 return slices.Contains(u.AllowedHosts, host)
50 if host == c {
51 return true
52 }
53 }
54 return false
55} 51}
56 52
57type UserStore interface { 53type 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 (
17 github.com/spf13/cobra v1.7.0 17 github.com/spf13/cobra v1.7.0
18 go.mongodb.org/mongo-driver v1.7.4 18 go.mongodb.org/mongo-driver v1.7.4
19 golang.org/x/crypto v0.11.0 19 golang.org/x/crypto v0.11.0
20 golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
20) 21)
21 22
22require ( 23require (
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
492golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= 492golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
493golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= 493golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
494golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= 494golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
495golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
496golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
495golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= 497golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
496golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= 498golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
497golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 499golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=