2021-11-08 17:30:56 +02:00
|
|
|
package com.tarkvaratehnika.demobackend.web.rest
|
|
|
|
|
2022-01-18 00:34:45 +02:00
|
|
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
2021-12-12 20:06:08 +02:00
|
|
|
import com.tarkvaratehnika.demobackend.config.SessionManager
|
|
|
|
import com.tarkvaratehnika.demobackend.dto.AuthDto
|
|
|
|
import com.tarkvaratehnika.demobackend.dto.AuthTokenDTO
|
2021-11-08 17:30:56 +02:00
|
|
|
import com.tarkvaratehnika.demobackend.security.AuthTokenDTOAuthenticationProvider
|
|
|
|
import com.tarkvaratehnika.demobackend.security.WebEidAuthentication
|
|
|
|
import org.slf4j.LoggerFactory
|
|
|
|
import org.springframework.http.HttpStatus
|
|
|
|
import org.springframework.http.MediaType
|
2022-01-18 00:34:45 +02:00
|
|
|
import org.springframework.http.ResponseEntity
|
2021-11-08 17:30:56 +02:00
|
|
|
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken
|
|
|
|
import org.springframework.web.bind.annotation.*
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("auth")
|
|
|
|
class AuthenticationController {
|
|
|
|
|
|
|
|
private val LOG = LoggerFactory.getLogger(AuthenticationController::class.java)
|
|
|
|
|
|
|
|
|
2021-12-06 23:01:10 +02:00
|
|
|
@PostMapping("login", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
|
2022-01-18 00:34:45 +02:00
|
|
|
fun authenticate(@RequestHeader headers: Map<String, String>, @RequestBody authTokenDTO: AuthTokenDTO): AuthDto {
|
2021-12-12 20:06:08 +02:00
|
|
|
val sessionId = SessionManager.getSessionId(headers)
|
|
|
|
|
2022-01-18 00:34:45 +02:00
|
|
|
// Check if an error occurred in the auth app.
|
|
|
|
if (authTokenDTO.error != null && authTokenDTO.error != 200) {
|
|
|
|
val auth = AuthDto(arrayListOf(), hashMapOf(), authTokenDTO.error)
|
|
|
|
SessionManager.addErrorToSession(sessionId, auth)
|
|
|
|
return auth
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:30:56 +02:00
|
|
|
// Create Spring Security Authentication object with supplied token as credentials.
|
2022-01-18 00:34:45 +02:00
|
|
|
val auth = PreAuthenticatedAuthenticationToken(null, authTokenDTO)
|
2021-11-08 17:30:56 +02:00
|
|
|
|
|
|
|
// Return authentication object if success.
|
2021-12-12 20:06:08 +02:00
|
|
|
return AuthTokenDTOAuthenticationProvider.authenticate(auth, sessionId)
|
2021-11-08 17:30:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-06 23:01:10 +02:00
|
|
|
|
|
|
|
@GetMapping("login", produces = [MediaType.APPLICATION_JSON_VALUE])
|
2022-01-18 00:34:45 +02:00
|
|
|
fun getAuthenticated(@RequestHeader headers: HashMap<String, String>) : ResponseEntity<String> {
|
2021-12-12 20:06:08 +02:00
|
|
|
return WebEidAuthentication.fromSession(headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("userData", produces = [MediaType.APPLICATION_JSON_VALUE])
|
|
|
|
fun getUserData(@RequestHeader headers: Map<String, String>) : AuthDto? {
|
|
|
|
return SessionManager.getSessionAuth(SessionManager.getSessionId(headers))
|
2021-11-08 17:30:56 +02:00
|
|
|
}
|
2021-12-06 23:39:13 +02:00
|
|
|
|
2021-12-14 19:50:11 +02:00
|
|
|
@PostMapping("logout")
|
|
|
|
fun logOut(@RequestHeader headers: Map<String, String>) : HttpStatus? {
|
2021-12-12 20:06:08 +02:00
|
|
|
SessionManager.removeRoleFromCurrentSession(headers)
|
2021-12-06 23:39:13 +02:00
|
|
|
return HttpStatus.ACCEPTED
|
|
|
|
|
|
|
|
}
|
2021-11-08 17:30:56 +02:00
|
|
|
}
|