unused removed
This commit is contained in:
parent
5f0a7fe2a3
commit
60fe7d5abb
|
|
@ -1,7 +1,6 @@
|
||||||
package com.kassaev.notes.config
|
package com.kassaev.notes.config
|
||||||
|
|
||||||
import com.kassaev.notes.repository.IUserRepository
|
import com.kassaev.notes.repository.IUserRepository
|
||||||
import com.kassaev.notes.repository.UserRepository
|
|
||||||
import com.kassaev.notes.service.CustomUserDetailsService
|
import com.kassaev.notes.service.CustomUserDetailsService
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class AuthController(
|
||||||
fun authenticate(@RequestBody authRequest: AuthenticationRequest): AuthenticationResponse =
|
fun authenticate(@RequestBody authRequest: AuthenticationRequest): AuthenticationResponse =
|
||||||
authenticationService.authentication(authRequest)
|
authenticationService.authentication(authRequest)
|
||||||
|
|
||||||
@PostMapping("/refresh_old")
|
@PostMapping("/refresh")
|
||||||
fun refreshAccessToken(
|
fun refreshAccessToken(
|
||||||
@RequestBody request: RefreshTokenRequest
|
@RequestBody request: RefreshTokenRequest
|
||||||
): TokenResponse =
|
): TokenResponse =
|
||||||
|
|
@ -26,14 +26,6 @@ class AuthController(
|
||||||
?.mapToTokenResponse()
|
?.mapToTokenResponse()
|
||||||
?: throw ResponseStatusException(HttpStatus.FORBIDDEN, "Invalid refresh token!")
|
?: throw ResponseStatusException(HttpStatus.FORBIDDEN, "Invalid refresh token!")
|
||||||
|
|
||||||
@PostMapping("/refresh")
|
|
||||||
fun refreshAccessToken2(
|
|
||||||
@RequestBody request: RefreshTokenRequest
|
|
||||||
): TokenResponse =
|
|
||||||
authenticationService.refreshAccessToken2(request.token)
|
|
||||||
?.mapToTokenResponse()
|
|
||||||
?: throw ResponseStatusException(HttpStatus.FORBIDDEN, "Invalid refresh token!")
|
|
||||||
|
|
||||||
private fun String.mapToTokenResponse(): TokenResponse =
|
private fun String.mapToTokenResponse(): TokenResponse =
|
||||||
TokenResponse(
|
TokenResponse(
|
||||||
token = this
|
token = this
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,7 @@ package com.kassaev.notes.controller.note
|
||||||
import com.kassaev.notes.model.Note
|
import com.kassaev.notes.model.Note
|
||||||
import com.kassaev.notes.service.NoteService
|
import com.kassaev.notes.service.NoteService
|
||||||
import lombok.AllArgsConstructor
|
import lombok.AllArgsConstructor
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.web.bind.annotation.*
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping
|
|
||||||
import org.springframework.web.bind.annotation.PutMapping
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
|
||||||
import org.springframework.web.bind.annotation.RestController
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
@ -19,9 +11,6 @@ import java.util.*
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
class NoteController(val service: NoteService) {
|
class NoteController(val service: NoteService) {
|
||||||
|
|
||||||
// @Autowired
|
|
||||||
// lateinit var service: NoteService
|
|
||||||
|
|
||||||
@GetMapping("/all")
|
@GetMapping("/all")
|
||||||
fun getAllNotes(): List<Note> {
|
fun getAllNotes(): List<Note> {
|
||||||
return service.getAllNotes()
|
return service.getAllNotes()
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,9 @@ package com.kassaev.notes.controller.user
|
||||||
import com.kassaev.notes.model.Role
|
import com.kassaev.notes.model.Role
|
||||||
import com.kassaev.notes.model.User
|
import com.kassaev.notes.model.User
|
||||||
import com.kassaev.notes.service.UserService
|
import com.kassaev.notes.service.UserService
|
||||||
import org.springframework.data.jpa.domain.AbstractPersistable_.id
|
|
||||||
import org.springframework.http.HttpStatus
|
import org.springframework.http.HttpStatus
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder
|
import org.springframework.security.crypto.password.PasswordEncoder
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping
|
import org.springframework.web.bind.annotation.*
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable
|
|
||||||
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
|
|
||||||
import org.springframework.web.server.ResponseStatusException
|
import org.springframework.web.server.ResponseStatusException
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package com.kassaev.notes.model
|
||||||
import jakarta.persistence.Entity
|
import jakarta.persistence.Entity
|
||||||
import jakarta.persistence.Id
|
import jakarta.persistence.Id
|
||||||
import jakarta.persistence.Table
|
import jakarta.persistence.Table
|
||||||
import org.springframework.security.core.userdetails.UserDetails
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "refresh_tokens")
|
@Table(name = "refresh_tokens")
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,6 @@ import com.kassaev.notes.model.User
|
||||||
import org.springframework.data.jpa.repository.JpaRepository
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
import org.springframework.data.jpa.repository.Query
|
import org.springframework.data.jpa.repository.Query
|
||||||
import org.springframework.data.repository.query.Param
|
import org.springframework.data.repository.query.Param
|
||||||
import java.awt.print.Book
|
|
||||||
import java.time.LocalDate
|
|
||||||
|
|
||||||
|
|
||||||
interface IUserRepository: JpaRepository<User, Long> {
|
interface IUserRepository: JpaRepository<User, Long> {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package com.kassaev.notes.repository
|
|
||||||
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails
|
|
||||||
import org.springframework.stereotype.Repository
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
class RefreshTokenRepository {
|
|
||||||
|
|
||||||
private val tokens = mutableMapOf<String, UserDetails>()
|
|
||||||
|
|
||||||
fun findUserDetailsByToken(token: String): UserDetails? =
|
|
||||||
tokens[token]
|
|
||||||
|
|
||||||
fun save(token: String, userDetails: UserDetails) {
|
|
||||||
tokens[token] = userDetails
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
package com.kassaev.notes.repository
|
|
||||||
|
|
||||||
import com.kassaev.notes.model.Role
|
|
||||||
import com.kassaev.notes.model.User
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder
|
|
||||||
import org.springframework.stereotype.Repository
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
class UserRepository(
|
|
||||||
private val encoder: PasswordEncoder
|
|
||||||
) {
|
|
||||||
private val users = mutableListOf(
|
|
||||||
User(
|
|
||||||
id = 1,
|
|
||||||
email = "user1@mail.com",
|
|
||||||
password = encoder.encode("pass1"),
|
|
||||||
role = Role.USER
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id = 2,
|
|
||||||
email = "user2@mail.com",
|
|
||||||
password = encoder.encode("pass2"),
|
|
||||||
role = Role.USER
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id = 3,
|
|
||||||
email = "user3@mail.com",
|
|
||||||
password = encoder.encode("pass3"),
|
|
||||||
role = Role.ADMIN
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
fun save(user: User): Boolean {
|
|
||||||
val updated = user.copy(password = encoder.encode(
|
|
||||||
user.password
|
|
||||||
))
|
|
||||||
return users.add(updated)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun findByEmail(email: String): User? =
|
|
||||||
users.firstOrNull { it.email == email }
|
|
||||||
|
|
||||||
fun findById(id: Long): User? =
|
|
||||||
users.firstOrNull { it.id == id }
|
|
||||||
|
|
||||||
fun findAll(): List<User> =
|
|
||||||
users
|
|
||||||
|
|
||||||
fun deleteById(id: Long): Boolean {
|
|
||||||
val foundUser = findById(id)
|
|
||||||
|
|
||||||
return foundUser?.let {
|
|
||||||
users.remove(it)
|
|
||||||
} ?: false
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -5,12 +5,11 @@ import com.kassaev.notes.controller.auth.AuthenticationRequest
|
||||||
import com.kassaev.notes.controller.auth.AuthenticationResponse
|
import com.kassaev.notes.controller.auth.AuthenticationResponse
|
||||||
import com.kassaev.notes.model.RefreshToken
|
import com.kassaev.notes.model.RefreshToken
|
||||||
import com.kassaev.notes.repository.IRefreshTokenRepository
|
import com.kassaev.notes.repository.IRefreshTokenRepository
|
||||||
import com.kassaev.notes.repository.RefreshTokenRepository
|
|
||||||
import org.springframework.security.authentication.AuthenticationManager
|
import org.springframework.security.authentication.AuthenticationManager
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
||||||
import org.springframework.security.core.userdetails.UserDetails
|
import org.springframework.security.core.userdetails.UserDetails
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.util.Date
|
import java.util.*
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class AuthenticationService(
|
class AuthenticationService(
|
||||||
|
|
@ -18,8 +17,7 @@ class AuthenticationService(
|
||||||
private val userDetailsService: CustomUserDetailsService,
|
private val userDetailsService: CustomUserDetailsService,
|
||||||
private val tokenService: TokenService,
|
private val tokenService: TokenService,
|
||||||
private val jwtProperties: JwtProperties,
|
private val jwtProperties: JwtProperties,
|
||||||
private val refreshTokenRepository: RefreshTokenRepository,
|
private val refreshTokenRepository: IRefreshTokenRepository
|
||||||
private val refreshTokenRepository2: IRefreshTokenRepository
|
|
||||||
) {
|
) {
|
||||||
fun authentication(authRequest: AuthenticationRequest): AuthenticationResponse {
|
fun authentication(authRequest: AuthenticationRequest): AuthenticationResponse {
|
||||||
authManager.authenticate(
|
authManager.authenticate(
|
||||||
|
|
@ -34,13 +32,12 @@ class AuthenticationService(
|
||||||
val accessToken = generateAccessToken(user)
|
val accessToken = generateAccessToken(user)
|
||||||
val refreshToken = generateRefreshToken(user)
|
val refreshToken = generateRefreshToken(user)
|
||||||
|
|
||||||
refreshTokenRepository2.save(
|
refreshTokenRepository.save(
|
||||||
RefreshToken(
|
RefreshToken(
|
||||||
email = user.username,
|
email = user.username,
|
||||||
token = refreshToken
|
token = refreshToken
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
// refreshTokenRepository2.save(refreshToken, user)
|
|
||||||
|
|
||||||
return AuthenticationResponse(
|
return AuthenticationResponse(
|
||||||
accessToken = accessToken,
|
accessToken = accessToken,
|
||||||
|
|
@ -63,49 +60,13 @@ class AuthenticationService(
|
||||||
|
|
||||||
return extractedEmail?.let { email ->
|
return extractedEmail?.let { email ->
|
||||||
val currentUserDetails = userDetailsService.loadUserByUsername(email)
|
val currentUserDetails = userDetailsService.loadUserByUsername(email)
|
||||||
val refreshTokenUserDetails = refreshTokenRepository.findUserDetailsByToken(token)
|
|
||||||
|
|
||||||
if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenUserDetails?.username)
|
val refreshTokenInDB = refreshTokenRepository.findByEmail(currentUserDetails.username)
|
||||||
// TODO: check if this refresh token is the same token in db for this user <refresh token, user>. If so generate new access token and refresh token and update record in with newly created refresh token for this user.
|
|
||||||
|
if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenInDB?.email)
|
||||||
generateAccessToken(currentUserDetails)
|
generateAccessToken(currentUserDetails)
|
||||||
else
|
else
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun refreshAccessToken2(token: String): String? {
|
|
||||||
val extractedEmail = tokenService.extractEmail(token)
|
|
||||||
|
|
||||||
println()
|
|
||||||
println(extractedEmail)
|
|
||||||
println()
|
|
||||||
|
|
||||||
return extractedEmail?.let { email ->
|
|
||||||
val currentUserDetails = userDetailsService.loadUserByUsername(email)
|
|
||||||
|
|
||||||
println()
|
|
||||||
println("AAAAAAAAAAAAAAAA")
|
|
||||||
println(currentUserDetails.username)
|
|
||||||
println()
|
|
||||||
|
|
||||||
val refreshTokenInDB = refreshTokenRepository2.findByEmail(currentUserDetails.username)
|
|
||||||
// val refreshTokenUserDetails = refreshTokenRepository2.findUserDetailsByToken(token)
|
|
||||||
|
|
||||||
println()
|
|
||||||
println("BBBBBBBBBBB")
|
|
||||||
println(refreshTokenInDB?.email)
|
|
||||||
println()
|
|
||||||
|
|
||||||
if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenInDB?.email){
|
|
||||||
|
|
||||||
// TODO: check if this refresh token is the same token in db for this user <refresh token, user>. If so generate new access token and refresh token and update record in with newly created refresh token for this user.
|
|
||||||
println()
|
|
||||||
println("PPPPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAASSSSSSSSSSSSSSSEEEEEEEEEEEEEEEDDDDDDDDDDDD!!!")
|
|
||||||
println()
|
|
||||||
generateAccessToken(currentUserDetails)
|
|
||||||
|
|
||||||
} else
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package com.kassaev.notes.service
|
package com.kassaev.notes.service
|
||||||
|
|
||||||
import com.kassaev.notes.model.Note
|
import com.kassaev.notes.model.Note
|
||||||
//import com.kassaev.notes.repository.MockRepository
|
|
||||||
import com.kassaev.notes.repository.INoteRepository
|
import com.kassaev.notes.repository.INoteRepository
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
@ -11,21 +10,17 @@ class NoteService(val repository: INoteRepository) {
|
||||||
|
|
||||||
fun getAllNotes(): List<Note>{
|
fun getAllNotes(): List<Note>{
|
||||||
return repository.findAll()
|
return repository.findAll()
|
||||||
// return repository.getAllNotes()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getNoteById(id: Long): Optional<Note> {
|
fun getNoteById(id: Long): Optional<Note> {
|
||||||
return repository.findById(id)
|
return repository.findById(id)
|
||||||
// return repository.getNoteById(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateNote(note: Note): Note {
|
fun updateNote(note: Note): Note {
|
||||||
return repository.save(note)
|
return repository.save(note)
|
||||||
// return repository.updateNote(note)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteNote(id: Long){
|
fun deleteNote(id: Long){
|
||||||
repository.deleteById(id)
|
repository.deleteById(id)
|
||||||
// repository.deleteNote(id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -12,11 +12,7 @@ import java.util.Date
|
||||||
class TokenService(
|
class TokenService(
|
||||||
jwtProperties: JwtProperties
|
jwtProperties: JwtProperties
|
||||||
) {
|
) {
|
||||||
val sekret = jwtProperties.key
|
|
||||||
|
|
||||||
|
|
||||||
private val secretKey = Keys.hmacShaKeyFor(
|
private val secretKey = Keys.hmacShaKeyFor(
|
||||||
|
|
||||||
jwtProperties.key.toByteArray()
|
jwtProperties.key.toByteArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue