auth

package
v0.0.0-...-d6d061b Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: BSD-3-Clause Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIKeyAuthMiddleware

func APIKeyAuthMiddleware(next httputils.HandlerFunc, svc Service) httputils.HandlerFunc

APIKeyAuthMiddleware authenticates user using provided API Key. If authentication fails, it returns 401. If authentication is successful, it sets User UUID in context.

func JWTAuthMiddleware

func JWTAuthMiddleware(next httputils.HandlerFunc, secretKey string) httputils.HandlerFunc

JWTAuthMiddleware parses and validates JWT from authorization header. If authentication fails, it returns 401. If authentication is successful, it sets User UUID in context.

func NewRepository

func NewRepository() *repository

NewRepository returns a new repository.

func NewService

func NewService(
	config *env.Config,
	dbPool *pgxpool.Pool,
	logger logging.Logger,
	mailClient mailclient.Client,
	tmplManager templatesmanager.Manager,
	repo Repository,
) *service

NewService returns a new service.

Types

type APIKey

type APIKey struct {
	ID        int        `db:"id"`
	UserUUID  string     `db:"user_uuid"`
	Prefix    string     `db:"prefix"`
	HashedKey string     `db:"hashed_key"`
	Name      string     `db:"name"`
	CreatedAt time.Time  `db:"created_at"`
	ExpiresAt *time.Time `db:"expires_at"`
}

APIKey represents database table of API keys.

type AuthContextKey

type AuthContextKey string

AuthContextKey is a string representing context keys.

const AuthContextKeyUserUUID AuthContextKey = "userUUID"

AuthContextKeyUserUUID is the key in context where User UUID is stored after authentication.

type JWTType

type JWTType string

JWTType is a string representing type of JWT. Allowed strings are "access", "refresh", and "activation".

const (
	JWTTypeAccess     JWTType = "access"
	JWTTypeRefresh    JWTType = "refresh"
	JWTTypeActivation JWTType = "activation"
)

type Repository

type Repository interface {
	CreateUser(dbConn *pgxpool.Conn, user *User) (*User, error)
	ActivateUserByUUID(dbConn *pgxpool.Conn, userUUID string) error
	GetUserByEmail(dbConn *pgxpool.Conn, email string) (*User, error)
	GetUserByUUID(dbConn *pgxpool.Conn, userUUID string) (*User, error)
	UpdateUser(dbConn *pgxpool.Conn, userUUID string, firstName *string, lastName *string) (*User, error)
	CreateAPIKey(dbConn *pgxpool.Conn, apiKey *APIKey) (*APIKey, error)
	ListAPIKeysByUserUUID(dbConn *pgxpool.Conn, userUUID string) ([]*APIKey, error)
	ListActiveAPIKeysByPrefix(dbConn *pgxpool.Conn, prefix string) ([]*APIKey, error)
	UpdateAPIKey(dbConn *pgxpool.Conn, apiKeyID int, userUUID string, name *string, expiresAt jsonutils.Optional[time.Time]) (*APIKey, error)
	DeleteAPIKey(dbConn *pgxpool.Conn, apiKeyID int, userUUID string) error
}

Repository is used to access and update auth data.

type Service

type Service interface {
	CreateUser(ctx context.Context, wg *sync.WaitGroup, email string, password string, firstName string, lastName string) (*User, error)
	ActivateUser(ctx context.Context, token string) error
	GetCurrentUser(ctx context.Context) (*User, error)
	UpdateUser(ctx context.Context, firstName *string, lastName *string) (*User, error)
	CreateJWT(ctx context.Context, email string, password string) (string, string, error)
	RefreshJWT(ctx context.Context, token string) (string, error)
	CreateAPIKey(ctx context.Context, name string, expiresAt *time.Time) (*APIKey, string, error)
	ListAPIKeys(ctx context.Context) ([]*APIKey, error)
	FindAPIKey(ctx context.Context, rawKey string) (*APIKey, error)
	DeleteAPIKey(ctx context.Context, apiKeyID int) error
}

Service performs all auth-related business logic.

type User

type User struct {
	UUID        string    `db:"uuid"`
	Email       string    `db:"email"`
	Password    string    `db:"password"`
	FirstName   string    `db:"first_name"`
	LastName    string    `db:"last_name"`
	IsActive    bool      `db:"is_active"`
	IsSuperUser bool      `db:"is_superuser"`
	CreatedAt   time.Time `db:"created_at"`
}

User represents database table of users.

Jump to

Keyboard shortcuts

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