Overview
TheAuthService class handles authentication operations including login, logout, and automatic token refresh. It implements the AuthRefresher interface to support automatic credential renewal.
Class Definition
Constructor
Default Constructor
AuthService instance with a default ApiClient.
Usage:
Constructor with ApiClient
AuthService instance with a custom ApiClient.
The API client to use for HTTP requests. Must not be null.
NullPointerException if apiClient is null
Methods
login
User’s email address
User’s password as a character array (for security)
API key for authentication
Target environment (DEV, QA, PRE, or PRD)
- Creates a unique device identifier using
UUID.randomUUID() - Constructs a
DeviceRequestwith device information:messagingid: Random UUIDdeviceid: Same UUID as messagingiddevicename: “HL7-Java-Client”bloqueado: falserecordar: false
- Sends authentication request to the configured auth URL
- Initializes
SessionContextwith the returned token and metadata - Starts the
SessionRefreshManagerfor automatic token renewal
NullPointerException- ifenvironmentis nullRuntimeException- if HTTP request fails or response is invalid:- “Error técnico durante login (HTTP )” - for HTTP errors (4xx, 5xx)
- “Respuesta vacía del servicio de autenticación” - for empty response body
- “Respuesta inválida del servicio de autenticación” - for malformed response
The password is passed as
char[] instead of String for security reasons. This allows the password to be cleared from memory after use.logout
- Stops the
SessionRefreshManager - Clears all data in
SessionContext
refreshAuth
AuthRefresher interface. Refreshes the authentication token using the current session’s device information.
Behavior:
- Validates that a session is active
- Validates that device information exists
- Sends refresh request to the auth-refresh endpoint with current token
- Updates
SessionContextwith new token and expiration - Restarts the
SessionRefreshManager
IllegalStateException- if no active session exists or device information is missing:- “No hay sesión activa para refrescar”
- “No hay información de device en sesión”
AuthProblemException- if refresh fails due to authentication issues (HTTP 401/403):- “Credenciales inválidas en refresh (HTTP )”
RuntimeException- for technical errors:- “Error técnico en auth-refresh (HTTP )” - for other HTTP errors
- “Respuesta vacía en auth-refresh” - for empty response
- “Respuesta inválida de auth-refresh” - for malformed response
doRefresh (Internal)
refreshAuth().
Implementation Details:
- Retrieves auth-refresh URL from
EnvironmentConfig - Serializes current
DeviceRequestto JSON - Adds
Authorization: Bearer {token}header - Handles HTTP status codes:
- 401/403: Clears session and throws
AuthProblemException - Other errors: Clears session and throws
RuntimeException
- 401/403: Clears session and throws
- Updates
SessionContextwith new token, expiration, and model-specific data
DeviceRequest Structure
ThecreateDevice() helper method generates a unique device identifier:
Unique device identifier (UUID)
Same as messagingid
Always “HL7-Java-Client”
Always
falseAlways
falseException Hierarchy
AuthProblemException: User needs to re-authenticate (credentials invalid/expired)RuntimeException: Retry or show technical error message
Related Types
- ApiClient - HTTP client used by AuthService
- Environment - Environment enum (DEVELOPMENT, QA, PRODUCTION)
- SessionContext - Session state management
- AuthRefresher - Interface for authentication refresh