super-graph/serv/auth.go

65 lines
1.1 KiB
Go
Raw Normal View History

2019-03-24 14:57:29 +01:00
package serv
import (
"context"
"errors"
"net/http"
)
const (
salt = "encrypted cookie"
signSalt = "signed encrypted cookie"
emptySecret = ""
authHeader = "Authorization"
)
var (
2019-03-29 03:34:42 +01:00
userIDProviderKey = struct{}{}
userIDKey = struct{}{}
errSessionData = errors.New("error decoding session data")
2019-03-24 14:57:29 +01:00
)
func headerHandler(next http.HandlerFunc) http.HandlerFunc {
2019-04-08 08:47:59 +02:00
fn := conf.Auth.Header
2019-03-24 14:57:29 +01:00
if len(fn) == 0 {
2019-04-08 08:47:59 +02:00
panic(errors.New("no auth.header defined"))
2019-03-24 14:57:29 +01:00
}
return func(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get(fn)
if len(userID) == 0 {
next.ServeHTTP(w, r)
return
}
ctx := context.WithValue(r.Context(), userIDKey, userID)
next.ServeHTTP(w, r.WithContext(ctx))
}
}
func withAuth(next http.HandlerFunc) http.HandlerFunc {
2019-04-08 08:47:59 +02:00
at := conf.Auth.Type
2019-03-24 14:57:29 +01:00
2019-04-08 08:47:59 +02:00
switch at {
2019-03-24 14:57:29 +01:00
case "header":
return headerHandler(next)
2019-04-08 08:47:59 +02:00
case "rails_cookie":
return railsCookieHandler(next)
2019-03-24 14:57:29 +01:00
2019-04-08 08:47:59 +02:00
case "rails_memcache":
return railsMemcacheHandler(next)
2019-03-24 14:57:29 +01:00
2019-04-08 08:47:59 +02:00
case "rails_redis":
return railsRedisHandler(next)
2019-03-24 14:57:29 +01:00
case "jwt":
return jwtHandler(next)
default:
2019-04-08 08:47:59 +02:00
return next
2019-03-24 14:57:29 +01:00
}
return next
}