jwt

package
v0.2.71 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildJWT

func BuildJWT(user string) string

BuildJWT builds a signed JWT token from user string. User string can be user ID, or DID, or something similar. This function is called to generate a token for client. The token is checked with the check function.

func BuildJWTWithLabel

func BuildJWTWithLabel(user, label string) string

func BuildJWTWithTime

func BuildJWTWithTime(user, label string, timeValid time.Duration) string

func CheckTokenValidity

func CheckTokenValidity(ctx context.Context) (context.Context, error)

CheckTokenValidity check if context includes valid JWT and if so, wraps a new one with valid user ID.

func EnsureValidToken

func EnsureValidToken(ctx context.Context, req interface{},
	info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

EnsureValidToken ensures a valid token exists within a request's metadata. If the token is missing or invalid, the interceptor blocks execution of the handler and returns an error. Otherwise, the interceptor invokes the unary handler.

func EnsureValidTokenStream

func EnsureValidTokenStream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

EnsureValidTokenStream ensures a valid token exists within a request's metadata. If the token is missing or invalid, the interceptor blocks execution of the handler and returns an error. Otherwise, the interceptor invokes the unary handler.

func IsTimeLeft

func IsTimeLeft(tokenStr string, delta time.Duration) bool

IsTimeLeft calculates if time is left in token with given duration.

func IsValidUser

func IsValidUser(user string, authorization []string) bool

IsValidUser loops thru the authorization and checks that correct user is in the Bearer JWT token.

func NewContextWithUser added in v0.2.70

func NewContextWithUser(ctx context.Context, user string) context.Context

func OauthToken

func OauthToken(jwt string) *oauth2.Token

OauthToken returns our JWT token as an oauth because it helps wrapping it to gRPC credentials.

func ParseValidate

func ParseValidate(user, tokenStr string) bool

ParseValidate validates the JWT token and that cheks it contains the correct the Username.

func SetJWTSecret

func SetJWTSecret(jwtSecret string)

func TimeLeft added in v0.2.23

func TimeLeft(tokenStr string) (t time.Duration, err error)

func TokenToContext

func TokenToContext(ctx context.Context, contextKey interface{}, token *Token) context.Context

TokenToContext stores user data from raw token to context Used with tests

func User

func User(ctx context.Context) string

User is a helper function to get user from the current ctx as a string.

Types

type Token

type Token struct {
	Label   string
	AgentID string
	Raw     string
}

func TokenFromContext

func TokenFromContext(ctx context.Context, contextKey interface{}) (*Token, error)

TokenFromContext receives the user data stored to context NOTE: token is validated by middleware before storing to context, actual verification is done currently there

type UserCtxKey

type UserCtxKey string

UserCtxKey is type for key to access user value from context. It's currently exported for possible outside use. NOTE: Must be own type and cannot be type alias because linter warnings with context!

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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