Skip to content

Commit

Permalink
Merge pull request #12 from jmpsec/permissions
Browse files Browse the repository at this point in the history
Separation between users and admins
  • Loading branch information
javuto authored Sep 24, 2019
2 parents fda4258 + 3361472 commit b9ce02d
Show file tree
Hide file tree
Showing 24 changed files with 465 additions and 218 deletions.
20 changes: 20 additions & 0 deletions cmd/admin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (
"github.com/jmpsec/osctrl/pkg/settings"
)

const (
adminLevel string = "admin"
userLevel string = "user"
)

// Helper to verify if user is an admin
func checkAdminLevel(level string) bool {
return (level == adminLevel)
}

// Handler to check access to a resource based on the authentication enabled
func handlerAuthCheck(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -28,6 +38,11 @@ func handlerAuthCheck(h http.Handler) http.Handler {
s := make(contextValue)
s["user"] = session.Username
s["csrftoken"] = session.Values["csrftoken"].(string)
if session.Values["admin"].(bool) {
s["level"] = adminLevel
} else {
s["level"] = userLevel
}
ctx := context.WithValue(r.Context(), contextKey("session"), s)
// Access granted
h.ServeHTTP(w, r.WithContext(ctx))
Expand Down Expand Up @@ -75,6 +90,11 @@ func handlerAuthCheck(h http.Handler) http.Handler {
s := make(contextValue)
s["user"] = session.Username
s["csrftoken"] = session.Values["csrftoken"].(string)
if session.Values["admin"].(bool) {
s["level"] = adminLevel
} else {
s["level"] = userLevel
}
ctx := context.WithValue(r.Context(), contextKey("session"), s)
// Access granted
samlMiddleware.RequireAccount(h).ServeHTTP(w, r.WithContext(ctx))
Expand Down
Loading

0 comments on commit b9ce02d

Please sign in to comment.