Documentation
¶
Index ¶
- Constants
- func BuildChallengeV2Payload(nonceHex, audience string) []byte
- func GenerateNonce() (string, error)
- func GenerateServiceIdentity() (ed25519.PublicKey, ed25519.PrivateKey, error)
- func GenerateSessionToken() (string, error)
- func HashAdminPassword(password string) (string, error)
- func NormalizeAudience(raw string) string
- func SignFederatedJWT(federatedID, sessionID, secret string, expiresAt time.Time) (string, error)
- func SignGuestJWT(guestID, sessionID, secret string, expiresAt time.Time) (string, error)
- func SignJWT(userID, sessionID, deviceID, secret string, expiresAt time.Time) (string, error)
- func TokenHash(tokenString string) string
- func ValidateJWT(tokenString, secret string) (userID, sessionID, deviceID string, isGuest, isFederated bool, ...)
- func VerifyAdminPassword(password, encodedHash string) (bool, error)
- func VerifyChallengeV2Signature(publicKey []byte, nonceHex, audience string, signature []byte) error
- func VerifySignature(publicKey []byte, nonceHex string, signature []byte) error
- func WrapServiceIdentityPrivateKey(privateKey []byte, masterKey string) ([]byte, string, error)
- type Claims
Constants ¶
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.
const NonceLen = 32
NonceLen is the number of random bytes in each challenge nonce.
Variables ¶
This section is empty.
Functions ¶
func BuildChallengeV2Payload ¶
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 ¶
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 ¶
GenerateSessionToken returns a cryptographically random opaque token for cookie sessions.
func HashAdminPassword ¶
HashAdminPassword derives an Argon2id password hash suitable for storage.
func NormalizeAudience ¶
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 ¶
SignFederatedJWT builds and signs a JWT for a federated user. Federated sessions have no DB session record - the JWT is stateless.
func SignGuestJWT ¶
SignGuestJWT builds and signs a short-lived guest JWT. The token is validated by signature only - no DB session record is stored.
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 ¶
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 ¶
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
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.