agola/internal/services/gateway/common/common.go
Simone Gotti b7c797bde1 gateway: use dedicated auth context keys type
Use auth context keys dedicated type instead of strings and avoid code
duplication by moving shared code under a common package.
2022-02-24 13:41:57 +01:00

37 lines
680 B
Go

package common
import "context"
type ContextKey int
const (
ContextKeyUserID ContextKey = iota
ContextKeyUsername
ContextKeyUserAdmin
)
func CurrentUserID(ctx context.Context) string {
userIDVal := ctx.Value(ContextKeyUserID)
if userIDVal == nil {
return ""
}
return userIDVal.(string)
}
func IsUserLogged(ctx context.Context) bool {
return ctx.Value(ContextKeyUserID) != nil
}
func IsUserAdmin(ctx context.Context) bool {
isAdmin := false
isAdminVal := ctx.Value(ContextKeyUserAdmin)
if isAdminVal != nil {
isAdmin = isAdminVal.(bool)
}
return isAdmin
}
func IsUserLoggedOrAdmin(ctx context.Context) bool {
return IsUserLogged(ctx) || IsUserAdmin(ctx)
}