-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translated JwtTokenValidationHandler
- Loading branch information
1 parent
6728507
commit cdead5f
Showing
13 changed files
with
82 additions
and
98 deletions.
There are no files selected for viewing
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
86 changes: 0 additions & 86 deletions
86
...rc/main/java/no/nav/security/token/support/core/validation/JwtTokenValidationHandler.java
This file was deleted.
Oops, something went wrong.
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
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
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
64 changes: 64 additions & 0 deletions
64
...rc/main/kotlin/no/nav/security/token/support/core/validation/JwtTokenValidationHandler.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,64 @@ | ||
package no.nav.security.token.support.core.validation | ||
|
||
import java.util.AbstractMap.SimpleImmutableEntry | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.collections.Map.Entry | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import no.nav.security.token.support.core.configuration.MultiIssuerConfiguration | ||
import no.nav.security.token.support.core.context.TokenValidationContext | ||
import no.nav.security.token.support.core.exceptions.IssuerConfigurationException | ||
import no.nav.security.token.support.core.exceptions.JwtTokenValidatorException | ||
import no.nav.security.token.support.core.http.HttpRequest | ||
import no.nav.security.token.support.core.jwt.JwtToken | ||
import no.nav.security.token.support.core.validation.JwtTokenRetriever.retrieveUnvalidatedTokens | ||
import no.nav.security.token.support.core.validation.JwtTokenValidatorFactory.tokenValidator | ||
|
||
class JwtTokenValidationHandler(private val config : MultiIssuerConfiguration) { | ||
|
||
fun getValidatedTokens(request : HttpRequest) = | ||
retrieveUnvalidatedTokens(config, request).run { | ||
with(mapNotNull(::validate) | ||
.associateByTo(ConcurrentHashMap(), { it.key }, { it.value })) { | ||
LOG.debug("Found {} tokens on request, number of validated tokens is {}", size, this@with.size) | ||
if (this@with.isEmpty() && isNotEmpty()) { | ||
LOG.debug("Found {} unvalidated token(s) with issuer(s) {} on request, is this a configuration error?", size, map(JwtToken::issuer)) | ||
} | ||
TokenValidationContext(this) | ||
} | ||
} | ||
|
||
private fun validate(jwtToken : JwtToken) : Entry<String, JwtToken>? { | ||
with(jwtToken) { | ||
try { | ||
LOG.debug("Check if token with issuer={} is present in config", issuer) | ||
if (config.getIssuer(issuer).isPresent) { | ||
val issuerShortName = issuerConfiguration(issuer).name | ||
LOG.debug("Found token from trusted issuer={} with shortName={} in request", issuer, issuerShortName) | ||
tokenValidator(jwtToken).assertValidToken(encodedToken) | ||
LOG.debug("Validated token from issuer[{}]", issuer) | ||
return SimpleImmutableEntry(issuerShortName, this) | ||
} | ||
return null.also { | ||
LOG.info("Found token from unknown issuer[{}], skipping validation.", issuer) | ||
} | ||
} | ||
catch (e : JwtTokenValidatorException) { | ||
return null.also { | ||
LOG.info("Found invalid token for issuer [{}, expires at {}], message:{} ",issuer, e.expiryDate, e.message) | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
private fun tokenValidator(jwtToken : JwtToken) = issuerConfiguration(jwtToken.issuer).tokenValidator | ||
|
||
private fun issuerConfiguration(issuer : String) = config.getIssuer(issuer) | ||
.orElseThrow { IssuerConfigurationException("Could not find IssuerConfiguration for issuer $issuer") } | ||
|
||
companion object { | ||
private val LOG : Logger = LoggerFactory.getLogger(JwtTokenValidationHandler::class.java) | ||
} | ||
} |
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
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