Skip to content
Snippets Groups Projects
Verified Commit ed43f699 authored by Jan-Niclas Strüwer's avatar Jan-Niclas Strüwer
Browse files

marked cookie as optional

parent ae203abe
No related branches found
No related tags found
No related merge requests found
Pipeline #27239 passed
...@@ -5,6 +5,7 @@ import com.auth0.jwt.JWTVerifier ...@@ -5,6 +5,7 @@ import com.auth0.jwt.JWTVerifier
import com.auth0.jwt.algorithms.Algorithm import com.auth0.jwt.algorithms.Algorithm
import com.auth0.jwt.exceptions.JWTCreationException import com.auth0.jwt.exceptions.JWTCreationException
import com.auth0.jwt.exceptions.JWTVerificationException import com.auth0.jwt.exceptions.JWTVerificationException
import de.fraunhofer.iem.dataprovider.logger.getLogger
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
data class JwtContentDto(val gitlabId: Long) { data class JwtContentDto(val gitlabId: Long) {
...@@ -16,8 +17,14 @@ data class JwtContentDto(val gitlabId: Long) { ...@@ -16,8 +17,14 @@ data class JwtContentDto(val gitlabId: Long) {
@Service @Service
class JwtService(private val securityProperties: SecurityProperties) { class JwtService(private val securityProperties: SecurityProperties) {
val algorithm: Algorithm = Algorithm.HMAC512(securityProperties.hmacKey) private val algorithm: Algorithm = Algorithm.HMAC512(securityProperties.hmacKey)
val issuer: String = "IEM-dataprovider" private val issuer: String = "IEM-dataprovider"
private val verifier: JWTVerifier = JWT.require(algorithm) // specify an specific claim validations
.withIssuer(issuer) // reusable verifier instance
.build()
private val logger = getLogger(javaClass)
fun createJWT(content: JwtContentDto): String? { fun createJWT(content: JwtContentDto): String? {
return try { return try {
...@@ -27,7 +34,7 @@ class JwtService(private val securityProperties: SecurityProperties) { ...@@ -27,7 +34,7 @@ class JwtService(private val securityProperties: SecurityProperties) {
.sign(algorithm) .sign(algorithm)
} catch (exception: JWTCreationException) { } catch (exception: JWTCreationException) {
// Invalid Signing configuration / Couldn't convert Claims. // Invalid Signing configuration / Couldn't convert Claims.
println("JWT creation for id $content failed") logger.error("JWT creation for id $content failed with $exception")
null null
} }
} }
...@@ -35,16 +42,13 @@ class JwtService(private val securityProperties: SecurityProperties) { ...@@ -35,16 +42,13 @@ class JwtService(private val securityProperties: SecurityProperties) {
fun getContentIfValid(encodedJwt: String): JwtContentDto? { fun getContentIfValid(encodedJwt: String): JwtContentDto? {
return try { return try {
val verifier: JWTVerifier = JWT.require(algorithm) // specify an specific claim validations
.withIssuer(issuer) // reusable verifier instance
.build()
val decodedJWT = verifier.verify(encodedJwt) val decodedJWT = verifier.verify(encodedJwt)
JwtContentDto( JwtContentDto(
gitlabId = decodedJWT.getClaim(JwtContentDto.GITLAB_ID_CLAIM_NAME).asLong() gitlabId = decodedJWT.getClaim(JwtContentDto.GITLAB_ID_CLAIM_NAME).asLong()
) )
} catch (exception: JWTVerificationException) { } catch (exception: JWTVerificationException) {
// Invalid signature/claims // Invalid signature/claims
logger.error("JWT verification failed with exception $exception")
null null
} }
} }
......
...@@ -135,10 +135,10 @@ class RepositoryController( ...@@ -135,10 +135,10 @@ class RepositoryController(
@GetMapping(ApiPaths.REPOSITORY_TOOL_RUN) @GetMapping(ApiPaths.REPOSITORY_TOOL_RUN)
suspend fun getToolRunForRepository( suspend fun getToolRunForRepository(
@CookieValue("gitlabIdToken") gitlabCookie: String, @CookieValue(name = "gitlabIdToken", required = false) gitlabCookie: String,
@PathVariable id: Long @PathVariable id: Long
): ToolRunResponseDto { ): ToolRunResponseDto {
println("Cookie value $gitlabCookie") logger.info("Cookie value $gitlabCookie")
val isProjectMember = jwtService.getContentIfValid(gitlabCookie)?.let { val isProjectMember = jwtService.getContentIfValid(gitlabCookie)?.let {
gitlabApi.userIsProjectMember(projectId = id, gitlabUserId = it.gitlabId) gitlabApi.userIsProjectMember(projectId = id, gitlabUserId = it.gitlabId)
...@@ -164,7 +164,7 @@ class RepositoryController( ...@@ -164,7 +164,7 @@ class RepositoryController(
} }
@PostMapping(ApiPaths.REPOSITORY_VALIDATE_USER) @PostMapping(ApiPaths.REPOSITORY_VALIDATE_USER)
suspend fun validateUserLogin( suspend fun getGitlabCookie(
@PathVariable id: Long, @PathVariable id: Long,
@RequestBody validateDto: ValidateUserDto @RequestBody validateDto: ValidateUserDto
): ResponseEntity<OkResponseDto> { ): ResponseEntity<OkResponseDto> {
...@@ -183,6 +183,4 @@ class RepositoryController( ...@@ -183,6 +183,4 @@ class RepositoryController(
.header(HttpHeaders.SET_COOKIE, gitlabCookie.toString()) .header(HttpHeaders.SET_COOKIE, gitlabCookie.toString())
.body(OkResponseDto()) .body(OkResponseDto())
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.