X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/39c17737ac69d7693684fe2f95bef0ec235a28bf..2d9aa62499005abd47b5f2fa604f01d055480722:/sdk/go/auth/auth.go diff --git a/sdk/go/auth/auth.go b/sdk/go/auth/auth.go index ad1d398c76..c2f6a0e8f0 100644 --- a/sdk/go/auth/auth.go +++ b/sdk/go/auth/auth.go @@ -5,6 +5,7 @@ package auth import ( + "context" "encoding/base64" "net/http" "net/url" @@ -19,7 +20,20 @@ func NewCredentials() *Credentials { return &Credentials{Tokens: []string{}} } -func NewCredentialsFromHTTPRequest(r *http.Request) *Credentials { +func NewContext(ctx context.Context, c *Credentials) context.Context { + return context.WithValue(ctx, contextKeyCredentials{}, c) +} + +func FromContext(ctx context.Context) (*Credentials, bool) { + c, ok := ctx.Value(contextKeyCredentials{}).(*Credentials) + return c, ok +} + +func CredentialsFromRequest(r *http.Request) *Credentials { + if c, ok := FromContext(r.Context()); ok { + // preloaded by middleware + return c + } c := NewCredentials() c.LoadTokensFromHTTPRequest(r) return c