token

package
v0.0.0-...-d9c52e5 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccessToken           TokenType  = "access"
	RefreshToken          TokenType  = "refresh"
	AuthEmailPassword     AuthMethod = "email_password"
	AuthPhone             AuthMethod = "phone"
	AuthGoogle            AuthMethod = "google"
	AuthApple             AuthMethod = "apple"
	DefaultNotBeforeDelay            = 15 * time.Minute
)

Variables

View Source
var (
	ErrInvalidToken        = errors.New("invalid token")
	ErrExpiredToken        = errors.New("access token has expired")
	ErrRefreshTokenExpired = errors.New("refresh token has expired")
	ErrTokenNotYetValid    = errors.New("token is not yet valid")
)

Functions

This section is empty.

Types

type AuthMethod

type AuthMethod string

type Maker

type Maker interface {
	// CreateToken creates a new token for a specific username and duration
	CreateToken(payloadData PayloadData, duration time.Duration, tokenType TokenType) (string, *Payload, error)

	// VerifyToken checks if the token is valid or not
	VerifyToken(ctx context.Context, cache cache.Cache, token string, tokenType TokenType) (*Payload, error)

	// Add a revoke endpoint
	RevokeTokenAccessToken(token string, ctx context.Context, store sqlc.Store, cache cache.Cache) error
}

func NewPasetoMaker

func NewPasetoMaker(accessKey, refreshKey string) (Maker, error)

type PasetoMaker

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

func (*PasetoMaker) CreateToken

func (maker *PasetoMaker) CreateToken(payloadData PayloadData, duration time.Duration, tokenType TokenType) (string, *Payload, error)

CreateToken implements Maker.

func (*PasetoMaker) RevokeTokenAccessToken

func (maker *PasetoMaker) RevokeTokenAccessToken(token string, ctx context.Context, store sqlc.Store, cache cache.Cache) error

func (*PasetoMaker) VerifyToken

func (maker *PasetoMaker) VerifyToken(ctx context.Context, cache cache.Cache, token string, tokenType TokenType) (*Payload, error)

type Payload

type Payload struct {
	PayloadData
	NotBefore time.Time `json:"nbf"` // not before
	Expires   time.Time `json:"exp"` // expiration time
	IssuedAt  time.Time `json:"iat"` // issued at
}

func NewPayload

func NewPayload(payload PayloadData, duration time.Duration) (*Payload, error)

func (*Payload) ValidateExpiry

func (payload *Payload) ValidateExpiry() error

type PayloadData

type PayloadData struct {
	Role          int8      `json:"role"`
	Subject       uuid.UUID `json:"sub"` // subject: the user ID
	Username      string    `json:"username,omitempty"`
	Email         string    `json:"email,omitempty"`
	Phone         string    `json:"phone,omitempty"`
	EmailVerified bool      `json:"email_verified"`
	SessionID     uuid.UUID `json:"session_id,omitempty"` // session ID is optional
	Issuer        string    `json:"iss"`                  // issuer
	Audience      string    `json:"aud"`                  // audience
	IP            string    `json:"ip"`                   // assuming IP is a string for simplicity
	UserAgent     string    `json:"user_agent"`
	MfaPassed     bool      `json:"mfa_passed"`
	TokenType     TokenType `json:"token_type"` // "access" or "refresh"

	PhoneVerified bool       `json:"phone_verified"` // TODO: Add to db
	DeviceID      string     `json:"device_id"`      // unique identifier for the device
	Platform      string     `json:"platform"`       // "android", "ios", "web", "desktop"
	OSVersion     string     `json:"os_version"`     // OS version of the device
	AppVersion    string     `json:"app_version"`    // version of your app
	AuthMethod    AuthMethod `json:"auth_method"`    // "email", "phone", "google", "apple"

}

type TokenType

type TokenType string

Jump to

Keyboard shortcuts

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