-
Notifications
You must be signed in to change notification settings - Fork 0
feat(identity): setup base entities, DTOs, and JWT security config #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
IsraaXx
wants to merge
3
commits into
develop
Choose a base branch
from
feature/authentication
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,31 @@ | ||
| spring.application.name=app | ||
|
|
||
| # DATABASE | ||
| # =============================== | ||
| spring.datasource.url=${DATABASE_URL} | ||
| spring.datasource.username=${DATABASE_USERNAME} | ||
| spring.datasource.password=${DATABASE_PASSWORD} | ||
| spring.datasource.driver-class-name=org.postgresql.Driver | ||
|
|
||
| # =============================== | ||
| # JPA | ||
| # =============================== | ||
| spring.jpa.hibernate.ddl-auto=update | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| spring.jpa.show-sql=true | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect | ||
| spring.jpa.properties.hibernate.hbm2ddl.create_namespaces=true | ||
|
|
||
| # =============================== | ||
| # MAIL CONFIGURATION | ||
| # =============================== | ||
| spring.mail.host=smtp.gmail.com | ||
| spring.mail.port=587 | ||
| spring.mail.username=${MAIL_USERNAME} | ||
| spring.mail.password=${MAIL_PASSWORD} | ||
| spring.mail.properties.mail.smtp.auth=true | ||
| spring.mail.properties.mail.smtp.starttls.enable=true | ||
|
|
||
| # =============================== | ||
| # JWT SECRET KEY | ||
| # =============================== | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| jwt.secret-key=${JWT_SECRET} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
identity/src/main/kotlin/org/spendoo/identity/controller/AuthController.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.spendoo.identity.controller | ||
|
|
||
| import jakarta.validation.Valid | ||
| import org.spendoo.identity.dto.response.AuthResponse | ||
| import org.spendoo.identity.dto.request.LoginRequest | ||
| import org.spendoo.identity.dto.request.RefreshTokenRequest | ||
| import org.spendoo.identity.dto.request.RegisterRequest | ||
| import org.spendoo.identity.service.AuthService | ||
| import org.springframework.http.HttpStatus | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.web.bind.annotation.PostMapping | ||
| import org.springframework.web.bind.annotation.RequestBody | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.RestController | ||
|
|
||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/auth") | ||
| class AuthController ( | ||
| private val authService: AuthService | ||
| ) { | ||
|
|
||
| @PostMapping("/signup") | ||
| fun register (@Valid @RequestBody request: RegisterRequest): ResponseEntity<AuthResponse> { | ||
| val response = authService.register(request) | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(response) | ||
| } | ||
|
|
||
| @PostMapping("/login") | ||
| fun login (@Valid @RequestBody request: LoginRequest): ResponseEntity<AuthResponse> { | ||
| val response = authService.login(request) | ||
| return ResponseEntity.ok(response) | ||
| } | ||
|
|
||
| @PostMapping("/refresh") | ||
| fun refresh (@Valid @RequestBody request: RefreshTokenRequest): ResponseEntity<AuthResponse> { | ||
| val response = authService.refreshToken(request) | ||
| return ResponseEntity.ok(response) | ||
| } | ||
|
|
||
| @PostMapping("/logout") | ||
| fun logout(@Valid @RequestBody request: RefreshTokenRequest): ResponseEntity<Map<String, String>> { | ||
| authService.logout(request) | ||
| return ResponseEntity.ok(mapOf("message" to "Logged out successfully")) | ||
| } | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
identity/src/main/kotlin/org/spendoo/identity/dto/request/ForgotPasswordRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.Email | ||
| import jakarta.validation.constraints.NotBlank | ||
|
|
||
| data class ForgotPasswordRequest( | ||
| @field:NotBlank(message = "Email is required") | ||
| @field:Email(message = "Invalid email format") | ||
| val email: String | ||
| ) |
18 changes: 18 additions & 0 deletions
18
identity/src/main/kotlin/org/spendoo/identity/dto/request/LoginRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.Email | ||
| import jakarta.validation.constraints.NotBlank | ||
| import jakarta.validation.constraints.Pattern | ||
|
|
||
| data class LoginRequest( | ||
| @field:NotBlank(message = "Email is required") | ||
| @field:Email(message = "Invalid email format") | ||
| val email: String, | ||
|
|
||
| @field:NotBlank(message = "Password is required") | ||
| @field:Pattern( | ||
| regexp = """^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!]).{8,}$""", | ||
| message = "Password must contain at least 8 characters, one uppercase, one lowercase, one number and one special character" | ||
| ) | ||
| val password: String | ||
| ) |
9 changes: 9 additions & 0 deletions
9
identity/src/main/kotlin/org/spendoo/identity/dto/request/RefreshTokenRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.NotBlank | ||
|
|
||
| data class RefreshTokenRequest( | ||
|
|
||
| @field:NotBlank(message = "Refresh token is required") | ||
| val refreshToken: String | ||
| ) |
32 changes: 32 additions & 0 deletions
32
identity/src/main/kotlin/org/spendoo/identity/dto/request/RegisterRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.Email | ||
| import jakarta.validation.constraints.NotBlank | ||
| import jakarta.validation.constraints.NotNull | ||
| import jakarta.validation.constraints.Past | ||
| import jakarta.validation.constraints.Pattern | ||
| import org.spendoo.identity.enums.Gender | ||
| import java.time.LocalDate | ||
|
|
||
| data class RegisterRequest( | ||
| @field:NotBlank(message = "Full name is required") | ||
| val fullName: String, | ||
|
|
||
| @field:NotBlank(message = "Email is required") | ||
| @field:Email(message = "Please provide a valid email address") | ||
| val email: String, | ||
|
|
||
| @field:NotBlank(message = "Password is required") | ||
| @field:Pattern( | ||
| regexp = """^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!]).{8,}$""", | ||
| message = "Password must contain at least 8 characters, one uppercase, one lowercase, one number and one special character" | ||
| ) | ||
| val password: String, | ||
|
|
||
| @field:NotNull(message = "Gender is required") | ||
| val gender: Gender, | ||
|
|
||
| @field:NotNull(message = "Birth date is required") | ||
| @field:Past(message = "Birth date must be in the past") | ||
| val birthDate: LocalDate | ||
| ) | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
23 changes: 23 additions & 0 deletions
23
identity/src/main/kotlin/org/spendoo/identity/dto/request/ResetPasswordRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.Email | ||
| import jakarta.validation.constraints.NotBlank | ||
| import jakarta.validation.constraints.Pattern | ||
| import jakarta.validation.constraints.Size | ||
|
|
||
| data class ResetPasswordRequest( | ||
| @field:NotBlank(message = "Email is required") | ||
| @field:Email(message = "Invalid email format") | ||
| val email: String, | ||
|
|
||
| @field:NotBlank(message = "OTP is required") | ||
| @field:Size(min = 4, max = 4, message = "OTP must be exactly 4 characters") | ||
| val otp: String, | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @field:NotBlank(message = "New password is required") | ||
| @field:Pattern( | ||
| regexp = """^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!]).{8,}$""", | ||
| message = "Password must contain at least 8 characters, one uppercase, one lowercase, one number and one special character" | ||
| ) | ||
| val newPassword: String | ||
| ) | ||
15 changes: 15 additions & 0 deletions
15
identity/src/main/kotlin/org/spendoo/identity/dto/request/VerifyOtpRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.spendoo.identity.dto.request | ||
|
|
||
| import jakarta.validation.constraints.Email | ||
| import jakarta.validation.constraints.NotBlank | ||
| import jakarta.validation.constraints.Size | ||
|
|
||
| data class VerifyOtpRequest( | ||
| @field:NotBlank(message = "Email is required") | ||
| @field:Email(message = "Invalid email format") | ||
| val email: String, | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @field:NotBlank(message = "OTP is required") | ||
| @field:Size(min = 4, max = 4, message = "OTP must be exactly 4 characters") | ||
| val otp: String | ||
| ) | ||
6 changes: 6 additions & 0 deletions
6
identity/src/main/kotlin/org/spendoo/identity/dto/response/AuthResponse.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.spendoo.identity.dto.response | ||
|
|
||
| data class AuthResponse( | ||
| val accessToken: String, | ||
| val refreshToken: String | ||
| ) | ||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
35 changes: 35 additions & 0 deletions
35
identity/src/main/kotlin/org/spendoo/identity/entity/EmailVerification.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.spendoo.identity.entity | ||
|
|
||
| import jakarta.persistence.* | ||
| import java.time.LocalDateTime | ||
| import java.util.UUID | ||
|
|
||
| @Entity | ||
| @Table(name = "email_verification", schema = "identity") | ||
| data class EmailVerification( | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| @Column(columnDefinition = "uuid", updatable = false, nullable = false) | ||
| val id: UUID = UUID.randomUUID(), | ||
|
|
||
| @Column(nullable = false) | ||
| val verificationCode: String, | ||
|
|
||
| @Column(nullable = false) | ||
| val email: String, | ||
|
|
||
| @Column(nullable = false) | ||
| val sentAt: LocalDateTime, | ||
|
|
||
| @Column(nullable = false) | ||
| val isUsed: Boolean, | ||
|
|
||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(nullable = false) | ||
| val user: User | ||
| ) { | ||
| fun isExpired(): Boolean { | ||
| return sentAt.plusMinutes(1).isBefore(LocalDateTime.now()) | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
identity/src/main/kotlin/org/spendoo/identity/entity/RefreshToken.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.spendoo.identity.entity | ||
|
|
||
| import jakarta.persistence.* | ||
| import java.time.LocalDateTime | ||
| import java.util.UUID | ||
|
|
||
| @Entity | ||
| @Table(name = "refresh_token", schema = "identity") | ||
| data class RefreshToken( | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| val id: Long = 0, | ||
|
|
||
IsraaXx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Column(nullable = false, unique = true) | ||
| val token: String, | ||
|
|
||
| @Column(nullable = false) | ||
| val expiryDate: LocalDateTime, | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| val user: User | ||
| ) | ||
41 changes: 41 additions & 0 deletions
41
identity/src/main/kotlin/org/spendoo/identity/entity/User.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.spendoo.identity.entity | ||
|
|
||
| import jakarta.persistence.* | ||
| import org.spendoo.identity.enums.Gender | ||
| import java.time.LocalDate | ||
| import java.time.LocalDateTime | ||
| import java.util.UUID | ||
|
|
||
| @Entity | ||
| @Table(name = "users", schema = "identity" ) | ||
| data class User( | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| @Column(columnDefinition = "uuid", updatable = false, nullable = false) | ||
| val id: UUID = UUID.randomUUID(), | ||
|
|
||
| @Column(nullable = false) | ||
| val fullName: String, | ||
|
|
||
| @Column(nullable = false, unique = true) | ||
| val email: String, | ||
|
|
||
| @Column(nullable = false) | ||
| val passwordHash: String, | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false) | ||
| val gender: Gender, | ||
|
|
||
| @Column(nullable = false) | ||
| val birthDate: LocalDate, | ||
|
|
||
| @Column(nullable = false, updatable = false) | ||
| val createdAt: LocalDateTime, | ||
|
|
||
| @OneToMany(mappedBy = "user", cascade = [CascadeType.ALL], fetch = FetchType.LAZY) | ||
| val emailVerifications: List<EmailVerification> = emptyList(), | ||
|
|
||
| @OneToMany(mappedBy = "user", cascade = [CascadeType.ALL], orphanRemoval = true) | ||
| val refreshTokens: List<RefreshToken> = emptyList() | ||
| ) |
6 changes: 6 additions & 0 deletions
6
identity/src/main/kotlin/org/spendoo/identity/enums/Gender.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.spendoo.identity.enums | ||
|
|
||
| enum class Gender { | ||
| MALE, | ||
| FEMALE | ||
| } |
16 changes: 16 additions & 0 deletions
16
identity/src/main/kotlin/org/spendoo/identity/exception/AuthExceptions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.spendoo.identity.exception | ||
|
|
||
| class InvalidCredentialsException( | ||
| message: String = "Invalid email or password") : RuntimeException(message) | ||
|
|
||
|
|
||
| class TokenExpiredException ( | ||
| message: String = "Token has expired. Please login again.") : RuntimeException(message) | ||
|
|
||
|
|
||
| class UnauthorizedException ( | ||
| message: String = "Unauthorized access") : RuntimeException(message) | ||
|
|
||
| class UserAlreadyExistsException( | ||
| message: String = "User already exists") : RuntimeException(message) | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.