-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CookieTokenFilter for lokal utvikling
Siste oppgradering av token-validation-spring (v4) fjernet støtte for tokens som ligger i en cookie. Det var vår måte å "logge inn" på i lokal utvikling. Ved å redusere "presedensen" til valideringsfilteret og sette inn et "CookieTokenFilter" foran, kan vi manuelt sette inn et token i auth-header som fanges opp av valideringsfilteret senere.
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/test/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/CookieTokenFilter.kt
This file contains 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,49 @@ | ||
package no.nav.arbeidsgiver.tiltakrefusjon | ||
|
||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletRequestWrapper | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.core.Ordered | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
/** | ||
* Dette filteret gjør at lokalkjøring av applikasjonen fortsatt fungerer med cookie-baserte | ||
* jwt-tokens. Frontendene har en lokal login-mekanisme når man kjører i dev-modus som henter | ||
* et fake JWT access-token og legger det i en cookie. | ||
* | ||
* Tidligere ble denne cookien plukket opp av token-validation-biblioteket, men siden versjon 4 | ||
* har støtte for cookies blitt fjernet. | ||
* | ||
* Ved å sørge for at dette filteret kjører før TokenValidationFilter sniker vi inn en auth-header | ||
* som deretter plukkes opp av token-validering senere. | ||
*/ | ||
@Component | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
class CookieTokenFilter: OncePerRequestFilter() { | ||
override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) { | ||
val aadToken = request.cookies?.find { it.name == "aad-token" }?.value | ||
val tokenxToken = request.cookies?.find { it.name == "tokenx-token" }?.value | ||
|
||
if (request.requestURI.startsWith("/api/arbeidsgiver") && tokenxToken != null) { | ||
val wrappedRequest = RequestMedToken(request, tokenxToken) | ||
filterChain.doFilter(wrappedRequest, response) | ||
} else if (request.requestURI.startsWith("/api/saksbehandler") && aadToken != null) { | ||
val wrappedRequest = RequestMedToken(request, aadToken) | ||
filterChain.doFilter(wrappedRequest, response) | ||
} else { | ||
filterChain.doFilter(request, response); | ||
} | ||
} | ||
} | ||
|
||
private class RequestMedToken(request: HttpServletRequest, val token: String): HttpServletRequestWrapper(request) { | ||
override fun getHeader(name: String?): String? { | ||
if (name?.lowercase() == "authorization") { | ||
return "Bearer $token" | ||
} | ||
return super.getHeader(name) | ||
} | ||
} |
This file contains 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 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