X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4b4458cfb9dbc2f80ab819efcb1533fcff8f6503..78c908ca43839aa38bb45ef9a9959e1005e39494:/sdk/go/auth/auth.go diff --git a/sdk/go/auth/auth.go b/sdk/go/auth/auth.go index ea492430e4..3c266e0d3a 100644 --- a/sdk/go/auth/auth.go +++ b/sdk/go/auth/auth.go @@ -19,7 +19,11 @@ func NewCredentials() *Credentials { return &Credentials{Tokens: []string{}} } -func NewCredentialsFromHTTPRequest(r *http.Request) *Credentials { +func CredentialsFromRequest(r *http.Request) *Credentials { + if c, ok := r.Context().Value(contextKeyCredentials).(*Credentials); ok { + // preloaded by middleware + return c + } c := NewCredentials() c.LoadTokensFromHTTPRequest(r) return c @@ -34,7 +38,7 @@ var EncodeTokenCookie func([]byte) string = base64.URLEncoding.EncodeToString // token. var DecodeTokenCookie func(string) ([]byte, error) = base64.URLEncoding.DecodeString -// LoadTokensFromHttpRequest loads all tokens it can find in the +// LoadTokensFromHTTPRequest loads all tokens it can find in the // headers and query string of an http query. func (a *Credentials) LoadTokensFromHTTPRequest(r *http.Request) { // Load plain token from "Authorization: OAuth2 ..." header @@ -83,7 +87,21 @@ func (a *Credentials) loadTokenFromCookie(r *http.Request) { a.Tokens = append(a.Tokens, string(token)) } -// TODO: LoadTokensFromHttpRequestBody(). We can't assume in -// LoadTokensFromHttpRequest() that [or how] we should read and parse -// the request body. This has to be requested explicitly by the -// application. +// LoadTokensFromHTTPRequestBody() loads credentials from the request +// body. +// +// This is separate from LoadTokensFromHTTPRequest() because it's not +// always desirable to read the request body. This has to be requested +// explicitly by the application. +func (a *Credentials) LoadTokensFromHTTPRequestBody(r *http.Request) error { + if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" { + return nil + } + if err := r.ParseForm(); err != nil { + return err + } + if t := r.PostFormValue("api_token"); t != "" { + a.Tokens = append(a.Tokens, t) + } + return nil +}