ExtractAccessToken from a request, or return an error detailing what went wrong. The error message MUST be human-readable and comprehensible to the client.
(req *http.Request)
| 102 | // ExtractAccessToken from a request, or return an error detailing what went wrong. The |
| 103 | // error message MUST be human-readable and comprehensible to the client. |
| 104 | func ExtractAccessToken(req *http.Request) (string, error) { |
| 105 | // cf https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/api/auth.py#L631 |
| 106 | authBearer := req.Header.Get("Authorization") |
| 107 | queryToken := req.URL.Query().Get("access_token") |
| 108 | if authBearer != "" && queryToken != "" { |
| 109 | return "", fmt.Errorf("mixing Authorization headers and access_token query parameters") |
| 110 | } |
| 111 | |
| 112 | if queryToken != "" { |
| 113 | return queryToken, nil |
| 114 | } |
| 115 | |
| 116 | if authBearer != "" { |
| 117 | parts := strings.SplitN(authBearer, " ", 2) |
| 118 | if len(parts) != 2 || parts[0] != "Bearer" { |
| 119 | return "", fmt.Errorf("invalid Authorization header") |
| 120 | } |
| 121 | return parts[1], nil |
| 122 | } |
| 123 | |
| 124 | return "", fmt.Errorf("missing access token") |
| 125 | } |
no test coverage detected