Compare commits
No commits in common. "43c2abf1386812b488ee1a7e2620135ddc8f727e" and "29dd3bb2bbb7cc5f6ad58b847309585815840d6e" have entirely different histories.
43c2abf138
...
29dd3bb2bb
13
handlers.go
13
handlers.go
|
@ -28,13 +28,12 @@ func NewHandler(oauth2Config *oauth2.Config, session SessionStorer, verifier *oi
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserClaims struct {
|
type UserClaims struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Verified bool `json:"email_verified"`
|
Verified bool `json:"email_verified"`
|
||||||
Name string `json:"given_name"`
|
Name string `json:"given_name"`
|
||||||
Username string `json:"cognito:username"`
|
Username string `json:"cognito:username"`
|
||||||
Picture string `json:"picture"`
|
Picture string `json:"picture"`
|
||||||
Sub string `json:"sub"`
|
Sub string `json:"sub"`
|
||||||
Groups []string `json:"cognito:groups"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateState() (string, error) {
|
func generateState() (string, error) {
|
||||||
|
|
|
@ -85,31 +85,6 @@ func (m *Middleware) ProtectedRouteWithRedirect(next http.Handler) http.Handler
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAdmin Checks if admin group is present
|
|
||||||
func IsAdmin(groups []string) bool {
|
|
||||||
for _, group := range groups {
|
|
||||||
if group == "admin" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// AdminProtectedRoute Checks if user is member of admin group, if not return forbidden
|
|
||||||
func (m *Middleware) AdminProtectedRoute(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
userOptional := r.Context().Value(userContextKey)
|
|
||||||
if userOptional != nil {
|
|
||||||
user := userOptional.(UserClaims)
|
|
||||||
if IsAdmin(user.Groups) {
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUserFromContext(r *http.Request) *UserClaims {
|
func GetUserFromContext(r *http.Request) *UserClaims {
|
||||||
userOptional := r.Context().Value(userContextKey)
|
userOptional := r.Context().Value(userContextKey)
|
||||||
if userOptional != nil {
|
if userOptional != nil {
|
||||||
|
|
Loading…
Reference in New Issue