security

package
v0.0.86 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 8 Imported by: 5

Documentation

Overview

Package security is a toolkit for security check and authorization

Package security is a toolkit for security check and authorization

Index

Constants

View Source
const (
	ErrInvalidToken = errors.String("invalid token")
	ErrNotFound     = errors.String("not found")
)
View Source
const (
	TokenNamespace = "security:jwt:token"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims added in v0.0.42

type Claims struct {
	ID        string                 `json:"jti,omitempty"`
	Issuer    string                 `json:"iss,omitempty"`
	Subject   string                 `json:"sub,omitempty"`
	Audience  []string               `json:"aud,omitempty"`
	ExpiresAt int64                  `json:"exp,omitempty"`
	NotBefore int64                  `json:"nbf,omitempty"`
	IssuedAt  int64                  `json:"iat,omitempty"`
	Custom    map[string]interface{} `json:"custom,omitempty"`
}

Claims defines basic Token claims

type Decoder added in v0.0.42

type Decoder interface {
	Decode(string) (string, bool)
}

type Encoder added in v0.0.42

type Encoder interface {
	Encode(args ...any) (string, error)
}

type Security added in v0.0.42

type Security interface {
	// GenerateToken Generate a JWT (JSON Web HeaderToken) with the provided subject.
	GenerateToken(ctx context.Context, subject string) (Token, error)
	// ValidateAccess Validate if a token is valid.
	ValidateAccess(ctx context.Context, tokenStr string) error
	// DestroyToken Invalidate a token by removing it from the token store.
	DestroyToken(ctx context.Context, tokenStr string) error
	// ParseSubject Parse the subject (or user identifier) from a given access token.
	ParseSubject(ctx context.Context, tokenStr string) (string, error)
	// Release any resources held by the authorize instance.
	Release(ctx context.Context) error
}

func NewSecurity added in v0.0.42

func NewSecurity(serializer TokenSerializer, ss ...Setting) Security

type Setting added in v0.0.42

type Setting = func(s *security)

func WithStorage added in v0.0.42

func WithStorage(store TokenStorage) Setting

type StorageOption added in v0.0.42

type StorageOption struct {
	Cache     cache.Cache
	Namespace string
}

StorageOption contains options for the JWT datacache

type StorageSetting added in v0.0.42

type StorageSetting = func(*StorageOption)

func WithCache added in v0.0.42

func WithCache(c cache.Cache) StorageSetting

func WithNamespace added in v0.0.42

func WithNamespace(ns string) StorageSetting

type Token

type Token struct {
	AccessToken  string    `json:"access_token"`
	TokenType    string    `json:"token_type,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	Expiry       time.Time `json:"expiry,omitempty"`
	ExpiresIn    int64     `json:"expires_in,omitempty"`
	Claims       *Claims   `json:"-"`
}

Token is an interface for getting token information

func (Token) Encode added in v0.0.42

func (t Token) Encode(args ...any) (string, error)

type TokenSerializer

type TokenSerializer interface {
	Generate(subject string, expires ...time.Duration) Token
	Parse(token string) (Token, error)
}

type TokenStorage added in v0.0.42

type TokenStorage interface {
	// Set stores the token with a specific expiration time
	Set(ctx context.Context, tokenStr string, expiration time.Duration) error
	// Delete deletes the token from the tokenStorage
	Delete(ctx context.Context, tokenStr string) error
	// Validate checks if the token exists in the tokenStorage
	Validate(ctx context.Context, tokenStr string) error
	// Close closes the tokenStorage
	Close(ctx context.Context) error
}

TokenStorage is the interface that tokenStorage the token.

func NewTokenStorage added in v0.0.42

func NewTokenStorage(ss ...StorageSetting) TokenStorage

NewTokenStorage creates a new TokenStorage with a Cache and optional StoreOptions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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