Skip to main content

Crate id_effect_auth

Crate id_effect_auth 

Source
Expand description

Authentication for id_effect services.

  • password — PasswordHasher: argon2id PHC hashes with the OWASP parameters by default, verification that hands back an upgraded hash when the parameters changed, and a dummy verification so an unknown user costs the same time as a wrong password. Hashing runs on the executor’s blocking pool.
  • session — Sessions: server-side sessions behind a SessionStore (in-memory MemorySessionStore for tests), with idle and absolute timeouts, a fresh id on every login, rotation, logout and revoke-all, and a hardened cookie. Feature pg adds PgSessionStore: sessions in PostgreSQL over id_effect_sql, keyed by the SHA-256 of the id, with the crate’s migrations/. Feature axum adds SessionLayer, which loads the request’s session into id_effect_axum’s request capabilities.
  • principal — AuthPrincipal, the id_effect_authz::Principal every credential check yields, tagged with its AuthScheme; AuthError says why a credential was rejected.
  • apikey — ApiKeys: random keys stored as SHA-256 digests in an ApiKeyStore, found by public id and compared in constant time.
  • basic — BasicAuth: username and password against a UserLookup, with PasswordHasher::verify_or_dummy for unknown users.
  • throttle — LoginThrottle: exponential backoff after failed logins per account and per client address, checked before hashing (on by default in BasicAuth), over a ThrottleStore; feature pg adds PgThrottleStore, shared by every instance.
  • jwt (feature jwt) — JwtVerifier / JwtIssuer: HS/RS/PS/ES/EdDSA tokens with issuer, audience, expiry and required-claim checks and a claim → roles/permissions mapping. Feature jwks adds JwksClient: the identity provider’s key set, cached and refetched (rate-limited) when a token names an unknown kid.
  • middleware (feature axum) — Authenticate: session → bearer → API key → Basic, in a configurable order, installing the principal as id_effect_authz::CurrentPrincipal. Missing credentials continue anonymously; invalid ones answer 401 Problem Details.
  • oidc (feature oidc) — OidcClient: OpenID Connect discovery, authorization code + PKCE (S256), ID tokens verified over the provider’s JWKS (iss, aud, exp, nonce, azp) and mapped with a ClaimMapping; OidcRoutes: /auth/oidc/{login,callback,logout} with state, nonce and code verifier in a server-side flow session, login through Sessions::login, and RP-initiated logout. Implements id_effect_platform’s OAuthClient.
  • totp (feature totp) — Totp: RFC 6238 enrolment (160-bit secret, otpauth:// URI) and verification with ±1 step of skew on a WallClock and replay protection through a TotpStepStore (PgTotpStepStore with feature pg); RecoveryCodes: single-use codes stored as argon2id hashes; Totp::step_up marks the session after a second factor, with_mfa_claims and require_mfa carry it to id_effect_authz policies.
  • account (feature accounts) — Accounts: registration, email verification and forgot / reset password with single-use expiring tokens stored as SHA-256 digests (PgAccountTokenStore), a forgot-password answer that does not reveal whether the account exists, and a reset that revokes the account’s sessions; the mails go through an AccountNotifier (id_effect_mail’s feature auth), never a job queue. AccountRoutes serves them under /auth.

PasswordHasher and Sessions are capabilities with a Layer, like the rest of the platform crates; the credential checks are cheap-to-clone values whose methods return effects, held by the middleware or a handler.

Tier platform (ADR 0010 Amendment 5): the Ledger reference app under examples/reference/ depends on it.

use id_effect::Exit;
use id_effect_auth::{
  MemorySessionStore, PasswordHasher, PasswordParams, SessionConfig, Sessions, Verification,
};

let hasher = PasswordHasher::new(PasswordParams::new(64, 1, 1).unwrap()); // low cost for docs
let sessions = Sessions::new(MemorySessionStore::new(), SessionConfig::default());

let Exit::Success(stored) = hasher.hash("s3cret").block_on() else { panic!() };
let Exit::Success(check) = hasher.verify_or_dummy("s3cret", Some(&stored)).block_on() else {
  panic!()
};
assert_eq!(check, Verification::Valid);
let Exit::Success(session) = sessions.login("user-1", None).block_on() else { panic!() };
assert!(sessions.set_cookie(&session).contains("HttpOnly"));

Re-exports§

pub use account::Account;
pub use account::AccountError;
pub use account::AccountMail;
pub use account::AccountNotifier;
pub use account::AccountRoutes;
pub use account::AccountStore;
pub use account::AccountToken;
pub use account::AccountTokenStore;
pub use account::Accounts;
pub use account::AccountsConfig;
pub use account::MemoryAccountNotifier;
pub use account::MemoryAccountStore;
pub use account::MemoryAccountTokenStore;
pub use account::PgAccountTokenStore;
pub use account::TokenKind;
pub use account::TokenRecord;
pub use apikey::ApiKey;
pub use apikey::ApiKeyRecord;
pub use apikey::ApiKeyStore;
pub use apikey::ApiKeys;
pub use apikey::MemoryApiKeyStore;
pub use basic::BasicAuth;
pub use basic::MemoryUsers;
pub use basic::UserCredentials;
pub use basic::UserLookup;
pub use basic::basic_credentials;
pub use basic::basic_header;
pub use jwks::JwksClient;
pub use jwt::ClaimMapping;
pub use jwt::JwtIssuer;
pub use jwt::JwtKey;
pub use jwt::JwtVerifier;
pub use middleware::Authenticate;
pub use middleware::AuthenticateService;
pub use oidc::OidcClient;
pub use oidc::OidcConfig;
pub use oidc::OidcError;
pub use oidc::OidcRoutes;
pub use password::MAX_PASSWORD_LEN;
pub use password::Password;
pub use password::PasswordError;
pub use password::PasswordHash;
pub use password::PasswordHasher;
pub use password::PasswordHasherEnv;
pub use password::PasswordParams;
pub use password::Verification;
pub use principal::AuthPrincipal;
pub use principal::AuthScheme;
pub use session::SessionLayer;
pub use session::CookieConfig;
pub use session::MemorySessionStore;
pub use session::SameSite;
pub use session::Session;
pub use session::SessionConfig;
pub use session::SessionError;
pub use session::SessionId;
pub use session::SessionStore;
pub use session::SessionStoreService;
pub use session::Sessions;
pub use session::PgSessionStore;
pub use session::pg_session_store_layer;
pub use throttle::PgThrottleStore;
pub use throttle::Failures;
pub use throttle::LoginThrottle;
pub use throttle::MEMORY_THROTTLE_STORE_POSTURE;
pub use throttle::MemoryThrottleStore;
pub use throttle::ThrottleStore;
pub use totp::PgTotpStepStore;
pub use totp::MemoryTotpStepStore;
pub use totp::RecoveryCodes;
pub use totp::Totp;
pub use totp::TotpConfig;
pub use totp::TotpError;
pub use totp::TotpSecret;
pub use totp::TotpStepStore;
pub use totp::TotpVerification;

Modules§

account
Account flows (feature accounts): registration, email verification, and forgot / reset password with single-use expiring tokens.
apikey
API keys: random secrets stored as SHA-256 digests, found by a public id and compared in constant time.
basic
HTTP Basic credentials: a username and password checked against a UserLookup with a PasswordHasher.
jwks
JwksClient (feature jwks): an issuer’s JSON Web Key Set, fetched over HTTP and cached, refetched when a token names a key it does not know.
jwt
JSON Web Tokens (feature jwt): JwtVerifier turns a bearer token into an AuthPrincipal, JwtIssuer signs first-party tokens.
middleware
Authenticate (feature axum): declarative credential middleware that turns a request’s session cookie, bearer JWT, API key or Basic credentials into its principal.
oidc
OpenID Connect login (feature oidc): OidcClient speaks to the identity provider, OidcRoutes mounts the browser flow on an axum router.
password
Password hashing: argon2id PHC strings, verification that reports when a stored hash should be upgraded, and a dummy verification for unknown users.
principal
AuthPrincipal: the principal a credential authenticates, as an id_effect_authz::Principal.
session
Server-side sessions: opaque random ids in a cookie, session state in a SessionStore.
throttle
LoginThrottle: exponential backoff after failed logins, per account and per client address, checked before any password hashing.
totp
TOTP second factor (RFC 6238, over totp-rs): enrolment, verification on the executor’s clock with replay protection, argon2-hashed recovery codes, and an MFA step-up marker on the session.

Enums§

Algorithm
The algorithms supported for signing/verifying JWTs
AuthError
Why a presented credential (bearer token, API key, Basic password) was not accepted, or why it could not be checked.