2019-03-24 14:57:29 +01:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2019-04-10 07:38:48 +02:00
|
|
|
"strings"
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-29 03:34:42 +01:00
|
|
|
userIDProviderKey = struct{}{}
|
|
|
|
userIDKey = struct{}{}
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
2019-04-10 07:38:48 +02:00
|
|
|
func headerAuth(r *http.Request, c *config) *http.Request {
|
|
|
|
if len(c.Auth.Header) == 0 {
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 07:38:48 +02:00
|
|
|
userID := r.Header.Get(c.Auth.Header)
|
|
|
|
if len(userID) != 0 {
|
2019-03-24 14:57:29 +01:00
|
|
|
ctx := context.WithValue(r.Context(), userIDKey, userID)
|
2019-04-10 07:38:48 +02:00
|
|
|
return r.WithContext(ctx)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-04-10 07:38:48 +02:00
|
|
|
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func withAuth(next http.HandlerFunc) http.HandlerFunc {
|
2019-04-08 08:47:59 +02:00
|
|
|
at := conf.Auth.Type
|
2019-04-10 07:38:48 +02:00
|
|
|
ru := conf.Auth.Rails.URL
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
switch at {
|
2019-04-10 07:38:48 +02:00
|
|
|
case "rails":
|
|
|
|
if strings.HasPrefix(ru, "memcache:") {
|
|
|
|
return railsMemcacheHandler(next)
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-04-10 07:38:48 +02:00
|
|
|
if strings.HasPrefix(ru, "redis:") {
|
|
|
|
return railsRedisHandler(next)
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-04-10 07:38:48 +02:00
|
|
|
return railsCookieHandler(next)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
case "jwt":
|
|
|
|
return jwtHandler(next)
|
|
|
|
}
|
|
|
|
|
|
|
|
return next
|
|
|
|
}
|