summaryrefslogtreecommitdiff
path: root/app
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 /app
parenta09b5eb2a787fc6671ea1d2452452b1e58df6a96 (diff)
downloadwebsocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.tar.bz2
websocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.tar.xz
websocket_proxy-896d13f0d17aba8bed9699665bd9a783e6d18f3c.zip
Use slices
Diffstat (limited to 'app')
-rw-r--r--app/middleware/token_auth.go11
-rw-r--r--app/models/auth_session.go9
-rw-r--r--app/models/user.go8
3 files changed, 7 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 {