Authenticate returns the request, with added tokens and/or localhost flags in the context, as appropriate.
(req *http.Request)
| 48 | // Authenticate returns the request, with added tokens and/or localhost |
| 49 | // flags in the context, as appropriate. |
| 50 | func (a *API) Authenticate(req *http.Request) (*http.Request, error) { |
| 51 | ctx := certAuthn(req, a.rootCAs) |
| 52 | |
| 53 | token, err := a.tokenAuthn(req) |
| 54 | if err == nil && token != "" { |
| 55 | // if this request was successfully authenticated with a token, pass the token along |
| 56 | ctx = newContextWithToken(ctx, token) |
| 57 | } |
| 58 | |
| 59 | local := a.localhostAuthn(req) |
| 60 | if local { |
| 61 | ctx = newContextWithLocalhost(ctx) |
| 62 | } |
| 63 | |
| 64 | // Temporary workaround. Dashboard is always ok. |
| 65 | // See loopbackOn comment above. |
| 66 | if strings.HasPrefix(req.URL.Path, "/dashboard/") || req.URL.Path == "/dashboard" { |
| 67 | return req.WithContext(ctx), nil |
| 68 | } |
| 69 | if loopbackOn && local { |
| 70 | return req.WithContext(ctx), nil |
| 71 | } |
| 72 | |
| 73 | // if there is no authentication at all, we return an "unauthenticated" error, |
| 74 | // which may be helpful when debugging |
| 75 | if len(X509Certs(ctx)) < 1 && err != nil { |
| 76 | return req, errors.New("unauthenticated") |
| 77 | } |
| 78 | |
| 79 | return req.WithContext(ctx), nil |
| 80 | } |
| 81 | |
| 82 | // checks the request for a valid client cert list. |
| 83 | // If found, it is added to the request's context. |
no test coverage detected