27 lines
530 B
Go
27 lines
530 B
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type ctxKey struct{}
|
|
|
|
func CallerMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
user := r.Header.Get("X-Auth-Request-User")
|
|
if user == "" {
|
|
user = r.Header.Get("X-Forwarded-User")
|
|
}
|
|
ctx := context.WithValue(r.Context(), ctxKey{}, user)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func Caller(ctx context.Context) string {
|
|
if v, ok := ctx.Value(ctxKey{}).(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|