diff --git a/auth/middleware.go b/auth/middleware.go index 98416ac6a3f02e2ab247eadd96d3923cdee62f4a..dde1b7cef600fff4815103ac21293badded695cd 100644 --- a/auth/middleware.go +++ b/auth/middleware.go @@ -219,8 +219,7 @@ } auth.AuthMethod = AUTH_COOKIE - ctx := context.WithValue(r.Context(), userCtxKey, auth) - r = r.WithContext(ctx) + r = r.WithContext(Context(r.Context(), auth)) next.ServeHTTP(w, r) } @@ -310,8 +309,7 @@ } break } - ctx := context.WithValue(r.Context(), userCtxKey, auth) - r = r.WithContext(ctx) + r = r.WithContext(Context(r.Context(), auth)) next.ServeHTTP(w, r) } @@ -558,8 +556,7 @@ if err != nil { panic(err) // unreachable } - ctx := context.WithValue(r.Context(), userCtxKey, &auth) - r = r.WithContext(ctx) + r = r.WithContext(Context(r.Context(), &auth)) next.ServeHTTP(w, r) } @@ -592,7 +589,7 @@ if clientID != nil { whAuth.BearerToken.ClientID = *clientID } - return context.WithValue(ctx, userCtxKey, &whAuth), nil + return Context(ctx, auth), nil } func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler { @@ -641,6 +638,7 @@ }) } } +// Returns the authentication details associated with this context. func ForContext(ctx context.Context) *AuthContext { raw, ok := ctx.Value(userCtxKey).(*AuthContext) if !ok { @@ -648,3 +646,8 @@ panic(fmt.Errorf("invalid authentication context")) } return raw } + +// Creates a new authenticated context. +func Context(base context.Context, auth *AuthContext) context.Context { + return context.WithValue(base, userCtxKey, auth) +}