Documentation
¶
Index ¶
- Variables
- func GenerateDomainVerifyCode(domain, orgID, secretKey string) string
- func GenerateOrgInviteToken() (string, []byte, error)
- func GenerateSecureToken() (token string, hash []byte, err error)
- func GetCurrentKeyID() string
- func HasTokenBeenRevoked(ctx context.Context, q *db.Queries, sessionId uuid.UUID) (bool, error)
- func HashAdminToken(token string) []byte
- func HashOrgInviteToken(token string) []byte
- func HashSecureToken(token string) []byte
- func HashTokens(tokens *Tokens) (sessionTokenHash []byte, refreshTokenHash []byte)
- func InitTokens(ctx context.Context) error
- func NewAdminToken() (token string, hash []byte, err error)
- func RegisterJWKS(r chi.Router)
- func ValidateDomainVerifyCode(domain, orgID, secretKey string, code string) (bool, error)
- func VerifyAdminToken(providedToken string, storedHash []byte) bool
- func VerifySecureToken(providedToken string, storedHash []byte) bool
- type Tokens
Constants ¶
This section is empty.
Variables ¶
var JWKSet *jwkset.MemoryJWKSet
JWKSet is the in-memory JWK set that will be used to serve the public keys for JWT verification.
var (
PublicKey ed25519.PublicKey
)
Functions ¶
func GenerateDomainVerifyCode ¶
GenerateDomainVerifyCode generates a domain verification code and returns it's hashed value.
func GenerateOrgInviteToken ¶
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 ¶
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 HashAdminToken ¶
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 HashSecureToken ¶
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 ¶
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 NewAdminToken ¶
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 VerifyAdminToken ¶
VerifyAdminToken compares the provided token against the stored hash in constant time to prevent timing attacks.
func VerifySecureToken ¶
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)