domain

package
v0.0.0-...-20ebf41 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccessTokenExpiration       string = "accessTokenExpiration"
	AnonymousExpiration         string = "anonymousExpiration"
	ShortRefreshTokenExpiration string = "shortRefreshTokenExpiration"
	LongRefreshTokenExpiration  string = "longRefreshTokenExpiration"
)

Expiration map valid keys.

Variables

View Source
var (
	ErrInternalError = errors.New("internal error")
	ErrUnauthorized  = errors.New("unauthorized")
)

Common errors.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	// GenerateAnonymousTokens generate authentication tokens for anonymous user.
	GenerateAnonymousTokens(userID string, flow FlowType) (*Tokens, error)
	// GenerateUserTokens generate authentication tokens for logged-in user.
	GenerateUserTokens(username, password string, flow FlowType) (*Tokens, error)
	// Revoke revokes an access token registered for a given refresh token.
	Revoke(accessToken string) error
	// Refresh refreshes an user authentication tokens.
	Refresh(refreshToken string) (*Tokens, error)
	// ValidateAccessToken checks if logged-in user authentication token exists.
	ValidateAccessToken(accessToken string) (*Claims, error)
	// JWTKey returns the authenticator JWT Keys.
	JWTKey() *rsa.PrivateKey
}

Authenticator is the interface for the authentication methods.

type Claims

type Claims struct {
	Name  string `json:"name,omitempty"`
	Level Level  `json:"level"`
	jwt.RegisteredClaims
}

Claims represents JWT claims data structure.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements the Authenticator interface.

func NewClient

func NewClient(
	logger *log.Logger,
	userServiceClient UserServiceClient,
	refreshTokenRepository,
	accessTokenRepository,
	flowRepository Repository,
	issuer string,
	expiration map[string]time.Duration,
	jwtKey *rsa.PrivateKey,
) *Client

NewClient returns a new instance of authentication Client.

func (*Client) GenerateAnonymousTokens

func (c *Client) GenerateAnonymousTokens(userID string, flow FlowType) (*Tokens, error)

GenerateAnonymousTokens generate authentication tokens for anonymous user.

func (*Client) GenerateUserTokens

func (c *Client) GenerateUserTokens(username, password string, flow FlowType) (*Tokens, error)

GenerateUserTokens generate authentication tokens for logged-in user.

func (*Client) JWTKey

func (c *Client) JWTKey() *rsa.PrivateKey

func (*Client) Refresh

func (c *Client) Refresh(refreshToken string) (*Tokens, error)

Refresh refreshes logged-in/anonymous user authentication token.

func (*Client) Revoke

func (c *Client) Revoke(accessToken string) error

Revoke revokes an access token registered for a given refresh token.

func (*Client) ValidateAccessToken

func (c *Client) ValidateAccessToken(accessToken string) (*Claims, error)

ValidateAccessToken checks if logged-in user authentication token exists.

type FlowType

type FlowType string

FlowType represents login flow type.

const (
	WebsiteSessionFlow FlowType = "websiteSession"
	RememberMeFlow     FlowType = "rememberMe"
)

Valid FlowType values as constants.

type Level

type Level string

Level represents the user's access level.

const (
	AdminLevel     Level = "admin"
	UserLevel      Level = "user"
	AnonymousLevel Level = "anonymous"
)

Valid Level values as constants.

type Repository

type Repository interface {
	// Keys retrieves keys matching the specified pattern.
	Keys(ctx context.Context, pattern string) ([]string, error)
	// Get retrieves the value associated with the specified key.
	Get(ctx context.Context, key string) (string, error)
	// Set sets the value associated with the specified key with an optional expiration duration.
	Set(ctx context.Context, key, value string, expiration time.Duration) error
	// Del deletes the value associated with the specified key.
	Del(ctx context.Context, key string) error
}

Repository is the interface for the authenticator storage repository.

type Tokens

type Tokens struct {
	RefreshToken           string `json:"refreshToken,omitempty"`
	AccessToken            string `json:"accessToken"`
	AccessTokenExpiration  int64  `json:"accessTokenExpiration"`
	RefreshTokenExpiration int64  `json:"refreshTokenExpiration,omitempty"`
}

Tokens represents the authentication token json response.

type User

type User struct {
	Username string `json:"username"`
	Name     string `json:"name"`
	Level    Level  `json:"level"`
}

User represents a user entity.

type UserServiceClient

type UserServiceClient interface {
	// CheckCredentials checks the credentials of a user.
	CheckCredentials(username, md5Password string) (*User, error)
}

UserServiceClient is an interface for interacting with the user service.

func NewUserServiceClient

func NewUserServiceClient(timeout time.Duration, endpoint string, logger *log.Logger) UserServiceClient

NewUserServiceClient creates a new instance of the user service client.

Jump to

Keyboard shortcuts

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