User Identity Model and Persistence
src/main/java/com/bankasap/model/User.java
src/main/java/com/bankasap/repository/UserRepository.java
Overview
The User entity defines the persisted identity record used by the authentication flow. It stores the login credentials, failed-attempt counters, lockout window, and two-factor code state that AuthService reads and updates during sign-up verification, login, and code resend operations.
UserRepository is the Spring Data persistence abstraction for User. It extends CrudRepository<User,Long> and is consumed by AuthService for user lookup and persistence writes. In the current source, AuthService uses findAll() and save() through this repository to manage user records, while EmailService handles delivery of the verification and lockout notifications associated with those records.
Model Shape
User
src/main/java/com/bankasap/model/User.java
User is mapped as a JPA entity with @Entity and @Table(name="AllUsers"). The identifier uses @Id with @GeneratedValue(strategy = GenerationType.IDENTITY).
| Property | Type | Description |
|---|---|---|
id |
Long |
Primary key generated by the database. |
email |
String |
User login and notification address. |
password |
String |
Stored password value used by authentication checks. |
failedLoginAttempts |
int |
Counter incremented when password validation fails. |
lockedUntil |
LocalDateTime |
Lockout timestamp used by isLocked(). |
twoFactorCode |
String |
Active 2FA code assigned during login verification. |
twoFactorCodeExpiresAt |
LocalDateTime |
Expiration time for the active 2FA code. |
Behavior exposed by User
| Method | Description |
|---|---|
getTwoFactorCode |
Returns the active 2FA code. |
setTwoFactorCode |
Updates the active 2FA code. |
getTwoFactorCodeExpiresAt |
Returns the 2FA expiration time. |
setTwoFactorCodeExpiresAt |
Updates the 2FA expiration time. |
isLocked |
Returns true when lockedUntil exists and is after LocalDateTime.now(). |
getEmail |
Returns the stored email address. |
getPassword |
[REDACTED] |
getFailedLoginAttempts |
Returns the current failed-attempt count. |
setFailedLoginAttempts |
Updates the failed-attempt count. |
getLockedUntil |
Returns the current lockout timestamp. |
setLockedUntil |
Updates the lockout timestamp. |
Constructor state
emailset to the supplied valuepasswordset to the supplied valuefailedLoginAttemptsset to0lockedUntilset tonull
That constructor state is the baseline used when AuthService.verifySignUp creates and saves a new user.
Repository Access Pattern
UserRepository
src/main/java/com/bankasap/repository/UserRepository.java
UserRepository is a marker-style Spring Data interface:
public interface UserRepository extends CrudRepository<User,Long> {
}
| Property | Type | Description |
|---|---|---|
| none declared | — | The interface adds no custom fields. |
| Method | Description |
|---|---|
| none declared | The interface declares no custom methods beyond CrudRepository<User,Long>. |
Persistence behavior used by the service layer
AuthService relies on the inherited repository operations in two ways:
findAll()is cast toList<User>and scanned linearly insidefindUserByEmail(String email).save(User newUser)is called after successful sign-up verification.
Because UserRepository does not define a direct lookup method such as an email-based finder, email matching is performed in service code by iterating over all retrieved users.
Service Consumers
AuthService
src/main/java/com/bankasap/service/AuthService.java
AuthService injects UserRepository, EmailService, and PasswordEncoder. It also keeps an in-memory List<PendingSignUp> for pre-registration verification.
Injected dependencies
| Type | Description |
|---|---|
EmailService |
Sends 2FA codes to email addresses. |
PasswordEncoder |
[REDACTED] |
UserRepository |
Loads and saves User records. |
Properties
| Property | Type | Description |
|---|---|---|
emailService |
EmailService |
Mail delivery dependency. |
formatter |
DateTimeFormatter |
Formats lockout timestamps with pattern dd/MM/yyyy HH:mm:ss. |
passwordEncoder |
PasswordEncoder |
Password hashing and matching dependency. |
pendingSignUps |
List<PendingSignUp> |
In-memory pending registration records. |
userRepository |
UserRepository |
Persistence access for User records. |
Public methods
| Method | Description |
|---|---|
signUp |
Validates the supplied credentials, creates a pending sign-up record, and emails a 2FA code. |
verifySignUp |
Confirms the pending sign-up code and persists a new User. |
resendSignUpCode |
Regenerates and re-sends the sign-up verification code. |
Data flow through persistence
signUprejects existing emails by callingfindUserByEmail(email), which iterates overuserRepository.findAll().- When the sign-up request passes validation, the method stores a
PendingSignUpin memory and callsemailService.sendTwoFactorCode(email, code). verifySignUploads the matching pending record, createsnew User(pendingSignUp.getEmail(), pendingSignUp.getHashedPassword()), and persists it withuserRepository.save(newUser).resendSignUpCodeupdates the in-memory pending record and sends a new email throughEmailService.
Lockout and credential state
AuthService.login and AuthService.verifyCode both act on User state:
- failed password attempts increment
failedLoginAttempts - lockout state is written through
setLockedUntil - active 2FA code state is written through
setTwoFactorCodeandsetTwoFactorCodeExpiresAt
Major interaction flow
sequenceDiagram
participant U as User
participant A as AuthService
participant R as UserRepository
participant E as EmailService
U->>A: signUp
A->>R: findAll
R-->>A: users
A->>E: sendTwoFactorCode
E-->>U: verification email
U->>A: verifySignUp
A->>R: save
R-->>A: persisted user
A-->>U: User registered successfully
EmailService
[!NOTE]
AuthService.loginsetslockedUntilwithLocalDateTime.now().plusSeconds(15), but the returned message saysAccount locked for 15 minutes. The stored lockout duration and the message text do not match.
src/main/java/com/bankasap/service/EmailService.java
EmailService is the mail delivery boundary used by authentication workflows. It injects JavaMailSender, builds SimpleMailMessage instances, and sends them immediately.
Injected dependencies
| Type | Description |
|---|---|
JavaMailSender |
Sends the constructed SimpleMailMessage. |
Properties
| Property | Type | Description |
|---|---|---|
mailSender |
JavaMailSender |
Mail transport used for outgoing messages. |
Public methods
| Method | Description |
|---|---|
sendTwoFactorCode |
Sends an email with subject Bank ASAP - 2FA Code and a body containing the supplied code. |
sendAccountLockedEmail |
Sends an email with subject Bank ASAP - Account Locked after repeated failed logins. |
Mail content used by the authentication flow
sendTwoFactorCode(String toEmail, String code)sets:- recipient:
toEmail - subject:
Bank ASAP - 2FA Code - text:
Your Bank ASAP authentication code is:plus the code value
- recipient:
sendAccountLockedEmail(String toEmail)sets:- recipient:
toEmail - subject:
Bank ASAP - Account Locked - text:
Your account was locked after 3 failed login attempts.
- recipient:
Mail delivery flow
sequenceDiagram
participant S as AuthService
participant M as EmailService
participant J as JavaMailSender
participant U as User
S->>M: sendTwoFactorCode
M->>J: send
J-->>U: email delivered
S->>M: sendAccountLockedEmail
M->>J: send
J-->>U: email delivered
Supporting Pending Sign-Up State
PendingSignUp
src/main/java/com/bankasap/model/PendingSignUp.java
PendingSignUp is an in-memory helper object used only by AuthService to hold sign-up data before a User is persisted.
| Property | Type | Description |
|---|---|---|
email |
String |
Email address tied to the pending registration. |
hashedPassword |
String |
Encoded password saved before final account creation. |
code |
String |
Verification code sent to the email address. |
codeExpiresAt |
LocalDateTime |
Expiration timestamp for the verification code. |
| Method | Description |
|---|---|
getEmail |
Returns the pending email address. |
getHashedPassword |
[REDACTED] |
getCode |
Returns the pending verification code. |
getCodeExpiresAt |
Returns the code expiration time. |
setCode |
Replaces the verification code. |
setCodeExpiresAt |
Replaces the code expiration time. |
AuthService.signUp, verifySignUp, and resendSignUpCode use this type to stage registration data before creating a User.
Persistence and Identity Flow
signUpvalidates email format, password rules, password confirmation, and uniqueness against repository data.- A successful sign-up creates a
PendingSignUpentry in memory and sends a code by email. verifySignUpchecks expiration and code equality, then writes a persistedUserwithuserRepository.save(newUser).loginloads theUser, checks lock status throughisLocked(), increments failed-attempt counters, and updates 2FA state on success.verifyCodeclears the stored 2FA values after a valid code match.resendCoderefreshes the 2FA code and expiration for the existingUser.EmailServicedelivers the verification and lockout notifications that correspond to those state changes.
Key Classes Reference
| Class | Location | Responsibility |
|---|---|---|
User |
src/main/java/com/bankasap/model/User.java |
Persisted identity record with password, lockout, and 2FA state. |
UserRepository |
src/main/java/com/bankasap/repository/UserRepository.java |
Spring Data persistence abstraction for User. |
AuthService |
src/main/java/com/bankasap/service/AuthService.java |
Orchestrates sign-up, verification, login state, and repository writes. |
EmailService |
src/main/java/com/bankasap/service/EmailService.java |
Sends 2FA and lockout emails. |
PendingSignUp |
src/main/java/com/bankasap/model/PendingSignUp.java |
In-memory staging record for pre-persistence sign-up flow. |