fix(auth): admin redirects to OIDC login when no session
This commit is contained in:
parent
bcfe28b16b
commit
94258749dc
1 changed files with 21 additions and 20 deletions
25
src/main.go
25
src/main.go
|
|
@ -167,27 +167,28 @@ func main() {
|
||||||
bearerAuth := admin.TokenAuthMiddleware(cfg.Admin.SecretToken)
|
bearerAuth := admin.TokenAuthMiddleware(cfg.Admin.SecretToken)
|
||||||
adminAuth := func(next http.Handler) http.Handler {
|
adminAuth := func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// First, try session-based authentication
|
if token := r.Header.Get("Authorization"); token != "" {
|
||||||
|
bearerAuth(next).ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
cookie, err := r.Cookie("nextwks_session")
|
cookie, err := r.Cookie("nextwks_session")
|
||||||
if err == nil && cookie != nil {
|
if err != nil || cookie == nil {
|
||||||
|
oidcHandler.LoginRedirect(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
session, err := sessionStore.ValidateSession(cookie.Value)
|
session, err := sessionStore.ValidateSession(cookie.Value)
|
||||||
if err == nil && session != nil {
|
if err != nil || session == nil {
|
||||||
|
oidcHandler.LoginRedirect(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
isAdmin, _ := roleChecker.IsAdmin(session.UserID)
|
isAdmin, _ := roleChecker.IsAdmin(session.UserID)
|
||||||
if isAdmin {
|
if isAdmin {
|
||||||
ctx := context.WithValue(r.Context(), auth.ContextUserID, session.UserID)
|
ctx := context.WithValue(r.Context(), auth.ContextUserID, session.UserID)
|
||||||
ctx = context.WithValue(ctx, auth.ContextRole, "admin")
|
ctx = context.WithValue(ctx, auth.ContextRole, "admin")
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
ctx := context.WithValue(r.Context(), auth.ContextUserID, session.UserID)
|
|
||||||
ctx = context.WithValue(ctx, auth.ContextRole, "user")
|
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
http.Error(w, "{\"error\":\"admin access required\"}", http.StatusForbidden)
|
||||||
}
|
|
||||||
// Fall back to bearer token
|
|
||||||
bearerAuth(next).ServeHTTP(w, r)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue