super-graph/serv/auth.go

90 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-03-24 14:57:29 +01:00
package serv
import (
"context"
"net/http"
)
2019-12-31 07:30:20 +01:00
type ctxkey int
const (
userIDProviderKey ctxkey = iota
userIDKey
userRoleKey
2019-03-24 14:57:29 +01:00
)
func headerAuth(authc configAuth, next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2019-03-24 14:57:29 +01:00
userIDProvider := r.Header.Get("X-User-ID-Provider")
if len(userIDProvider) != 0 {
ctx = context.WithValue(ctx, userIDProviderKey, userIDProvider)
}
userID := r.Header.Get("X-User-ID")
if len(userID) != 0 {
ctx = context.WithValue(ctx, userIDKey, userID)
}
userRole := r.Header.Get("X-User-Role")
if len(userRole) != 0 {
ctx = context.WithValue(ctx, userRoleKey, userRole)
}
next.ServeHTTP(w, r.WithContext(ctx))
}
2019-03-24 14:57:29 +01:00
}
func headerHandler(authc configAuth, next http.Handler) http.HandlerFunc {
hdr := authc.Header
2019-03-24 14:57:29 +01:00
if len(hdr.Name) == 0 {
errlog.Fatal().Str("auth", authc.Name).Msg("no header.name defined")
}
if !hdr.Exists && len(hdr.Value) == 0 {
errlog.Fatal().Str("auth", authc.Name).Msg("no header.value defined")
}
return func(w http.ResponseWriter, r *http.Request) {
var fo1 bool
value := r.Header.Get(hdr.Name)
switch {
case hdr.Exists:
fo1 = (len(value) == 0)
default:
fo1 = (value != hdr.Value)
}
2019-03-24 14:57:29 +01:00
if fo1 {
http.Error(w, "401 unauthorized", http.StatusUnauthorized)
return
}
2019-03-24 14:57:29 +01:00
next.ServeHTTP(w, r)
}
}
func withAuth(next http.Handler, authc configAuth) http.Handler {
if authc.CredsInHeader {
next = headerAuth(authc, next)
}
switch authc.Type {
case "rails":
return railsHandler(authc, next)
2019-03-24 14:57:29 +01:00
case "jwt":
return jwtHandler(authc, next)
case "header":
return headerHandler(authc, next)
2019-03-24 14:57:29 +01:00
}
return next
}