github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/rpc/context.go (about)

     1  // Quorum
     2  package rpc
     3  
     4  import (
     5  	"context"
     6  
     7  	"github.com/kisexp/xdchain/core/types"
     8  	"github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto"
     9  )
    10  
    11  type securityContextKey string
    12  type SecurityContext context.Context
    13  
    14  const (
    15  	HttpAuthorizationHeader              = "Authorization"
    16  	HttpPrivateStateIdentifierHeader     = "Quorum-PSI"
    17  	QueryPrivateStateIdentifierParamName = "PSI"
    18  	EnvVarPrivateStateIdentifier         = "QUORUM_PSI"
    19  	// this key is set by server to indicate if server supports mulitenancy
    20  	ctxIsMultitenant = securityContextKey("IS_MULTITENANT")
    21  	// this key is set into the secured context to indicate
    22  	// the authorized private state being operated on for the request.
    23  	// the value MUST BE OF TYPE types.PrivateStateIdentifier
    24  	ctxPrivateStateIdentifier = securityContextKey("PRIVATE_STATE_IDENTIFIER")
    25  	// this key is set into the request context to indicate
    26  	// the private state being operated on for the request
    27  	ctxRequestPrivateStateIdentifier = securityContextKey("REQUEST_PRIVATE_STATE_IDENTIFIER")
    28  	// this key is exported for WS transport
    29  	ctxCredentialsProvider = securityContextKey("CREDENTIALS_PROVIDER") // key to save reference to rpc.HttpCredentialsProviderFunc
    30  	ctxPSIProvider         = securityContextKey("PSI_PROVIDER")         // key to save reference to rpc.PSIProviderFunc
    31  	// keys used to save values in request context
    32  	ctxAuthenticationError   = securityContextKey("AUTHENTICATION_ERROR")   // key to save error during authentication before processing the request body
    33  	ctxPreauthenticatedToken = securityContextKey("PREAUTHENTICATED_TOKEN") // key to save the preauthenticated token once authenticated
    34  )
    35  
    36  // WithIsMultitenant populates ctx with ctxIsMultitenant key and provided value
    37  func WithIsMultitenant(ctx context.Context, isMultitenant bool) SecurityContext {
    38  	return context.WithValue(ctx, ctxIsMultitenant, isMultitenant)
    39  }
    40  
    41  // IsMultitenantFromContext returns bool value from ctx with ctxIsMultitenant key
    42  // and returns false if value does not exist in the ctx
    43  func IsMultitenantFromContext(ctx SecurityContext) bool {
    44  	if f, ok := ctx.Value(ctxIsMultitenant).(bool); ok {
    45  		return f
    46  	}
    47  	return false
    48  }
    49  
    50  // WithPrivateStateIdentifier populates ctx with ctxPrivateStateIdentifier key and provided value
    51  func WithPrivateStateIdentifier(ctx context.Context, psi types.PrivateStateIdentifier) SecurityContext {
    52  	return context.WithValue(ctx, ctxPrivateStateIdentifier, psi)
    53  }
    54  
    55  // PrivateStateIdentifierFromContext returns types.PrivateStateIdentifier value from ctx with ctxPrivateStateIdentifier key
    56  func PrivateStateIdentifierFromContext(ctx SecurityContext) (types.PrivateStateIdentifier, bool) {
    57  	psi, found := ctx.Value(ctxPrivateStateIdentifier).(types.PrivateStateIdentifier)
    58  	return psi, found
    59  }
    60  
    61  // WithCredentialsProvider populates ctx with ctxCredentialsProvider key and provided value
    62  func WithCredentialsProvider(ctx context.Context, f HttpCredentialsProviderFunc) SecurityContext {
    63  	return context.WithValue(ctx, ctxCredentialsProvider, f)
    64  }
    65  
    66  // CredentialsProviderFromContext returns HttpCredentialsProviderFunc value from ctx with ctxCredentialsProvider key
    67  // and returns nil if value does not exist in the ctx
    68  func CredentialsProviderFromContext(ctx SecurityContext) HttpCredentialsProviderFunc {
    69  	if f, ok := ctx.Value(ctxCredentialsProvider).(HttpCredentialsProviderFunc); ok {
    70  		return f
    71  	}
    72  	return nil
    73  }
    74  
    75  // WithPSIProvider populates ctx with ctxPSIProvider key and provided value
    76  func WithPSIProvider(ctx context.Context, f PSIProviderFunc) SecurityContext {
    77  	return context.WithValue(ctx, ctxPSIProvider, f)
    78  }
    79  
    80  // PSIProviderFromContext returns PSIProviderFunc value from ctx with ctxPSIProvider key
    81  // and returns nil if value does not exist in the ctx
    82  func PSIProviderFromContext(ctx SecurityContext) PSIProviderFunc {
    83  	if f, ok := ctx.Value(ctxPSIProvider).(PSIProviderFunc); ok {
    84  		return f
    85  	}
    86  	return nil
    87  }
    88  
    89  // WithPreauthenticatedToken populates ctx with ctxPreauthenticatedToken key and provided value
    90  func WithPreauthenticatedToken(ctx context.Context, token *proto.PreAuthenticatedAuthenticationToken) SecurityContext {
    91  	return context.WithValue(ctx, ctxPreauthenticatedToken, token)
    92  }
    93  
    94  // PreauthenticatedTokenFromContext returns *proto.PreAuthenticatedAuthenticationToken value from ctx with ctxPreauthenticatedToken key
    95  // and returns nil if value does not exist in the ctx
    96  func PreauthenticatedTokenFromContext(ctx SecurityContext) *proto.PreAuthenticatedAuthenticationToken {
    97  	if t, ok := ctx.Value(ctxPreauthenticatedToken).(*proto.PreAuthenticatedAuthenticationToken); ok {
    98  		return t
    99  	}
   100  	return nil
   101  }