Liking cljdoc? Tell your friends :D

wagoe.user.shell.auth

Shell Layer - Authentication service with side effects.

This is the SHELL layer for authentication operations:

  • Password hashing and verification (bcrypt)
  • JWT token creation and validation
  • Session token generation
  • Authentication coordination
  • MFA verification

All I/O and side effects happen here. Pure business logic is delegated to wagoe.user.core.authentication.

Shell Layer - Authentication service with side effects.

This is the SHELL layer for authentication operations:
- Password hashing and verification (bcrypt)
- JWT token creation and validation
- Session token generation
- Authentication coordination
- MFA verification

All I/O and side effects happen here. Pure business logic
is delegated to wagoe.user.core.authentication.
raw docstring

account-in-tenantclj

(account-in-tenant user-repository tenant-id email)

The account of email in tenant tenant-id, nil meaning no tenant.

The account of `email` in tenant `tenant-id`, nil meaning no tenant.
sourceraw docstring

accounts-ofclj

(accounts-of user-repository email)

Every account of email. A repository without ITenantAccounts knows one per email (BOU-995).

Every account of `email`. A repository without `ITenantAccounts` knows one
per email (BOU-995).
sourceraw docstring

authenticate-userclj

(authenticate-user auth-service email password login-context)

Coordinate user authentication with all security checks including MFA.

Args: auth-service: AuthenticationService instance email: User email password: Plain text password login-context: Map with :ip-address, :user-agent, :mfa-code (optional), and :tenant-given? with :tenant-id to sign in to one tenant's account; without, every account of the email is tried

Returns: On success {:success? true :user … :session …}.

On failure {:success? false :error {:type … :message …}}, the shape ADR-036 §3 decided — a wrong password is an answer, not an exceptional condition, and a caller that wants to escalate can rethrow the :error map as a typed ex-info without inventing a taxonomy. :type is one of :validation-error, :authentication-failed, :account-locked, :mfa-required, :mfa-enrolment-required, :mfa-verification-failed, :tenant-choice-required; :account-locked, and an authentication-failed lockout, carry :retry-after. A tenant choice comes with :tenant-ids: the tenants (nil for none) where this email and password both match (BOU-995).

:requires-mfa? stays at the top level: it is not part of why the attempt failed, it is what the caller does next.

Coordinate user authentication with all security checks including MFA.

Args:
  auth-service: AuthenticationService instance
  email: User email
  password: Plain text password
  login-context: Map with :ip-address, :user-agent, :mfa-code (optional),
                 and :tenant-given? with :tenant-id to sign in to one tenant's
                 account; without, every account of the email is tried
  
Returns:
  On success `{:success? true :user … :session …}`.

  On failure `{:success? false :error {:type … :message …}}`, the shape
  ADR-036 §3 decided — a wrong password is an answer, not an exceptional
  condition, and a caller that wants to escalate can rethrow the `:error`
  map as a typed `ex-info` without inventing a taxonomy. `:type` is one of
  `:validation-error`, `:authentication-failed`, `:account-locked`, `:mfa-required`,
  `:mfa-enrolment-required`, `:mfa-verification-failed`,
  `:tenant-choice-required`; `:account-locked`, and an authentication-failed
  lockout, carry `:retry-after`. A tenant choice comes with `:tenant-ids`: the
  tenants (nil for none) where this email and password both match (BOU-995).

  `:requires-mfa?` stays at the top level: it is not part of why the attempt
  failed, it is what the caller does next.
sourceraw docstring

create-authentication-serviceclj

(create-authentication-service user-repository
                               session-repository
                               mfa-service
                               auth-config)

Factory function: Create authentication service instance.

Args: user-repository: IUserRepository implementation session-repository: IUserSessionRepository implementation mfa-service: MFAService implementation
auth-config: Authentication configuration map. :mfa-required? is an optional (fn [user login-context] bool), a symbol naming one, or an IMfaPolicy; throws :configuration-error otherwise.

Returns: AuthenticationService instance

Factory function: Create authentication service instance.

Args:
  user-repository: IUserRepository implementation
  session-repository: IUserSessionRepository implementation
  mfa-service: MFAService implementation  
  auth-config: Authentication configuration map. :mfa-required? is an
               optional (fn [user login-context] bool), a symbol naming
               one, or an IMfaPolicy; throws :configuration-error otherwise.
  
Returns:
  AuthenticationService instance
sourceraw docstring

create-jwt-tokenclj

(create-jwt-token user session-duration-hours)
(create-jwt-token user session-duration-hours session-id)

Shell function: Create JWT token for authenticated user.

Args: user: User entity session-duration-hours: How long token should be valid

Returns: JWT token string

Side effects: Crypto operations, time-dependent

session-id goes in as :sid; the JWT middleware refuses a token whose session has ended, and one without a sid (BOU-1006).

Shell function: Create JWT token for authenticated user.

Args:
  user: User entity
  session-duration-hours: How long token should be valid
  
Returns:
  JWT token string
  
Side effects: Crypto operations, time-dependent

`session-id` goes in as :sid; the JWT middleware refuses a token whose
session has ended, and one without a sid (BOU-1006).
sourceraw docstring

create-user-with-passwordclj

