Merge branch '13647-keepstore-config'
[arvados.git] / sdk / go / auth / auth.go
index 3c266e0d3afda2254df6b3c7ccad7157a121bc6c..c2f6a0e8f0885e68a98f7e62a4ee4f17d0d930d2 100644 (file)
@@ -5,6 +5,7 @@
 package auth
 
 import (
+       "context"
        "encoding/base64"
        "net/http"
        "net/url"
@@ -19,8 +20,17 @@ func NewCredentials() *Credentials {
        return &Credentials{Tokens: []string{}}
 }
 
+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 := r.Context().Value(contextKeyCredentials).(*Credentials); ok {
+       if c, ok := FromContext(r.Context()); ok {
                // preloaded by middleware
                return c
        }