aboutsummaryrefslogtreecommitdiff
path: root/internal/authz/middleware.go
diff options
context:
space:
mode:
authorMax Resnick <max@ofmax.li>2024-02-12 21:16:48 -0800
committerMax Resnick <max@ofmax.li>2024-02-17 22:28:39 -0800
commit3db63367ef110e7f4a245cde61471e232e86339c (patch)
tree7be4be99ab5953f8d7beb1c613b0d0bc64db6c65 /internal/authz/middleware.go
parent45a9f3814c14b41b93e47ae4cbc3f50c34d94991 (diff)
downloadgo-git-server-3db63367ef110e7f4a245cde61471e232e86339c.tar.gz
fix: fix up tests and linting
Diffstat (limited to '')
-rw-r--r--internal/authz/middleware.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/internal/authz/middleware.go b/internal/authz/middleware.go
index a35b6b4..6763323 100644
--- a/internal/authz/middleware.go
+++ b/internal/authz/middleware.go
@@ -1,3 +1,4 @@
+// authentication and authorization module
package authz
import (
@@ -11,6 +12,13 @@ import (
"golang.org/x/crypto/bcrypt"
)
+// AuthzContextKey key used to store urn of user in context
+type AuthzContextKey string
+
+var (
+ AuthzUrnKey AuthzContextKey = "goGitAuthzUrn"
+)
+
func Authentication(authMap TokenMap, next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
u, p, ok := req.BasicAuth()
@@ -34,7 +42,7 @@ func Authentication(authMap TokenMap, next http.Handler) http.Handler {
http.Error(rw, "Bad Request", http.StatusForbidden)
return
}
- ctx := context.WithValue(req.Context(), "urn", urn)
+ ctx := context.WithValue(req.Context(), AuthzUrnKey, urn)
next.ServeHTTP(rw, req.WithContext(ctx))
})
}
@@ -43,7 +51,10 @@ func Authentication(authMap TokenMap, next http.Handler) http.Handler {
func Authorization(adminSvc *admin.Servicer, next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
- urn := ctx.Value("urn").(string)
+ urn, ok := ctx.Value(AuthzUrnKey).(string)
+ if !ok || urn == "" {
+ http.Error(rw, "Bad Request", http.StatusBadRequest)
+ }
repo := req.URL.Path
action := req.Method
ok, err := adminSvc.Enforce(urn, repo, action)