1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
12 type contextKeyCredentials struct{}
14 // LoadToken wraps the next handler, adding credentials to the request
15 // context so subsequent handlers can access them efficiently via
16 // CredentialsFromRequest.
17 func LoadToken(next http.Handler) http.Handler {
18 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19 if _, ok := r.Context().Value(contextKeyCredentials{}).(*Credentials); !ok {
20 r = r.WithContext(context.WithValue(r.Context(), contextKeyCredentials{}, CredentialsFromRequest(r)))
26 // RequireLiteralToken wraps the next handler, rejecting any request
27 // that doesn't supply the given token. If the given token is empty,
28 // RequireLiteralToken returns next (i.e., no auth checks are
30 func RequireLiteralToken(token string, next http.Handler) http.Handler {
34 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35 c := CredentialsFromRequest(r)
36 if len(c.Tokens) == 0 {
37 http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
40 for _, t := range c.Tokens {
46 http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)