auth

package
v0.0.0-...-45f4925 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerHandler

func BearerHandler(verificationKey string) routing.Handler

BearerHandler returns a Bearer token-based authentication middleware.

func JWTHandler

func JWTHandler(verificationKey string) routing.Handler

JWTHandler returns a JWT-based authentication middleware.

func MockAuthHandler

func MockAuthHandler(c *routing.Context) error

MockAuthHandler creates a mock authentication middleware for testing purpose. If the request contains an Authorization header whose value is "TEST", then it considers the user is authenticated as "Tester" whose ID is "100". It fails the authentication otherwise.

func MockAuthHeader

func MockAuthHeader() http.Header

MockAuthHeader returns an HTTP header that can pass the authentication check by MockAuthHandler.

func RegisterHandlers

func RegisterHandlers(rg *routing.RouteGroup, service Service, logger log.Logger)

RegisterHandlers registers handlers for different HTTP requests.

func WithUser

func WithUser(ctx context.Context, id, name string) context.Context

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

Types

type Identity

type Identity interface {
	// GetID returns the user ID.
	GetID() string
	// GetName returns the user name.
	GetName() string
	// GetEmail returns the user email.
	GetEmail() string
}

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 Repository

type Repository interface {
	// GetUserByEmail passes the email to the database and returns the user
	GetUserByEmail(ctx context.Context, email string) (entity.User, error)
	// GetActiveUserByEmail passes the email to the database and returns the active user
	GetActiveUserByEmail(ctx context.Context, email string) (entity.User, error)
	// CreateUser stores a new user in the database
	CreateUser(ctx context.Context, user entity.User) error
}

Repository encapsulates the logic to access info from the data source.

func NewRepository

func NewRepository(db *dbcontext.DB, logger log.Logger) Repository

NewRepository creates a new auth repository

type Service

type Service interface {
	// authenticate authenticates a user using email and password.
	// It returns a JWT token if authentication succeeds. Otherwise, an error is returned.
	Login(ctx context.Context, email, password string) (string, error)
	// Register registers a new user.
	Register(ctx context.Context, name, email, password string) error
}

Service encapsulates the authentication logic.

func NewService

func NewService(repo Repository, signingKey string, tokenExpiration int, logger log.Logger) 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