tokens

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

JWKSet is the in-memory JWK set that will be used to serve the public keys for JWT verification.

View Source
var (
	PublicKey ed25519.PublicKey
)

Functions

func GenerateDomainVerifyCode

func GenerateDomainVerifyCode(domain, orgID, secretKey string) string

GenerateDomainVerifyCode generates a domain verification code and returns it's hashed value.

func GenerateOrgInviteToken

func GenerateOrgInviteToken() (string, []byte, error)

GenerateOrgInviteToken generates a new token for inviting users to an organization. It returns the token string, its hash, and any error that occurred during generation.

func GenerateSecureToken

func GenerateSecureToken() (token string, hash []byte, err error)

GenerateSecureToken generates a new secure token.

The process works as follows: 1. Generate a secure, random 32 byte token. 2. Encode the token using base64 URL encoding. 3. Hash the token using SHA-256 to create a unique identifier. 4. Return the base64 URL encoded token (goes to user), and hash of the token (goes to database, with expiry time of 1hr).

func GetCurrentKeyID

func GetCurrentKeyID() string

GetCurrentKeyID returns the current key ID for the public key.

func HasTokenBeenRevoked

func HasTokenBeenRevoked(ctx context.Context, q *db.Queries, sessionId uuid.UUID) (bool, error)

func HashAdminToken

func HashAdminToken(token string) []byte

HashAdminToken hashes the provided admin token using SHA-256 and returns it.

Use Case: Hash the token to store OR to compare with a stored hash for verification.

func HashOrgInviteToken

func HashOrgInviteToken(token string) []byte

func HashSecureToken

func HashSecureToken(token string) []byte

HashSecureToken hashes the given secure token using SHA-256.

It returns the hashed token as a byte array. The token should be the RAW base64 URL encoded string generated by GenerateSecureToken.

func HashTokens

func HashTokens(tokens *Tokens) (sessionTokenHash []byte, refreshTokenHash []byte)

HashTokens hashes the session and refresh tokens using SHA-256. It returns the hashed values as strings (And empty strings if the input tokens are empty).

func InitTokens

func InitTokens(ctx context.Context) error

func NewAdminToken

func NewAdminToken() (token string, hash []byte, err error)

NewAdminToken generates a new admin token and its SHA-256 hash. The token is a base64 URL-encoded string of 32 random bytes. The hash is a SHA-256 hash of the token.

func RegisterJWKS

func RegisterJWKS(r chi.Router)

func ValidateDomainVerifyCode

func ValidateDomainVerifyCode(domain, orgID, secretKey string, code string) (bool, error)

func VerifyAdminToken

func VerifyAdminToken(providedToken string, storedHash []byte) bool

VerifyAdminToken compares the provided token against the stored hash in constant time to prevent timing attacks.

func VerifySecureToken

func VerifySecureToken(providedToken string, storedHash []byte) bool

VerifySecureToken compares the provided token against the stored hash in constant time to prevent timing attacks.

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

Types

type Tokens

type Tokens struct {
	// SessionId is the unique identifier for the session.
	SessionId uuid.UUID `json:"sessionId"`
	// SessionToken is the generated session token.
	//
	// This is a standard JWT.
	SessionToken string `json:"sessionToken"`
	// SessionTokenExpiry is the expiration time of the session token.
	SessionTokenExpiry time.Time `json:"sessionTokenExpiry"`
	// RefreshToken is the generated refresh token.
	//
	// This is a secure random 32 bytes encoded as base64.RawURLEncoding
	RefreshToken string `json:"refreshToken"`
	// RefreshTokenExpiry is the expiration time of the refresh token.
	RefreshTokenExpiry time.Time `json:"refreshTokenExpiry"`
}

Tokens represents the result of generating a new token pair.

func GenerateTokens

func GenerateTokens(userId uuid.UUID, claims nexeres_tokens.NexeresClaims) (*Tokens, error)

GenerateTokens generates a new session and refresh token pair for the given user ID and claims.

This sets the standard claims, but app-specific claims defined in NexeresClaims need to be set by the caller. NOTE: This function will NOT store the tokens in the database.

func RefreshExistingTokens

func RefreshExistingTokens(session db.Session, claims nexeres_tokens.NexeresClaims) (*Tokens, error)

Jump to

Keyboard shortcuts

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