auth

package
v0.0.0-...-3c7337f Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHash         = errors.New("the encoded hash is not in the correct format")
	ErrIncompatibleVersion = errors.New("incompatible version of argon2")
)

Functions

func CompareHash

func CompareHash(data string, encodedHash string) (bool, error)

func ComputeHash

func ComputeHash(data string) (*string, error)

func GenerateOTP

func GenerateOTP(length int) (*string, error)

func GenerateRefreshCookie

func GenerateRefreshCookie(value string) *fiber.Cookie

func GenerateURLSafeToken

func GenerateURLSafeToken(length int) (*string, error)

func SetResponseTokens

func SetResponseTokens(c *fiber.Ctx, tokens *Token) *errors.Error

func ValidatePassword

func ValidatePassword(password string) *errors.Error

Types

type Claims

type Claims struct {
	StandardClaims *jwt.StandardClaims
	CustomClaims   *jwt.MapClaims
}

type CustomClaims

type CustomClaims struct {
	jwt.StandardClaims
	Role string `json:"role"`
}

func From

func From(c *fiber.Ctx) (*CustomClaims, *errors.Error)

From extracts the CustomClaims from the fiber context Returns nil if the claims are not present

type JWTClient

type JWTClient struct {
	RefreshExp    time.Duration
	AccessExp     time.Duration
	RefreshKey    *m.Secret[string]
	AccessKey     *m.Secret[string]
	SigningMethod jwt.SigningMethod
}

func (*JWTClient) ExtractClaims

func (j *JWTClient) ExtractClaims(tokenString string, tokenType JWTType) (jwt.MapClaims, *errors.Error)

ExtractClaims extracts the claims from the token. It returns the claims if successful, otherwise it returns an error.

func (*JWTClient) GenerateToken

func (j *JWTClient) GenerateToken(claims Claims, tokenType JWTType) ([]byte, *errors.Error)

GenerateToken generates a token with the claims passed in. It returns the token if successful, otherwise it returns an error.

func (*JWTClient) GenerateTokenPair

func (j *JWTClient) GenerateTokenPair(accessClaims, refreshClaims Claims) (*Token, *errors.Error)

func (*JWTClient) IsTokenValid

func (j *JWTClient) IsTokenValid(tokenString string, tokenType JWTType) (bool, *errors.Error)

IsTokenValid checks if the token is valid. It returns true if the token is valid, otherwise it returns false.

func (*JWTClient) ParseToken

func (j *JWTClient) ParseToken(tokenString string, tokenType JWTType) (*jwt.Token, *errors.Error)

ParseToken parses the token string and returns the token if successful, otherwise it returns an error. It uses the secret key for the token type to parse the token.

func (*JWTClient) RefreshToken

func (j *JWTClient) RefreshToken(token, refreshToken string, tokenType JWTType, newClaims jwt.MapClaims) ([]byte, *errors.Error)

RefreshToken generates a new access token using the refresh token. It checks if the refresh token is valid and extracts the claims from the access token. It then updates the issued at and expires at claims and gives the new claims priority over the old claims. It returns the new access token if successful, otherwise it returns an error.

type JWTClientInterface

type JWTClientInterface interface {
	GenerateTokenPair(accessClaims, refreshClaims Claims) (*Token, *errors.Error)
	GenerateToken(claims Claims, tokenType JWTType) ([]byte, *errors.Error)
	RefreshToken(token, refreshToken string, tokenType JWTType, newClaims jwt.MapClaims) ([]byte, *errors.Error)
	ExtractClaims(tokenString string, tokenType JWTType) (jwt.MapClaims, *errors.Error)
	ParseToken(tokenString string, tokenType JWTType) (*jwt.Token, *errors.Error)
	IsTokenValid(tokenString string, tokenType JWTType) (bool, *errors.Error)
}

func NewJWTClient

func NewJWTClient(authSettings config.AuthSettings, signingMethod jwt.SigningMethod) JWTClientInterface