(create-user-with-password user-data _user-repository)

Shell function: Create user with hashed password.

Args: user-data: User creation data with plain text password user-repository: IUserRepository implementation

Returns: User entity with password-hash field populated

Side effects: Password hashing, I/O operations

Shell function: Create user with hashed password.

Args:
  user-data: User creation data with plain text password
  user-repository: IUserRepository implementation
  
Returns:
  User entity with password-hash field populated
  
 Side effects: Password hashing, I/O operations
sourceraw docstring

generate-session-tokenclj

(generate-session-token)

Shell function: Generate cryptographically secure session token.

Returns: URL-safe base64-encoded random token string (no +, /, or = chars). Uses URL-safe encoding so tokens can safely appear in URL paths without triggering Jetty 'Ambiguous URI path separator' errors.

Side effects: Secure random generation

Shell function: Generate cryptographically secure session token.

Returns:
  URL-safe base64-encoded random token string (no +, /, or = chars).
  Uses URL-safe encoding so tokens can safely appear in URL paths
  without triggering Jetty 'Ambiguous URI path separator' errors.

Side effects: Secure random generation
sourceraw docstring

get-jwt-secretclj

(get-jwt-secret)

The validated JWT_SECRET. Throws WGE-103 as jwt-secret/value does.

The validated JWT_SECRET. Throws WGE-103 as `jwt-secret/value` does.
sourceraw docstring

hash-passwordclj

(hash-password password)

password bcrypt-hashed, see wagoe.user.shell.bcrypt.

`password` bcrypt-hashed, see `wagoe.user.shell.bcrypt`.
sourceraw docstring

login-accountsclj

(login-accounts user-repository email login-context)

The accounts email signs in to: the one of login-context's tenant when it names one (:tenant-given?, :tenant-id nil for no tenant), else every one.

The accounts `email` signs in to: the one of `login-context`'s tenant when it
names one (`:tenant-given?`, `:tenant-id` nil for no tenant), else every one.
sourceraw docstring

mfa-required?clj

(mfa-required? auth-service user login-context)

Whether this login needs a second factor: the app's :mfa-required? hook when it set one, else whether the user turned MFA on. Shared by password and OIDC sign-in. The hook sees neither the user's secrets nor the code.

Whether this login needs a second factor: the app's :mfa-required? hook
when it set one, else whether the user turned MFA on. Shared by password
and OIDC sign-in. The hook sees neither the user's secrets nor the code.
sourceraw docstring

open-session!clj

(open-session! auth-service user login-risk login-context current-time)

Create the DB session for a user who has proven who they are, under the session policy. Shared by password and OIDC sign-in.

Returns {:policy … :session …}; :session carries a :jwt-token that is not stored.

Create the DB session for a user who has proven who they are, under the
session policy. Shared by password and OIDC sign-in.

Returns {:policy … :session …}; :session carries a :jwt-token that is not
stored.
sourceraw docstring

password-fingerprintclj

(password-fingerprint user)

What changes when user's password does, without revealing its hash: an enrolment ticket carries it, and a changed password voids the ticket.

What changes when `user`'s password does, without revealing its hash: an
enrolment ticket carries it, and a changed password voids the ticket.
sourceraw docstring

validate-jwt-secret!clj

(validate-jwt-secret!)

Fail-fast startup check: force resolution + validation of JWT_SECRET so a missing or too-short secret aborts boot, rather than booting green and failing on the first authentication request. Call from system wiring.

Fail-fast startup check: force resolution + validation of JWT_SECRET so a
missing or too-short secret aborts boot, rather than booting green and
failing on the first authentication request. Call from system wiring.
sourceraw docstring

validate-jwt-tokenclj

(validate-jwt-token token)

Shell function: Validate and decode JWT token.

Args: token: JWT token string

Returns: {:valid? true :claims {...}} or {:valid? false :error string}

Side effects: Crypto operations

Shell function: Validate and decode JWT token.

Args:
  token: JWT token string
  
Returns:
  {:valid? true :claims {...}} or {:valid? false :error string}
  
Side effects: Crypto operations
sourceraw docstring

verify-passwordclj

(verify-password password hash)

Shell function: Verify password against bcrypt hash.

Args: password: Plain text password
hash: Bcrypt hash string

Returns: Boolean - true if password matches

Side effects: Crypto operations

Shell function: Verify password against bcrypt hash.

Args:
  password: Plain text password  
  hash: Bcrypt hash string
  
Returns:
  Boolean - true if password matches
  
Side effects: Crypto operations
sourceraw docstring

verify-second-factor!clj

(verify-second-factor! auth-service user code)

Check the TOTP or backup code for user, spending a backup code. The caller has claimed the attempt with lockout/claim-attempts!, so a wrong or blank code stays counted, the same counter as a wrong password (BOU-698). Returns mfa-shell/verify-mfa-code's map, or nil for a blank code.

Check the TOTP or backup `code` for `user`, spending a backup code. The
caller has claimed the attempt with `lockout/claim-attempts!`, so a wrong
or blank code stays counted, the same counter as a wrong password (BOU-698).
Returns `mfa-shell/verify-mfa-code`'s map, or nil for a blank code.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close