auth

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 5 Imported by: 0

README

📦 Package auth

Source Path: pkg/auth

🏷️ Variables

var ErrTokenExpired = errors.New("token expired")

🧩 Types

Manager
type Manager struct {
	secretKey []byte
}
Methods
GenerateToken

GenerateToken generates a new JWT token for the given user ID and audience.

The token is valid for 12 hours from the time it is generated. The subject claim is set to the given user ID. The audience claim is set to the given audience. The token is signed with the secret key set on the Manager during initialization.

If the token cannot be generated, an error is returned.

func (m *Manager) GenerateToken(userId int, audience string) (string, error)
ValidateToken

ValidateToken validates a JWT token and returns the user ID string if the validation is successful. If the token is invalid or expired, an error is returned. The error is wrapped with jwt.ErrTokenExpired if the token is expired. The error is wrapped with jwt.ErrTokenExpired if the token is expired.

func (m *Manager) ValidateToken(accessToken string) (string, error)
TokenManager
type TokenManager interface {
	GenerateToken func(userId int, audience string) (string, error)
	ValidateToken func(tokenString string) (string, error)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTokenExpired = errors.New("token expired")

ErrTokenExpired is an error that is returned when a JWT token is expired.

Functions

This section is empty.

Types

type Manager

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

func NewManager

func NewManager(secretKey string) (*Manager, error)

NewManager creates a new Manager instance with the given secret key.

The secret key is expected to be a non-empty string. If the secret key is empty, an error is returned.

func (*Manager) GenerateToken

func (m *Manager) GenerateToken(userId int, audience string) (string, error)

GenerateToken generates a new JWT token for the given user ID and audience.

The token is valid for 12 hours from the time it is generated. The subject claim is set to the given user ID. The audience claim is set to the given audience. The token is signed with the secret key set on the Manager during initialization.

If the token cannot be generated, an error is returned.

func (*Manager) ValidateToken

func (m *Manager) ValidateToken(accessToken string) (string, error)

ValidateToken validates a JWT token and returns the user ID string if the validation is successful. If the token is invalid or expired, an error is returned. The error is wrapped with jwt.ErrTokenExpired if the token is expired. The error is wrapped with jwt.ErrTokenExpired if the token is expired.

type TokenManager

type TokenManager interface {
	GenerateToken(userId int, audience string) (string, error)
	ValidateToken(tokenString string) (string, error)
}

Directories

Path Synopsis
Package mock_auth is a generated GoMock package.
Package mock_auth is a generated GoMock package.

Jump to

Keyboard shortcuts

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