super-graph/serv/auth.go

66 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-03-24 14:57:29 +01:00
package serv
import (
"context"
"net/http"
"strings"
2019-03-24 14:57:29 +01:00
)
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
)
2019-12-31 07:30:20 +01:00
func headerAuth(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
}
2019-12-31 07:30:20 +01:00
func withAuth(next http.Handler) http.Handler {
2019-04-08 08:47:59 +02:00
at := conf.Auth.Type
ru := conf.Auth.Rails.URL
2019-03-24 14:57:29 +01:00
if conf.Auth.CredsInHeader {
next = headerAuth(next)
}
2019-04-08 08:47:59 +02:00
switch at {
case "rails":
if strings.HasPrefix(ru, "memcache:") {
return railsMemcacheHandler(next)
}
2019-03-24 14:57:29 +01:00
if strings.HasPrefix(ru, "redis:") {
return railsRedisHandler(next)
}
2019-03-24 14:57:29 +01:00
return railsCookieHandler(next)
2019-03-24 14:57:29 +01:00
case "jwt":
return jwtHandler(next)
}
return next
}