Skip to content

Commit

Permalink
Whole project refactoring and formatting (#62)
Browse files Browse the repository at this point in the history
formatting and refactoring
  • Loading branch information
MocStepan authored Apr 29, 2024
1 parent 632a0e6 commit 605aa82
Show file tree
Hide file tree
Showing 44 changed files with 502 additions and 605 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.DefaultSecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.security.web.authentication.logout.LogoutHandler
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.CorsConfigurationSource

Expand All @@ -27,15 +25,13 @@ import org.springframework.web.cors.CorsConfigurationSource
@EnableMethodSecurity
class SecurityConfiguration(
private val objectMapper: ObjectMapper,
private val logoutSuccessHandler: LogoutSuccessHandler,
private val cookieClearingLogoutHandler: LogoutHandler,
private val jwtAuthenticationFilter: JwtAuthenticationFilter
) {

private val userUnsecuredEndpoints =
arrayOf(
"/api/v1/auth/login",
"/api/v1/auth/register",
"/api/v1/auth/signIn",
"/api/v1/auth/signUp",
"/api/v1/weather/current/*",
)

Expand Down Expand Up @@ -64,13 +60,6 @@ class SecurityConfiguration(
.exceptionHandling {
it.authenticationEntryPoint(authenticationExceptionHandler)
}
.logout {
it.logoutUrl("/api/auth/logout")
.addLogoutHandler(cookieClearingLogoutHandler)
.logoutSuccessHandler(logoutSuccessHandler)
.deleteCookies("access_token")
.permitAll()
}
.build()

val authenticationExceptionHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AccessTokenService(

val COOKIE_NAME = "access_token"

// does not work, fix in issue https://github.com/MocStepan/STIN-semestral-project/issues/55
private val maxAge = duration.milliseconds

private val jwtService = JwtService(secret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys


class JwtService(
secret: String
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tul.backend.auth.controller

import com.tul.backend.auth.dto.LoginDTO
import com.tul.backend.auth.dto.RegisterDTO
import com.tul.backend.auth.dto.SignInDTO
import com.tul.backend.auth.dto.SignUpDTO
import com.tul.backend.auth.service.AuthUserService
import jakarta.servlet.http.HttpServletResponse
import org.springframework.http.HttpStatus
Expand All @@ -18,21 +18,21 @@ class AuthUserController(
private val authUserService: AuthUserService,
) {

@PostMapping("/v1/auth/login")
fun login(
@RequestBody loginDTO: LoginDTO,
@PostMapping("/v1/auth/signIn")
fun signIn(
@RequestBody signInDTO: SignInDTO,
response: HttpServletResponse
): ResponseEntity<Boolean> {
val responseDTO = authUserService.login(loginDTO, response)
val responseDTO = authUserService.signIn(signInDTO, response)
val status = if (responseDTO) HttpStatus.OK else HttpStatus.NOT_FOUND
return ResponseEntity(responseDTO, status)
}

@PostMapping("/v1/auth/register")
fun register(
@RequestBody registerDTO: RegisterDTO,
@PostMapping("/v1/auth/signUp")
fun signUp(
@RequestBody signUpDTO: SignUpDTO,
): ResponseEntity<Boolean> {
val response = authUserService.register(registerDTO)
val response = authUserService.signUp(signUpDTO)
val status = if (response) HttpStatus.OK else HttpStatus.BAD_REQUEST
return ResponseEntity(response, status)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tul.backend.auth.dto

import com.tul.backend.auth.base.valueobject.EmailAddress

data class LoginDTO(
data class SignInDTO(
val email: EmailAddress,
val password: String,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tul.backend.auth.dto

import com.tul.backend.auth.base.valueobject.EmailAddress

data class RegisterDTO(
data class SignUpDTO(
val username: String,
val email: EmailAddress,
val password: String,
Expand Down
10 changes: 5 additions & 5 deletions backend/src/main/kotlin/com/tul/backend/auth/entity/AuthUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.tul.backend.auth.entity
import com.tul.backend.auth.base.valueobject.AuthUserRole
import com.tul.backend.auth.base.valueobject.AuthUserRole.USER
import com.tul.backend.auth.base.valueobject.EmailAddress
import com.tul.backend.auth.dto.RegisterDTO
import com.tul.backend.auth.dto.SignUpDTO
import com.tul.backend.weather.entity.UserWeatherLocation
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
Expand All @@ -27,11 +27,11 @@ class AuthUser(
val locations: List<UserWeatherLocation> = mutableListOf()
) {
companion object {
fun from(registerDTO: RegisterDTO): AuthUser {
fun from(signUpDTO: SignUpDTO): AuthUser {
return AuthUser(
username = registerDTO.username,
email = registerDTO.email,
password = registerDTO.password
username = signUpDTO.username,
email = signUpDTO.email,
password = signUpDTO.password
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ import org.springframework.stereotype.Repository
interface AuthUserRepository : JpaRepository<AuthUser, Long> {
fun findByEmail(email: String): AuthUser?

fun findByUsername(username: String?): AuthUser?

fun existsByEmail(email: String): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tul.backend.auth.service

import com.tul.backend.auth.dto.LoginDTO
import com.tul.backend.auth.dto.RegisterDTO
import com.tul.backend.auth.dto.SignInDTO
import com.tul.backend.auth.dto.SignUpDTO
import com.tul.backend.auth.repository.AuthUserRepository
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.servlet.http.HttpServletResponse
Expand All @@ -16,35 +16,35 @@ class AuthUserService(
private val authenticationHandler: AuthenticationHandler,
private val authUserRepository: AuthUserRepository
) {
fun login(loginDTO: LoginDTO, response: HttpServletResponse): Boolean {
if (!loginDTO.isValid()) {
log.warn { "LoginDTO: $loginDTO is invalid" }
fun signIn(signInDTO: SignInDTO, response: HttpServletResponse): Boolean {
if (!signInDTO.isValid()) {
log.warn { "SignInDTO: $signInDTO is invalid" }
return false
}

val authUser = authUserRepository.findByEmail(loginDTO.email.value)
val authUser = authUserRepository.findByEmail(signInDTO.email.value)
if (authUser == null) {
log.warn { "User with email: ${loginDTO.email} does not exist" }
log.warn { "User with email: ${signInDTO.email} does not exist" }
return false
}

return authenticationHandler.authenticate(loginDTO, authUser, response)
return authenticationHandler.authenticate(signInDTO, authUser, response)
}

fun register(registerDTO: RegisterDTO): Boolean {
if (!registerDTO.isValid()) {
log.warn { "RegisterDTO: $registerDTO is invalid" }
fun signUp(signUpDTO: SignUpDTO): Boolean {
if (!signUpDTO.isValid()) {
log.warn { "SignUpDTO: $signUpDTO is invalid" }
return false
}

val exists = authUserRepository.existsByEmail(registerDTO.email.value)
val exists = authUserRepository.existsByEmail(signUpDTO.email.value)
if (exists) {
log.warn { "User with email: ${registerDTO.email} already exists" }
log.warn { "User with email: ${signUpDTO.email} already exists" }
return false
}

authUserRepository.save(
authenticationHandler.hashRegistrationPassword(registerDTO)
authenticationHandler.hashSignUpPassword(signUpDTO)
)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.tul.backend.auth.service

import com.tul.backend.auth.base.service.AccessTokenService
import com.tul.backend.auth.base.service.CustomPasswordEncoder
import com.tul.backend.auth.dto.LoginDTO
import com.tul.backend.auth.dto.RegisterDTO
import com.tul.backend.auth.dto.SignInDTO
import com.tul.backend.auth.dto.SignUpDTO
import com.tul.backend.auth.entity.AuthUser
import jakarta.servlet.http.HttpServletResponse
import org.springframework.http.HttpHeaders
Expand All @@ -17,11 +17,11 @@ class AuthenticationHandler(
private val customPasswordEncoder: CustomPasswordEncoder
) {
fun authenticate(
loginDTO: LoginDTO,
signInDTO: SignInDTO,
authUser: AuthUser,
response: HttpServletResponse
): Boolean {
return if (customPasswordEncoder.matches(loginDTO.password, authUser.password)) {
return if (customPasswordEncoder.matches(signInDTO.password, authUser.password)) {
val claims = accessTokenService.createClaims(authUser)
val cookie = accessTokenService.createCookie(claims)
response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString())
Expand All @@ -31,8 +31,8 @@ class AuthenticationHandler(
}
}

fun hashRegistrationPassword(registerDTO: RegisterDTO): AuthUser {
val authUser = AuthUser.from(registerDTO)
fun hashSignUpPassword(signUpDTO: SignUpDTO): AuthUser {
val authUser = AuthUser.from(signUpDTO)
authUser.password = customPasswordEncoder.encode(authUser.password)
return authUser
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JwtAuthenticationFilterTests : FeatureSpec({

feature("doFilterInternal") {

scenario("login successfull") {
scenario("sign in successfull") {
val spec = getSpec()
val request = mockk<HttpServletRequest>()
val response = mockk<HttpServletResponse>()
Expand All @@ -34,7 +34,7 @@ class JwtAuthenticationFilterTests : FeatureSpec({
verify { filterChain.doFilter(request, response) }
}

scenario("login failed") {
scenario("sign in failed") {
val spec = getSpec()
val request = mockk<HttpServletRequest>()
val response = mockk<HttpServletResponse>()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ class SecurityConfigurationTests : FeatureSpec({

scenario("securityFilterChain configuration") {
val objectMapper = ObjectMapper()
val jwtConfiguration = JwtConfiguration()
val logoutSuccessHandler = jwtConfiguration.logoutSuccessHandler()
val cookieClearingLogoutHandler = jwtConfiguration.cookieClearingLogoutHandler()
val jwtAuthenticationFilter = mockk<JwtAuthenticationFilter>()

val securityConfiguration = SecurityConfiguration(
objectMapper,
logoutSuccessHandler,
cookieClearingLogoutHandler,
jwtAuthenticationFilter
)

Expand All @@ -52,15 +47,10 @@ class SecurityConfigurationTests : FeatureSpec({

scenario("authenticationExceptionHandler function") {
val objectMapper = ObjectMapper()
val jwtConfiguration = JwtConfiguration()
val logoutSuccessHandler = jwtConfiguration.logoutSuccessHandler()
val cookieClearingLogoutHandler = jwtConfiguration.cookieClearingLogoutHandler()
val jwtAuthenticationFilter = mockk<JwtAuthenticationFilter>()

val securityConfiguration = SecurityConfiguration(
objectMapper,
logoutSuccessHandler,
cookieClearingLogoutHandler,
jwtAuthenticationFilter
)
val errorDTO = ErrorDTO("Unauthorized")
Expand Down
Loading

0 comments on commit 605aa82

Please sign in to comment.