auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWrongCredentials    = errors.New("wrong username or password")
	ErrInvalidRefreshToken = errors.New("invalid refresh token")
	ErrSessionNotFound     = errors.New("session is not found")
)

Functions

This section is empty.

Types

type CheckPasswordFunc

type CheckPasswordFunc func(ctx context.Context, user, password string) (userID string, ok bool, err error)

type Credentials

type Credentials struct {
	Username    string
	Password    string
	Fingerprint string
}

type EnrichClaimsFunc

type EnrichClaimsFunc func(claims token.Claims)

type Logger

type Logger interface {
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
}

type Meta

type Meta struct {
	IP net.IP
}

type MetaOption

type MetaOption func(meta *Meta)

func WithIP

func WithIP(ip net.IP) MetaOption

type RefreshSession

type RefreshSession struct {
	RefreshToken string
	Fingerprint  string
}

type Service

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

func NewService

func NewService(storage SessionStorage, opts ...ServiceOption) *Service

func (*Service) Login

func (s *Service) Login(ctx context.Context, cred Credentials, opts ...MetaOption) (TokenPair, error)

func (*Service) Logout

func (s *Service) Logout(ctx context.Context, refreshToken string) error

func (*Service) RefreshSession

func (s *Service) RefreshSession(ctx context.Context, rs RefreshSession, opts ...MetaOption) (TokenPair, error)

type ServiceConfig

type ServiceConfig struct {
	AccessTokenTTL  time.Duration
	RefreshTokenTTL time.Duration
	Issuer          string
	SignedKey       any
	CheckPassword   CheckPasswordFunc
	EnrichClaims    EnrichClaimsFunc
	Logger          Logger
}

type ServiceOption

type ServiceOption func(conf *ServiceConfig)

func WithAccessTokenTTL

func WithAccessTokenTTL(d time.Duration) ServiceOption

func WithCheckPassword

func WithCheckPassword(fn CheckPasswordFunc) ServiceOption

func WithEnrichClaims

func WithEnrichClaims(fn EnrichClaimsFunc) ServiceOption

func WithIssuer

func WithIssuer(iss string) ServiceOption

func WithLogger

func WithLogger(logger Logger) ServiceOption

func WithRefreshTokenTTL

func WithRefreshTokenTTL(d time.Duration) ServiceOption

func WithSignedKey

func WithSignedKey(key any) ServiceOption

type Session

type Session struct {
	UserID       string
	RefreshToken string
	Fingerprint  string
	IP           net.IP
	ExpiresAt    time.Time
	CreatedAt    time.Time
}

type SessionStorage

type SessionStorage interface {
	Set(ctx context.Context, sess Session) error
	GetWithDelete(ctx context.Context, refreshToken string) (Session, error)
}

type TokenPair

type TokenPair struct {
	AccessToken  string
	RefreshToken string
}

Directories

Path Synopsis
storage
transport

Jump to

Keyboard shortcuts

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