github.com/influxdata/influxdb/v2@v2.7.6/context/token.go (about)

     1  package context
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/influxdata/influxdb/v2"
     8  	"github.com/influxdata/influxdb/v2/kit/platform"
     9  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
    10  )
    11  
    12  type contextKey string
    13  
    14  const (
    15  	authorizerCtxKey    contextKey = "influx/authorizer/v1"
    16  	authorizerCtxPtrKey contextKey = "influx/authorizer/pointer"
    17  )
    18  
    19  // SetAuthorizer sets an authorizer on context.
    20  func SetAuthorizer(ctx context.Context, a influxdb.Authorizer) context.Context {
    21  	return context.WithValue(ctx, authorizerCtxKey, a)
    22  }
    23  
    24  // GetAuthorizer retrieves an authorizer from context.
    25  func GetAuthorizer(ctx context.Context) (influxdb.Authorizer, error) {
    26  	a, ok := ctx.Value(authorizerCtxKey).(influxdb.Authorizer)
    27  	if !ok {
    28  		return nil, &errors.Error{
    29  			Msg:  "authorizer not found on context",
    30  			Code: errors.EInternal,
    31  		}
    32  	}
    33  	if a == nil {
    34  		return nil, &errors.Error{
    35  			Code: errors.EInternal,
    36  			Msg:  "unexpected invalid authorizer",
    37  		}
    38  	}
    39  
    40  	return a, nil
    41  }
    42  
    43  // GetToken retrieves a token from the context; errors if no token.
    44  func GetToken(ctx context.Context) (string, error) {
    45  	a, ok := ctx.Value(authorizerCtxKey).(influxdb.Authorizer)
    46  	if !ok {
    47  		return "", &errors.Error{
    48  			Msg:  "authorizer not found on context",
    49  			Code: errors.EInternal,
    50  		}
    51  	}
    52  
    53  	auth, ok := a.(*influxdb.Authorization)
    54  	if !ok {
    55  		return "", &errors.Error{
    56  			Msg:  fmt.Sprintf("authorizer not an authorization but a %T", a),
    57  			Code: errors.EInternal,
    58  		}
    59  	}
    60  
    61  	return auth.Token, nil
    62  }
    63  
    64  // GetUserID retrieves the user ID from the authorizer on the context.
    65  func GetUserID(ctx context.Context) (platform.ID, error) {
    66  	a, err := GetAuthorizer(ctx)
    67  	if err != nil {
    68  		return 0, err
    69  	}
    70  	return a.GetUserID(), nil
    71  }
    72  
    73  // ProvideAuthorizerStorage puts a pointer to an Authorizer in the context.
    74  // This is used to pass an Authorizer up the stack for logging purposes
    75  func ProvideAuthorizerStorage(ctx context.Context, ap *influxdb.Authorizer) context.Context {
    76  	return context.WithValue(ctx, authorizerCtxPtrKey, ap)
    77  }
    78  
    79  // StoreAuthorizer stores an Authorizer in a pointer from the Context.
    80  // This permits functions deep in the stack to set the pointer to return
    81  // values up the call chain
    82  func StoreAuthorizer(ctx context.Context, auth influxdb.Authorizer) bool {
    83  	ap, ok := ctx.Value(authorizerCtxPtrKey).(*influxdb.Authorizer)
    84  	if ok && (ap != nil) {
    85  		(*ap) = auth
    86  		return true
    87  	} else {
    88  		return false
    89  	}
    90  }