Documentation
¶
Overview ¶
Package crypto provides cryptographic utilities for the OAuth authorization server.
Index ¶
- Constants
- func ComputePKCEChallenge(verifier string) string
- func DeriveAlgorithm(key crypto.Signer) (string, error)
- func DeriveKeyID(key crypto.Signer) (string, error)
- func GeneratePKCEVerifier() (string, error)
- func LoadSigningKey(keyPath string) (crypto.Signer, error)
- func ValidateAlgorithmForKey(alg string, key crypto.Signer) error
- type HMACSecrets
- type SigningKeyParams
Constants ¶
const MinRSAKeyBits = 2048
MinRSAKeyBits is the minimum required RSA key size in bits. NIST recommends at least 2048 bits for RSA keys.
const MinSecretLength = 32
MinSecretLength is the minimum required length for the HMAC secret in bytes. Using 256 bits (32 bytes) provides adequate security for HMAC-SHA256.
const PKCEChallengeMethodS256 = "S256"
PKCEChallengeMethodS256 is the PKCE challenge method using SHA-256 (RFC 7636).
Variables ¶
This section is empty.
Functions ¶
func ComputePKCEChallenge ¶
ComputePKCEChallenge computes the code_challenge from a code_verifier using the S256 method per RFC 7636 Section 4.2. code_challenge = BASE64URL(SHA256(code_verifier))
func DeriveAlgorithm ¶
DeriveAlgorithm determines the appropriate JWT signing algorithm for the given key. Returns the algorithm string (e.g., "RS256", "ES256", "EdDSA") based on key type and parameters.
func DeriveKeyID ¶
DeriveKeyID computes a key ID from the public key using RFC 7638 JWK Thumbprint. The thumbprint is computed as base64url(SHA-256(JWK canonical form)).
func GeneratePKCEVerifier ¶
GeneratePKCEVerifier generates a cryptographically random code_verifier per RFC 7636 Section 4.1. The verifier is 43 characters (32 bytes base64url encoded without padding), using characters from the base64url alphabet: [A-Z], [a-z], [0-9], "-", "_".
func LoadSigningKey ¶
LoadSigningKey loads a private key from a PEM file. Supports both RSA (PKCS1 and PKCS8) and ECDSA (PKCS8) formats. Returns a crypto.Signer that can be used for JWT signing.
Types ¶
type HMACSecrets ¶
type HMACSecrets struct {
// Current is the active secret used for signing new tokens.
Current []byte
// Rotated contains previously-used secrets for verifying existing tokens.
Rotated [][]byte
}
HMACSecrets holds the current secret and any rotated (previous) secrets. This supports zero-downtime secret rotation for OAuth token signing.
func LoadHMACSecrets ¶
func LoadHMACSecrets(paths []string) (*HMACSecrets, error)
LoadHMACSecrets loads HMAC secrets from file paths for rotation support. paths[0] is the current (signing) secret; paths[1:] are rotated (verification) secrets. Returns nil if paths is empty (caller should generate random secret).
type SigningKeyParams ¶
type SigningKeyParams struct {
// Key is the private key used for signing.
Key crypto.Signer
// KeyID is the key identifier (either derived from thumbprint or configured).
KeyID string
// Algorithm is the signing algorithm (either derived from key type or configured).
Algorithm string
}
SigningKeyParams contains the derived or configured parameters for a signing key.
func DeriveSigningKeyParams ¶
func DeriveSigningKeyParams(key crypto.Signer, keyID, algorithm string) (*SigningKeyParams, error)
DeriveSigningKeyParams derives or validates signing key parameters. If keyID or algorithm are empty, they are derived from the key. If they are provided, they are validated against the key type.