security

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argon2idHasher

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

Argon2idHasher implements PasswordHasher using argon2id.

func NewArgon2idHasher

func NewArgon2idHasher(memory uint32, iterations uint32, parallelism uint8, saltLength int, keyLength uint32) *Argon2idHasher

NewArgon2idHasher creates a new Argon2id hasher with the given parameters. memory: memory usage in KiB. iterations: number of iterations to perform. parallelism: number of parallel threads to use. saltLength: length of the salt to use. keyLength: length of the generated key to use.

func (*Argon2idHasher) Compare

func (h *Argon2idHasher) Compare(hashPassword, password string) error

Compare checks if the password matches the hash. Expects the hash to be in the format generated by Hash().

func (*Argon2idHasher) Hash

func (h *Argon2idHasher) Hash(password string) (string, error)

Hash generates an argon2id hash of the password. The returned string is in the format: $argon2id$v=19$m=<memory>,t=<iterations>,p=<parallelism>$<salt>$<hash> where salt and hash are base64 encoded.

type BcryptHasher

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

BcryptHasher implements PasswordHasher using bcrypt.

func NewBcryptHasher

func NewBcryptHasher(cost int) *BcryptHasher

NewBcryptHasher creates a new bcrypt hasher with the given cost. If cost is less than bcrypt.MinCost, it will be set to bcrypt.DefaultCost.

func (*BcryptHasher) Compare

func (h *BcryptHasher) Compare(hash, password string) error

Compare checks if the password matches the hash.

func (*BcryptHasher) Hash

func (h *BcryptHasher) Hash(password string) (string, error)

Hash generates a bcrypt hash of the password.

type JWTKeyProvider

type JWTKeyProvider interface {
	// GetKey returns the key material for the given key ID.
	GetKey(keyID string) ([]byte, error)
}

JWTKeyProvider provides signing keys for JWT tokens by key ID.

type JWTTokenService

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

JWTTokenService implements token issuance and validation using a JWTKeyProvider.

func NewJWTTokenService

func NewJWTTokenService(keyProvider JWTKeyProvider, cfg config.JWTConfig) *JWTTokenService

NewJWTTokenService creates a new JWTTokenService.

func (*JWTTokenService) IssueAccessToken

func (s *JWTTokenService) IssueAccessToken(ctx context.Context, subject string, claims map[string]any) (string, error)

IssueAccessToken creates a new access token. The key ID of the current signing key is placed in the "kid" header.

func (*JWTTokenService) IssueRefreshToken

func (s *JWTTokenService) IssueRefreshToken(ctx context.Context, subject string, claims map[string]any) (string, error)

IssueRefreshToken creates a new refresh token.

func (*JWTTokenService) ValidateToken

func (s *JWTTokenService) ValidateToken(ctx context.Context, token string) (map[string]any, error)

ValidateToken validates a token and returns its claims. It extracts the key ID from the "kid" header and uses the key provider to get the key for validation.

type PasswordHasher

type PasswordHasher interface {
	Hash(password string) (string, error)
	Compare(hash, password string) error
}

type StaticKeyProvider

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

StaticKeyProvider is a simple JWTKeyProvider that uses a static set of keys.

func NewStaticKeyProvider

func NewStaticKeyProvider(keys map[string][]byte) *StaticKeyProvider

NewStaticKeyProvider creates a new StaticKeyProvider with the given keys.

func (*StaticKeyProvider) GetKey

func (p *StaticKeyProvider) GetKey(keyID string) ([]byte, error)

GetKey returns the key material for the given key ID.

type TokenService

type TokenService interface {
	IssueAccessToken(ctx context.Context, subject string, claims map[string]any) (string, error)
	IssueRefreshToken(ctx context.Context, subject string, claims map[string]any) (string, error)
	ValidateToken(ctx context.Context, token string) (map[string]any, error)
}

Jump to

Keyboard shortcuts

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