auth

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAllowTokenHeader

func WithAllowTokenHeader[T any](header string) kit.Option[Management[T]]

WithAllowTokenHeader is a functional option to set a custom token header in Management. Parameters: - header: The custom header to be used for tokens (string). Returns: - kit.Option[Management[T]]: A function that sets the token header in Management.

func WithDecryptKey

func WithDecryptKey(decryptKey string) kit.Option[Options]

WithDecryptKey is a functional option to set a custom decryption key in Options. Parameters: - decryptKey: The custom decryption key to be used ('string'). Returns: - kit.Option[Options]: A function that sets the decryption key in Options.

func WithExposeAccessHeader

func WithExposeAccessHeader[T any](header string) kit.Option[Management[T]]

WithExposeAccessHeader is a functional option to set a custom header to expose the access token in Management. Parameters: - header: The custom header to expose the access token (string). Returns: - kit.Option[Management[T]]: A function that sets the access token expose header in Management.

func WithExposeRefreshHeader

func WithExposeRefreshHeader[T any](header string) kit.Option[Management[T]]

WithExposeRefreshHeader is a functional option to set a custom header to expose the refresh token in Management. Parameters: - header: The custom header to expose the refresh token (string). Returns: - kit.Option[Management[T]]: A function that sets the refresh token expose header in Management.

func WithGenIDFunc

func WithGenIDFunc(fn func() string) kit.Option[Options]

WithGenIDFunc is a functional option to set a custom ID generation function in Options. Parameters: - fn: The function used to generate IDs ('func() string'). Returns: - kit.Option[Options]: A function that sets the ID generation function in Options.

func WithIssuer

func WithIssuer(issuer string) kit.Option[Options]

WithIssuer is a functional option to set a custom issuer in Options. Parameters: - issuer: The custom issuer entity ('string'). Returns: - kit.Option[Options]: A function that sets the issuer in Options.

func WithMethod

func WithMethod(method jwt.SigningMethod) kit.Option[Options]

WithMethod is a functional option to set a custom JWT signing method in Options. Parameters: - method: The JWT signing method to be used ('jwt.SigningMethod'). Returns: - kit.Option[Options]: A function that sets the JWT signing method in Options.

func WithNowFunc

func WithNowFunc[T any](nowFunc func() time.Time) kit.Option[Management[T]]

WithNowFunc is a functional option to set a custom function to retrieve the current time in Management. Parameters: - nowFunc: The custom function to retrieve the current time (func() time.Time). Returns: - kit.Option[Management[T]]: A function that sets the current time function in Management.

func WithRefreshJWTOptions

func WithRefreshJWTOptions[T any](refreshOpts Options) kit.Option[Management[T]]

WithRefreshJWTOptions is a functional option to set options for refresh JWT in Management. Parameters: - refreshOpts: The options for the refresh JWT (Options). Returns: - kit.Option[Management[T]]: A function that sets the refresh JWT options in Management.

func WithRotateRefreshToken

func WithRotateRefreshToken[T any](isRotate bool) kit.Option[Management[T]]

WithRotateRefreshToken is a functional option to set refresh token rotation in Management. Parameters: - isRotate: A boolean value indicating whether to rotate refresh tokens (bool). Returns: - kit.Option[Management[T]]: A function that sets the refresh token rotation in Management.

Types

type Management

type Management[T any] struct {
	// contains filtered or unexported fields
}

Management struct is a generic type that manages the configuration and operations for token management, including access and refresh tokens.

func InitManagement

func InitManagement[T any](accessJWTOptions Options, opts ...kit.Option[Management[T]]) *Management[T]

InitManagement initializes a Management instance with the provided access JWT options and other optional configurations using variadic functional options. Parameters: - accessJWTOptions: Options for the access JWT (Options). - opts: A variadic list of functional options for configuring the Management instance (kit.Option[Management[T]]). Returns: - *Management[T]: A pointer to the initialized Management instance.

func (*Management[T]) GenerateAccessToken

func (m *Management[T]) GenerateAccessToken(data T) (string, error)

GenerateAccessToken generates a new access token containing the specified data. Parameters: - data: The data to be included in the token (T). Returns: - string: The generated access token. - error: An error if token generation fails.

func (*Management[T]) GenerateRefreshToken

func (m *Management[T]) GenerateRefreshToken(data T) (string, error)

GenerateRefreshToken generates a new refresh token containing the specified data. Parameters: - data: The data to be included in the refresh token (T). Returns: - string: The generated refresh token. - error: An error if token generation fails or refresh options are not set.

func (*Management[T]) MiddlewareBuilder

func (m *Management[T]) MiddlewareBuilder() *MiddlewareBuilder[T]

MiddlewareBuilder initializes a new MiddlewareBuilder for generating middleware. Returns: - *MiddlewareBuilder[T]: A pointer to a new MiddlewareBuilder instance.

func (*Management[T]) Refresh

func (m *Management[T]) Refresh(ctx *mist.Context)

Refresh handles the process of refreshing tokens in an HTTP context. It verifies the refresh token, generates a new access token, and optionally generates a new refresh token, setting headers accordingly. Parameters: - ctx: The HTTP context for the incoming request (*mist.Context).

func (*Management[T]) SetClaims

func (m *Management[T]) SetClaims(ctx *mist.Context, claims RegisteredClaims[T])

