auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const ChallengeV2Header = "HUSH-AUTH-CHALLENGE-V2"

ChallengeV2Header is the domain-separation tag for the v2 auth challenge signature payload. It binds the signature to the auth challenge purpose and prevents a v2 signature from being mistaken for any other Ed25519 payload signed by the same identity key.

View Source
const NonceLen = 32

NonceLen is the number of random bytes in each challenge nonce.

Variables

This section is empty.

Functions

func BuildChallengeV2Payload

func BuildChallengeV2Payload(nonceHex, audience string) []byte

BuildChallengeV2Payload returns the canonical UTF-8 byte encoding of the v2 auth-challenge signature payload:

HUSH-AUTH-CHALLENGE-V2\naudience=<origin>\nnonce=<hex>

Both client and server MUST produce the same bytes from the same (nonce, audience) pair. The audience is expected to already be in normalized form (see NormalizeAudience); this function does not re-normalize so callers can pin the exact bytes that get signed.

func GenerateNonce

func GenerateNonce() (string, error)

GenerateNonce returns a 64-character lowercase hex string backed by 32 cryptographically random bytes. The nonce is suitable for use in the BIP39 challenge-response authentication flow.

func GenerateServiceIdentity

func GenerateServiceIdentity() (ed25519.PublicKey, ed25519.PrivateKey, error)

GenerateServiceIdentity creates a new Ed25519 keypair for technical service use.

func GenerateSessionToken

func GenerateSessionToken() (string, error)

GenerateSessionToken returns a cryptographically random opaque token for cookie sessions.

func HashAdminPassword

func HashAdminPassword(password string) (string, error)

HashAdminPassword derives an Argon2id password hash suitable for storage.

func NormalizeAudience

func NormalizeAudience(raw string) string

NormalizeAudience reduces an audience string to scheme://host[:port] form with the scheme and host lowercased, the default port for the scheme stripped, and any path / query / fragment removed.

Returns the normalized form, or "" when the input is empty, missing a scheme/host, uses a scheme other than http/https, or otherwise fails to parse. The empty-string return is the signal that callers should treat as "unusable" — never compare a normalized audience against "" as if it were a valid origin.

func SignFederatedJWT

func SignFederatedJWT(federatedID, sessionID, secret string, expiresAt time.Time) (string, error)

SignFederatedJWT builds and signs a JWT for a federated user. Federated sessions have no DB session record - the JWT is stateless.

func SignGuestJWT

func SignGuestJWT(guestID, sessionID, secret string, expiresAt time.Time) (string, error)

SignGuestJWT builds and signs a short-lived guest JWT. The token is validated by signature only - no DB session record is stored.

func SignJWT

func SignJWT(userID, sessionID, deviceID, secret string, expiresAt time.Time) (string, error)

SignJWT builds and signs a JWT for the user/session. Expires at expiresAt.

func TokenHash

func TokenHash(tokenString string) string

TokenHash returns a deterministic hash of the token for storage/lookup.

func ValidateJWT

func ValidateJWT(tokenString, secret string) (userID, sessionID, deviceID string, isGuest, isFederated bool, federatedIdentityID string, err error)

ValidateJWT parses and validates the token, returning userID, sessionID, isGuest, isFederated, federatedIdentityID, and any error.

func VerifyAdminPassword

func VerifyAdminPassword(password, encodedHash string) (bool, error)

VerifyAdminPassword compares a stored Argon2id hash against the supplied password.

func VerifyChallengeV2Signature

func VerifyChallengeV2Signature(publicKey []byte, nonceHex, audience string, signature []byte) error

VerifyChallengeV2Signature verifies an Ed25519 signature over the v2 challenge payload produced by BuildChallengeV2Payload(nonceHex, audience).

audience MUST be the caller-validated, normalized expected audience for this request. The caller is responsible for rejecting requests whose declared audience does not match the server's canonical public API origin BEFORE calling this function; the verifier itself only attests that the signature is a valid Ed25519 signature over the v2 payload for the given (nonceHex, audience).

Returns nil on success.

func VerifySignature

func VerifySignature(publicKey []byte, nonceHex string, signature []byte) error

VerifySignature verifies that signature is a valid Ed25519 signature of the nonce (expressed as a hex string) under publicKey. This is the legacy v1 challenge protocol: the signed message is the raw nonce bytes with no audience binding. It is retained ONLY for backward compatibility with pre-v2 clients and must never be invoked when the request carries any v2 fields (challengeVersion == 2 or audience present).

Returns nil on success, non-nil error on any verification failure including:

  • publicKey is not exactly ed25519.PublicKeySize (32) bytes
  • signature is not exactly ed25519.SignatureSize (64) bytes
  • nonceHex is not valid lowercase hex
  • the signature does not verify

func WrapServiceIdentityPrivateKey

func WrapServiceIdentityPrivateKey(privateKey []byte, masterKey string) ([]byte, string, error)

WrapServiceIdentityPrivateKey encrypts the private key for at-rest storage.

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
	// SessionID is the session UUID used to look up the session record in the DB.
	// Guest sessions have no DB record; for guests, SessionID holds the guest UUID.
	SessionID string `json:"sid"`
	// IsGuest marks a short-lived ephemeral guest session. Guest sessions are
	// validated by JWT signature only - no DB session record is required.
	IsGuest bool `json:"is_guest,omitempty"`
	// IsFederated marks a stateless federated session. Federated sessions have no
	// DB session record - the JWT is the sole source of truth.
	IsFederated bool `json:"is_federated,omitempty"`
	// FederatedIdentityID is the federated_identities row ID for the remote user.
	FederatedIdentityID string `json:"fid,omitempty"`
	// DeviceID identifies the local device that obtained the session.
	// Empty for guests and federated sessions.
	DeviceID string `json:"did,omitempty"`
}

Claims holds JWT claims for Hush auth.

Jump to

Keyboard shortcuts

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