Documentation
¶
Index ¶
- Constants
- Variables
- func AddHttpCode() errhttp.ErrorProcessor
- func GetPerformerID(ctx context.Context) uuid.UUID
- func GetRefreshToken(ctx context.Context) string
- func NewManifesto() module.Manifesto
- func NewModule(options ...module.Option) *module.Module
- func OverrideAccountRepository[T repository.AccountRepository](authModule *module.Module) *module.Module
- func OverrideCredentialRepository[T repository.CredentialRepository](authModule *module.Module) *module.Module
- func OverrideIdentityRepository[T repository.IdentityRepository](authModule *module.Module) *module.Module
- func OverrideMiddlewareAuthenticator[T Authenticator](authModule *module.Module) *module.Module
- func OverrideResetPasswordRequestRepository[T repository.ResetPasswordRequestRepository](authModule *module.Module) *module.Module
- func OverrideTokenHashStrategy[T hash.TokenHashStrategy](authModule *module.Module) *module.Module
- func OverrideTokenRepository[T repository.TokenRepository](authModule *module.Module) *module.Module
- func RemoveRefreshToken(ctx context.Context)
- func SendRefreshToken(ctx context.Context, token string)
- func WithPerformer(ctx context.Context, performer Performer) context.Context
- func WithRefreshToken(ctx context.Context, refreshToken string) context.Context
- type Authenticator
- type Middleware
- type MiddlewareConfig
- type ModuleConfig
- type PasswordAuthenticator
- func (a *PasswordAuthenticator) Authenticate(ctx context.Context, identity, password string) (Performer, error)
- func (a *PasswordAuthenticator) Register(ctx context.Context, identity, password string, ...) (repository.Account, error)
- func (a *PasswordAuthenticator) RemoveIdentity(ctx context.Context, identity string) error
- type Performer
- type PlainTokenAuthenticator
- func (a *PlainTokenAuthenticator) Authenticate(ctx context.Context, token string) (Performer, error)
- func (a *PlainTokenAuthenticator) IssueNewAccessToken(ctx context.Context, refreshToken string, ...) (repository.AccessToken, error)
- func (a *PlainTokenAuthenticator) IssueTokens(ctx context.Context, identityID uuid.UUID, ...) (TokenPair, error)
- func (a *PlainTokenAuthenticator) RefreshAccessToken(ctx context.Context, refreshToken string, ...) (repository.AccessToken, error)
- type RefreshTokenConfig
- type TokenPair
Constants ¶
const TagUnauthenticated = "unauthenticated"
Variables ¶
var ErrCannotCreateAccessToken = errors.New("cannot create access token")
var ErrCannotCreateRefreshToken = errors.New("cannot create refresh token")
var ErrCannotHashPassword = errors.New("cannot hash password")
var ErrIdentityIsBlocked = errors.New("identity is blocked")
var ErrInvalidIdentity = errors.New("invalid identity")
var ErrInvalidPassword = errors.New("invalid password")
var ErrInvalidToken = translation.WithDomain( erruser.New("invalid access token", "Please provide a valid access token"), locales.Domain, )
var ErrTokenIsExpired = errors.New("token is expired")
var ErrTokenIsRevoked = errors.New("token is revoked")
var ErrUnauthenticated = translation.WithDomain( errors.WithAddedTags( erruser.New("unauthenticated", localize.Singular("Please authenticate to get access to this resource")), TagUnauthenticated, ), locales.Domain, )
errors.WithAddedTags( erruser.New("unauthorized", "You are not authorized to access this resource"), TagUnauthorized, ), locales.Domain, )
Functions ¶
func AddHttpCode ¶
func AddHttpCode() errhttp.ErrorProcessor
func GetRefreshToken ¶
func NewManifesto ¶
func NewModule ¶
NewModule creates a new module for the auth package. It works with the default storage implementation. It uses pgxpool for database connection.
If you want to use a custom identity storage implementation, you should implement the IdentityRepository interface and call authModule := auth.OverrideIdentityRepository(auth.NewModule(), NewYourIdentityRepositoryImplementation). The same is for other storage implementations if you need it.
func OverrideAccountRepository ¶
func OverrideAccountRepository[T repository.AccountRepository](authModule *module.Module) *module.Module
OverrideAccountRepository overrides the default account storage implementation with the custom one. repository should be a constructor returning the implementation of the AccountRepository interface.
func OverrideCredentialRepository ¶
func OverrideCredentialRepository[T repository.CredentialRepository](authModule *module.Module) *module.Module
OverrideCredentialRepository overrides the default credential storage implementation with the custom one. repository should be a constructor returning the implementation of the CredentialRepository interface.
func OverrideIdentityRepository ¶
func OverrideIdentityRepository[T repository.IdentityRepository](authModule *module.Module) *module.Module
OverrideIdentityRepository overrides the default identity storage implementation with the custom one. repository should be a constructor returning the implementation of the IdentityRepository interface.
func OverrideMiddlewareAuthenticator ¶
func OverrideMiddlewareAuthenticator[T Authenticator](authModule *module.Module) *module.Module
OverrideMiddlewareAuthenticator overrides the default middleware authenticator with the custom one. authenticator should be a constructor returning the implementation of the Authenticator interface.
func OverrideResetPasswordRequestRepository ¶
func OverrideResetPasswordRequestRepository[T repository.ResetPasswordRequestRepository](authModule *module.Module) *module.Module
OverrideResetPasswordRequestRepository overrides the default reset password request storage implementation with the custom one.
func OverrideTokenHashStrategy ¶
OverrideTokenHashStrategy overrides the default token hash strategy with the custom one. strategy should be a constructor returning the implementation of the hash.TokenHashStrategy interface. by default, the sha1 hash strategy is used. if you don't want to hash tokens, you can set the strategy to none, like this auth.OverrideTokenHashStrategy[*hash.None](authModule)
func OverrideTokenRepository ¶
func OverrideTokenRepository[T repository.TokenRepository](authModule *module.Module) *module.Module
OverrideTokenRepository overrides the default token storage implementation with the custom one. repository should be a constructor returning the implementation of the TokenRepository interface.
func RemoveRefreshToken ¶
func SendRefreshToken ¶
Types ¶
type Authenticator ¶
type Authenticator interface {
// Authenticate authenticates the user with the given token.
// It returns the performer of the authenticated user.
//
// Errors:
// * github.com/go-modulus/auth.ErrTokenIsRevoked - if the token is revoked.
// * github.com/go-modulus/auth.ErrTokenIsExpired - if the token is expired.
Authenticate(ctx context.Context, token string) (Performer, error)
}
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
func NewMiddleware ¶
func NewMiddleware( authenticator Authenticator, config *MiddlewareConfig, errorPipeline *errhttp.ErrorPipeline, ) *Middleware
func (*Middleware) HttpMiddleware ¶
func (a *Middleware) HttpMiddleware() func(http.Handler) http.Handler
func (*Middleware) Middleware ¶
func (a *Middleware) Middleware(next http.Handler) errhttp.Handler
type MiddlewareConfig ¶
type MiddlewareConfig struct {
RefreshTokenConfig
}
func NewMiddlewareConfig ¶
func NewMiddlewareConfig() (*MiddlewareConfig, error)
type ModuleConfig ¶
type PasswordAuthenticator ¶
type PasswordAuthenticator struct {
// contains filtered or unexported fields
}
func NewPasswordAuthenticator ¶
func NewPasswordAuthenticator( accountRepository repository.AccountRepository, identityRepository repository.IdentityRepository, credentialRepository repository.CredentialRepository, ) *PasswordAuthenticator
func (*PasswordAuthenticator) Authenticate ¶
func (a *PasswordAuthenticator) Authenticate(ctx context.Context, identity, password string) (Performer, error)
Authenticate authenticates the user with the given identity and password. It returns the performer of the authenticated user.
Errors: * github.com/go-modulus/auth.ErrIdentityIsBlocked - if the identity is blocked. * github.com/go-modulus/auth.ErrInvalidPassword - if the password is invalid. * github.com/go-modulus/auth.ErrInvalidIdentity - if identity is not found in the repository.
func (*PasswordAuthenticator) Register ¶
func (a *PasswordAuthenticator) Register( ctx context.Context, identity, password string, identityType repository.IdentityType, roles []string, userInfo map[string]interface{}, ) (repository.Account, error)
Register registers a new user account with the given identity and password. In the userInfo, you can pass any additional data you want to store (e.g. IP, unregistered user token from frontend, name, date of birth, etc.). It returns the performer of the registered user.
Errors: * github.com/go-modulus/auth.ErrIdentityExists - if you try to register a user for the same identity. * github.com/go-modulus/auth.ErrIdentityIsBlocked - if the identity exists in the storage, and it has status blocked. * Any error from the IdentityRepository.Create method. * Any error from the CredentialRepository.Create method.
func (*PasswordAuthenticator) RemoveIdentity ¶
func (a *PasswordAuthenticator) RemoveIdentity(ctx context.Context, identity string) error
type Performer ¶
func GetPerformer ¶
type PlainTokenAuthenticator ¶
type PlainTokenAuthenticator struct {
// contains filtered or unexported fields
}
func NewPlainTokenAuthenticator ¶
func NewPlainTokenAuthenticator( accountRepository repository.AccountRepository, tokenRepository repository.TokenRepository, identityRepository repository.IdentityRepository, config ModuleConfig, ) *PlainTokenAuthenticator
func (*PlainTokenAuthenticator) Authenticate ¶
func (a *PlainTokenAuthenticator) Authenticate(ctx context.Context, token string) (Performer, error)
Authenticate authenticates the user with the given token. It returns the performer of the authenticated user.
Errors: * github.com/go-modulus/auth.ErrTokenIsRevoked - if the token is revoked. * github.com/go-modulus/auth.ErrTokenIsExpired - if the token is expired.
func (*PlainTokenAuthenticator) IssueNewAccessToken ¶
func (a *PlainTokenAuthenticator) IssueNewAccessToken( ctx context.Context, refreshToken string, additionalData map[string]interface{}, ) (repository.AccessToken, error)
IssueNewAccessToken issues the new access token linking it to the session from the given refresh token. It returns a new access token. Refresh token is not revoked. Old access token is not revoked. The session is not changed.
Errors: * ErrTokenIsRevoked - if the refresh token is revoked. * ErrTokenIsExpired - if the refresh token expired. * ErrCannotCreateAccessToken - if the access token cannot be created. * ErrCannotCreateRefreshToken - if the refresh token cannot be created.
func (*PlainTokenAuthenticator) IssueTokens ¶
func (a *PlainTokenAuthenticator) IssueTokens( ctx context.Context, identityID uuid.UUID, additionalData map[string]interface{}, ) (TokenPair, error)
IssueTokens starts a new session for the given performer. It means creation the new pair of access and refresh tokens without revoking any existing tokens. It returns an access token and a refresh token.
The additionalData parameter is used to store additional data in the access token. For example, you can store the IP address of the user.
Errors: * ErrCannotCreateAccessToken - if the access token cannot be created. * ErrCannotCreateRefreshToken - if the refresh token cannot be created. * repository.ErrIdentityNotFound - if the identity does not exist. * repository.ErrCannotCreateAccessToken - if there are some issues with DB to create a token * repository.ErrCannotCreateRefreshToken - if there are some issues with DB to create a token
func (*PlainTokenAuthenticator) RefreshAccessToken ¶
func (a *PlainTokenAuthenticator) RefreshAccessToken( ctx context.Context, refreshToken string, additionalData map[string]interface{}, expirationLag time.Duration, ) (repository.AccessToken, error)
RefreshAccessToken refreshes the access token with the given refresh token. It returns a new access token. Refresh token is not revoked. Old access token expires. The session is not changed.
Errors: * ErrTokenIsRevoked - if the refresh token is revoked. * ErrTokenIsExpired - if the refresh token expired. * ErrCannotCreateAccessToken - if the access token cannot be created. * ErrCannotCreateRefreshToken - if the refresh token cannot be created.
type RefreshTokenConfig ¶
type RefreshTokenConfig struct {
CookieName string `env:"REFRESH_TOKEN_COOKIE_NAME, default=art"`
CookieDomain string `env:"REFRESH_TOKEN_COOKIE_DOMAIN, default=localhost"`
CookieSecure bool `env:"REFRESH_TOKEN_COOKIE_SECURE, default=false"`
TTL time.Duration `env:"AUTH_REFRESH_TOKEN_TTL, default=8760h"`
}
type TokenPair ¶
type TokenPair struct {
AccessToken repository.AccessToken
RefreshToken repository.RefreshToken
}