SetClaims sets the given claims into the HTTP request context. Parameters: - ctx: The HTTP context for the incoming request (*mist.Context). - claims: The claims to set into the context (RegisteredClaims[T]).

func (*Management[T]) VerifyAccessToken

func (m *Management[T]) VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)

VerifyAccessToken verifies the provided access token. Parameters: - token: The access token to verify (string). - opts: Additional parser options for the JWT (...jwt.ParserOption). Returns: - RegisteredClaims[T]: The claims extracted from the token. - error: An error if token verification fails.

func (*Management[T]) VerifyRefreshToken

func (m *Management[T]) VerifyRefreshToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)

VerifyRefreshToken verifies the provided refresh token. Parameters: - token: The refresh token to verify (string). - opts: Additional parser options for the JWT (...jwt.ParserOption). Returns: - RegisteredClaims[T]: The claims extracted from the token. - error: An error if token verification fails or refresh options are not set.

type Manager

type Manager[T any] interface {
	// MiddlewareBuilder returns an instance of MiddlewareBuilder for the specified type T.
	// Returns:
	// - *MiddlewareBuilder[T]: A pointer to an instance of MiddlewareBuilder for the given type T.
	MiddlewareBuilder() *MiddlewareBuilder[T]

	// GenerateAccessToken generates an access token containing the given data of type T.
	// Parameters:
	// - data: The data to be included in the access token ('T').
	// Returns:
	// - string: The generated access token.
	// - error: An error if token generation fails.
	GenerateAccessToken(data T) (string, error)

	// VerifyAccessToken verifies the given access token and extracts the claims from it.
	// Parameters:
	// - token: The access token to be verified ('string').
	// - opts: Additional options for the JWT parser (variadic 'jwt.ParserOption').
	// Returns:
	// - RegisteredClaims[T]: The claims extracted from the verified token.
	// - error: An error if token verification fails.
	VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)

	// GenerateRefreshToken generates a refresh token containing the given data of type T.
	// Parameters:
	// - data: The data to be included in the refresh token ('T').
	// Returns:
	// - string: The generated refresh token.
	// - error: An error if token generation fails.
	GenerateRefreshToken(data T) (string, error)

	// VerifyRefreshToken verifies the given refresh token and extracts the claims from it.
	// Parameters:
	// - token: The refresh token to be verified ('string').
	// - opts: Additional options for the JWT parser (variadic 'jwt.ParserOption').
	// Returns:
	// - RegisteredClaims[T]: The claims extracted from the verified token.
	// - error: An error if token verification fails.
	VerifyRefreshToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)

	// SetClaims sets the provided claims in the context.
	// Parameters:
	// - ctx: The context where the claims are to be set ('*mist.Context').
	// - claims: The claims to be set in the context ('RegisteredClaims[T]').
	SetClaims(ctx *mist.Context, claims RegisteredClaims[T])
}

Manager is a generic interface that manages tokens and claims for a given type T.

type MiddlewareBuilder

type MiddlewareBuilder[T any] struct {
	// contains filtered or unexported fields
}

MiddlewareBuilder is a generic struct for constructing middleware for type T.

func (*MiddlewareBuilder[T]) Build

func (m *MiddlewareBuilder[T]) Build() mist.Middleware

Build constructs the middleware using the settings configured in the MiddlewareBuilder instance. Returns: - mist.Middleware: A middleware function that processes the request.

func (*MiddlewareBuilder[T]) IgnorePath

func (m *MiddlewareBuilder[T]) IgnorePath(path ...string) *MiddlewareBuilder[T]

IgnorePath sets the paths that should be ignored by middleware. This method internally calls IgnorePathFunc. Parameters: - path: Variadic list of paths to ignore ('...string'). Returns: - *MiddlewareBuilder[T]: The MiddlewareBuilder instance, to allow for method chaining.

func (*MiddlewareBuilder[T]) IgnorePathFunc

func (m *MiddlewareBuilder[T]) IgnorePathFunc(fn func(path string) bool) *MiddlewareBuilder[T]

IgnorePathFunc sets a custom function that determines if a given path should be ignored by the middleware. Parameters: - fn: Function that determines if a path should be ignored ('func(path string) bool'). Returns: - *MiddlewareBuilder[T]: The MiddlewareBuilder instance, to allow for method chaining.

type Options

type Options struct {
	// Expire defines the duration after which the token expires.
	Expire time.Duration

	// EncryptionKey is used to encrypt data.
	EncryptionKey string

	// DecryptKey is used to decrypt data.
	DecryptKey string

	// Method is the JWT signing method used for token generation.
	Method jwt.SigningMethod

	// Issuer is the entity that issues the token.
	Issuer string
	// contains filtered or unexported fields
}

Options struct defines the configuration options for token management.

func InitOptions

func InitOptions(expire time.Duration, encryptionKey string, opts ...kit.Option[Options]) Options

InitOptions initializes an Options struct with default or provided values. Parameters: - expire: The duration token should be valid for ('time.Duration'). - encryptionKey: The key used for encryption ('string'). - opts: A variadic list of functional options for configuring the Options struct ('kit.Option[Options]'). Returns: - Options: The initialized Options struct.

type RegisteredClaims

type RegisteredClaims[T any] struct {
	Data                 T `json:"data"` // Custom data of type T associated with the registered claims.
	jwt.RegisteredClaims   // Embeds standard JWT registered claims.
}

RegisteredClaims is a generic struct that holds claims registered in a JWT, including user-defined data of type T.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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