auth

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCredentials  = server.NewHTTPError(http.StatusUnauthorized, "INVALID_CREDENTIALS", "Username or password is incorrect")
	ErrUserBlocked         = server.NewHTTPError(http.StatusUnauthorized, "USER_BLOCKED", "Your account has been blocked and may not login")
	ErrInvalidRefreshToken = server.NewHTTPError(http.StatusUnauthorized, "INVALID_REFRESH_TOKEN", "Invalid refresh token")
)

Custom errors

Functions

func NewHTTP

func NewHTTP(svc Service, e *echo.Echo)

NewHTTP creates new auth http service

Types

type Auth

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

Auth represents auth application service

func New

func New(db *gorm.DB, udb UserDB, jwt JWT, cr Crypter) *Auth

New creates new auth service

func (*Auth) Authenticate

func (s *Auth) Authenticate(ctx context.Context, data Credentials) (*model.AuthToken, error)

Authenticate tries to authenticate the user provided by given credentials

func (*Auth) LoginUser

func (s *Auth) LoginUser(ctx context.Context, u *model.User) (*model.AuthToken, error)

LoginUser logs in the given user, returns access token

func (*Auth) RefreshToken

func (s *Auth) RefreshToken(ctx context.Context, data RefreshTokenData) (*model.AuthToken, error)

RefreshToken returns the new access token with expired time extended

func (*Auth) User

func (s *Auth) User(c echo.Context) *model.AuthUser

User returns user data stored in jwt token

type Credentials

type Credentials struct {
	Username string `json:"username" validate:"required" example:"superadmin"`
	Password string `json:"password" validate:"required" example:"superadmin123!@#"`
}

Credentials represents login request data

type Crypter

type Crypter interface {
	CompareHashAndPassword(string, string) bool
	UID() string
}

Crypter represents security interface

type HTTP

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

HTTP represents auth http service

type JWT

type JWT interface {
	GenerateToken(map[string]interface{}, *time.Time) (string, int, error)
}

JWT represents token generator (jwt) interface

type RefreshTokenData

type RefreshTokenData struct {
	RefreshToken string `json:"refresh_token" validate:"required"`
}

RefreshTokenData represents refresh token request data

type Service

type Service interface {
	Authenticate(context.Context, Credentials) (*model.AuthToken, error)
	RefreshToken(context.Context, RefreshTokenData) (*model.AuthToken, error)
}

Service represents auth service interface

type UserDB

type UserDB interface {
	dbutil.Intf
	FindByUsername(context.Context, *gorm.DB, string) (*model.User, error)
	FindByRefreshToken(context.Context, *gorm.DB, string) (*model.User, error)
}

UserDB represents user repository interface

Jump to

Keyboard shortcuts

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