auth

package
v2.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 2

README

Auth

GetAccountID

We offer a convenient way to extract the AccountID field from an incoming authorization token. For this purpose auth.GetAccountID(ctx, nil) function can be used:

func (s *contactsServer) Read(ctx context.Context, req *ReadRequest) (*ReadResponse, error) {
	input := req.GetContact()

	accountID, err := auth.GetAccountID(ctx, nil)
	if err == nil {
		input.AccountId = accountID
	} else if input.GetAccountId() == "" {
		return nil, err
	}

	c, err := DefaultReadContact(ctx, input, s.db)
	if err != nil {
		return nil, err
	}
	return &ReadResponse{Contact: c}, nil
}

When bootstrapping a gRPC server, add middleware that will extract the account_id token from the request context and set it in the request struct. The middleware will have to navigate the request struct via reflection, in the case that the account_id field is nested within the request (like if it's in a request wrapper as per our example above)

Documentation

Index

Constants

View Source
const (
	// MultiTenancyField the field name for a specific tenant
	MultiTenancyField = "account_id"

	// AuthorizationHeader contains information about the header value for the token
	AuthorizationHeader = "Authorization"

	// DefaultTokenType is the name of the authorization token (e.g. "Bearer"
	// or "token")
	DefaultTokenType = "Bearer"
)

Variables

This section is empty.

Functions

func GetAccountID

func GetAccountID(ctx context.Context, keyfunc jwt.Keyfunc) (string, error)

GetAccountID gets the JWT from a context and returns the AccountID field

func GetJWTField

func GetJWTField(ctx context.Context, tokenField string, keyfunc jwt.Keyfunc) (string, error)

GetJWTField gets the JWT from a context and returns the specified field using the DefaultTokenName

func GetJWTFieldWithTokenType

func GetJWTFieldWithTokenType(ctx context.Context, tokenType, tokenField string, keyfunc jwt.Keyfunc) (string, error)

GetJWTFieldWithTokenType gets the JWT from a context and returns the specified field. The user must provide a token type, which prefixes the token itself (e.g. "Bearer" or "token")

func LogrusStreamServerInterceptor

func LogrusStreamServerInterceptor() grpc.StreamServerInterceptor

LogrusStreamServerInterceptor returns grpc.StreamServerInterceptor which populates request-scoped logrus logger with account_id field

func LogrusUnaryServerInterceptor

func LogrusUnaryServerInterceptor() grpc.UnaryServerInterceptor

LogrusUnaryServerInterceptor returns grpc.UnaryServerInterceptor which populates request-scoped logrus logger with account_id field

func OutgoingContext

func OutgoingContext(ctx context.Context) context.Context

OutgoingContext set to outgoing context request_id, auth_token, X-Forwarded-For, x-geo- and x-b3- headers value

func OutgoingContextWithCustomParams

func OutgoingContextWithCustomParams(ctx context.Context, keys ...string) context.Context

OutgoingContextWithCustomParams set to outgoing context specified parameters from incoming context by keys

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL