1 package ociauth 2 3 import ( 4 "context" 5 ) 6 7 type scopeKey struct{} 8 9 // ContextWithScope returns ctx annotated with the given 10 // scope. When the ociauth transport receives a request with a scope in the context, 11 // it will treat it as "desired authorization scope"; new authorization tokens 12 // will be acquired with that scope as well as any scope required by 13 // the operation. 14 func ContextWithScope(ctx context.Context, s Scope) context.Context { 15 return context.WithValue(ctx, scopeKey{}, s) 16 } 17 18 // ScopeFromContext returns any scope associated with the context 19 // by [ContextWithScope]. 20 func ScopeFromContext(ctx context.Context) Scope { 21 s, _ := ctx.Value(scopeKey{}).(Scope) 22 return s 23 } 24 25 type requestInfoKey struct{} 26 27 // RequestInfo provides information about the OCI request that 28 // is currently being made. It is expected to be attached to an HTTP 29 // request context. The [ociclient] package will add this to all 30 // requests that is makes. 31 type RequestInfo struct { 32 // RequiredScope holds the authorization scope that's required 33 // by the request. The ociauth logic will reuse any available 34 // auth token that has this scope. When acquiring a new token, 35 // it will add any scope found in [ScopeFromContext] too. 36 RequiredScope Scope 37 } 38 39 // ContextWithRequestInfo returns ctx annotated with the given 40 // request informaton. When ociclient receives a request with 41 // this attached, it will respect info.RequiredScope to determine 42 // what auth tokens to reuse. When it acquires a new token, 43 // it will ask for the union of info.RequiredScope [ScopeFromContext]. 44 func ContextWithRequestInfo(ctx context.Context, info RequestInfo) context.Context { 45 return context.WithValue(ctx, requestInfoKey{}, info) 46 } 47 48 // RequestInfoFromContext returns any request information associated with the context 49 // by [ContextWithRequestInfo]. 50 func RequestInfoFromContext(ctx context.Context) RequestInfo { 51 info, _ := ctx.Value(requestInfoKey{}).(RequestInfo) 52 return info 53 } 54