fix(context): add {Get,Set}Authorization methods in context package

pull/10616/head
Michael Desa 2018-10-02 13:19:24 -04:00
parent bd07426c87
commit 920ec6749a
1 changed files with 18 additions and 2 deletions

View File

@ -10,8 +10,9 @@ import (
type contextKey string
const (
authorizerCtxKey = contextKey("influx/authorizer/v1")
tokenCtxKey = contextKey("influx/token/v1")
authorizationCtxKey = contextKey("influx/authorization/v1")
authorizerCtxKey = contextKey("influx/authorizer/v1")
tokenCtxKey = contextKey("influx/token/v1")
)
// SetAuthorizer sets an authorizer on context.
@ -43,3 +44,18 @@ func GetToken(ctx context.Context) (string, error) {
return t, nil
}
// SetAuthorization sets an authorization on context.
func SetAuthorization(ctx context.Context, a *platform.Authorization) context.Context {
return context.WithValue(ctx, authorizationCtxKey, a)
}
// GetAuthorization retrieves an authorization from context.
func GetAuthorization(ctx context.Context) (*platform.Authorization, error) {
a, ok := ctx.Value(authorizationCtxKey).(*platform.Authorization)
if !ok {
return nil, errors.InternalErrorf("authorization not found on context")
}
return a, nil
}