func NewJWTMockClient

func NewJWTMockClient() JWTClientInterface

type JWTMockClient

type JWTMockClient struct{}

func (*JWTMockClient) ExtractClaims

func (c *JWTMockClient) ExtractClaims(tokenString string, tokenType JWTType) (jwt.MapClaims, *errors.Error)

func (*JWTMockClient) GenerateToken

func (c *JWTMockClient) GenerateToken(claims Claims, tokenType JWTType) ([]byte, *errors.Error)

func (*JWTMockClient) GenerateTokenPair

func (c *JWTMockClient) GenerateTokenPair(accessClaims, refreshClaims Claims) (*Token, *errors.Error)

func (*JWTMockClient) IsTokenValid

func (c *JWTMockClient) IsTokenValid(tokenString string, tokenType JWTType) (bool, *errors.Error)

func (*JWTMockClient) ParseToken

func (c *JWTMockClient) ParseToken(tokenString string, tokenType JWTType) (*jwt.Token, *errors.Error)

func (*JWTMockClient) RefreshToken

func (c *JWTMockClient) RefreshToken(token, refreshToken string, tokenType JWTType, newClaims jwt.MapClaims) ([]byte, *errors.Error)

type JWTType

type JWTType string
const (
	AccessToken  JWTType = "access"
	RefreshToken JWTType = "refresh"
)

type Permission

type Permission string
const (
	// User Management
	UserRead          Permission = "user:read"
	UserWrite         Permission = "user:write"
	UserDelete        Permission = "user:delete"
	UserManageProfile Permission = "user:manage_profile"
	UserReadAll       Permission = "user:read_all"

	// Tag Management
	TagRead   Permission = "tag:read"
	TagCreate Permission = "tag:create"
	TagWrite  Permission = "tag:write"
	TagDelete Permission = "tag:delete"

	// Club Management
	ClubRead            Permission = "club:read"
	ClubCreate          Permission = "club:create"
	ClubWrite           Permission = "club:write"
	ClubDelete          Permission = "club:delete"
	ClubManageMembers   Permission = "club:manage_members"
	ClubManageFollowers Permission = "club:manage_followers"

	// Point of Contact Management
	PointOfContactRead   Permission = "pointOfContact:read"
	PointOfContactCreate Permission = "pointOfContact:create"
	PointOfContactWrite  Permission = "pointOfContact:write"
	PointOfContactDelete Permission = "pointOfContact:delete"

	// Comment Management
	CommentRead   Permission = "comment:read"
	CommentCreate Permission = "comment:create"
	CommentWrite  Permission = "comment:write"
	CommentDelete Permission = "comment:delete"

	// Event Management
	EventRead        Permission = "event:read"
	EventCreate      Permission = "event:create"
	EventWrite       Permission = "event:write"
	EventDelete      Permission = "event:delete"
	EventManageRSVPs Permission = "event:manage_rsvps"

	// Contact Management
	ContactRead   Permission = "contact:read"
	ContactCreate Permission = "contact:create"
	ContactWrite  Permission = "contact:write"
	ContactDelete Permission = "contact:delete"

	// Category Management
	CategoryRead   Permission = "category:read"
	CategoryCreate Permission = "category:create"
	CategoryWrite  Permission = "category:write"
	CategoryDelete Permission = "category:delete"

	// Notification Management
	NotificationRead   Permission = "notification:read"
	NotificationCreate Permission = "notification:create"
	NotificationWrite  Permission = "notification:write"
	NotificationDelete Permission = "notification:delete"

	// Global Permissions (for convenience)
	ReadAll   Permission = "all:read"
	CreateAll Permission = "all:create"
	WriteAll  Permission = "all:write"
	DeleteAll Permission = "all:delete"
)

func GetPermissions

func GetPermissions(role models.UserRole) []Permission

type Token

type Token struct {
	AccessToken  []byte
	RefreshToken []byte
}

Jump to

Keyboard shortcuts

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