auth

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(verificationKey string) echo.MiddlewareFunc

Handler returns a JWT-based authentication middleware.

func IsAuthenticated

func IsAuthenticated(authHandler echo.MiddlewareFunc) echo.MiddlewareFunc

IsAuthenticated middleware checks if a user is authenticated. If not, it calls the next handler HTTP. If yes, it validates the JWT token and returns an error if token is invalid.

func RegisterHandlers

func RegisterHandlers(g *echo.Group, service Service, logger log.Logger,
	mailer Mailer, templater tpl.Service, UIAddress string)

RegisterHandlers registers handlers for different HTTP requests.

func WithSource added in v0.4.0

func WithSource(ctx context.Context, src string) context.Context

WithSource returns a context that contains the source of the HTTP request.

func WithUser

func WithUser(ctx context.Context, id string, isAdmin bool) context.Context

WithUser returns a context that contains the user identity from the given JWT.

Types

type Identity

type Identity interface {
	// ID returns the user ID.
	ID() string
	// IsAdmin return true if the user have admin privileges.
	IsAdmin() bool
}

Identity represents an authenticated user identity.

func CurrentUser

func CurrentUser(ctx context.Context) Identity

CurrentUser returns the user identity from the given context. Nil is returned if no user identity is found in the context.

type LoginResponse added in v0.5.0

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

type Mailer added in v0.3.0

type Mailer interface {
	Send(body, subject, from, to string) error
}

Mailer represents the mailer interface/

type ResendConfirmationResponse added in v0.4.0

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

type ResetPasswordResponse added in v0.3.0

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

type Service

type Service interface {
	// Login authenticates a user using username or email and a password.
	// It returns a JWT token if authentication succeeds. Otherwise, an error is returned.
	Login(ctx context.Context, usernameOrEmail, password string) (LoginResponse, error)
	// reset password generates a password reset token. The hash of the token
	// is stored in the database, a GUID is also generated to retrieve the
	// document when the user send the new password from the html form.
	ResetPassword(ctx context.Context, email string) (ResetPasswordResponse, error)
	// VerifyAccount confirms the user account by verifying the token.
	VerifyAccount(ctx context.Context, id, token string) error
	// create a new password if the user has already a reset password token.
	CreateNewPassword(ctx context.Context, id, token, password string) error
	// resend a new confirmation email for the user's account.
	ResendConfirmation(ctx context.Context, email string) (ResendConfirmationResponse, error)
}

Service encapsulates the authentication logic.

func NewService

func NewService(signingKey string, tokenExpiration int,
	logger log.Logger, sec secure.Password, userSvc user.Service,
	tokenGen secure.TokenGenerator) Service

NewService creates a new authentication service.

Jump to

Keyboard shortcuts

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