Skip to content

Commit

Permalink
style: format according to ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
tronghn committed Apr 17, 2024
1 parent 8a008ab commit 09e0051
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 50 deletions.
18 changes: 10 additions & 8 deletions wonderwalled-azure/src/main/kotlin/io/nais/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import io.nais.common.OpenIdConfiguration
import io.nais.common.defaultHttpClient
import io.nais.common.getOpenIdConfiguration

private val config = systemProperties() overriding
EnvironmentVariables()
private val config =
systemProperties() overriding
EnvironmentVariables()

data class Configuration(
val port: Int = config.getOrElse(Key("application.port", intType), 8080),
val azure: Azure = Azure(),
// optional, generally only needed when running locally
val ingress: String = config.getOrElse(
key = Key("wonderwall.ingress", stringType),
default = "",
),
val ingress: String =
config.getOrElse(
key = Key("wonderwall.ingress", stringType),
default = "",
),
) {
data class Azure(
val clientId: String = config[Key("azure.app.client.id", stringType)],
val clientSecret: String = config[Key("azure.app.client.secret", stringType)],
val wellKnownConfigurationUrl: String = config[Key("azure.app.well.known.url", stringType)],
val openIdConfiguration: OpenIdConfiguration = defaultHttpClient()
.getOpenIdConfiguration(wellKnownConfigurationUrl),
val openIdConfiguration: OpenIdConfiguration =
defaultHttpClient().getOpenIdConfiguration(wellKnownConfigurationUrl),
)
}
16 changes: 9 additions & 7 deletions wonderwalled-azure/src/main/kotlin/io/nais/Wonderwalled.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ fun main() {

fun Application.wonderwalled(config: Configuration) {
val jwksUrl = URI.create(config.azure.openIdConfiguration.jwksUri).toURL()
val jwkProvider = JwkProviderBuilder(jwksUrl)
.cached(10, 1, TimeUnit.HOURS)
.rateLimited(10, 1, TimeUnit.MINUTES)
.build()
val jwkProvider =
JwkProviderBuilder(jwksUrl)
.cached(10, 1, TimeUnit.HOURS)
.rateLimited(10, 1, TimeUnit.MINUTES)
.build()

commonSetup()

Expand All @@ -57,9 +58,10 @@ fun Application.wonderwalled(config: Configuration) {

// challenge is called if the request authentication fails or is not provided
challenge { _, _ ->
val ingress = config.ingress.ifEmpty(defaultValue = {
"${call.request.local.scheme}://${call.request.host()}"
})
val ingress =
config.ingress.ifEmpty(defaultValue = {
"${call.request.local.scheme}://${call.request.host()}"
})

// redirect to login endpoint (wonderwall) and indicate that the user should be redirected back
// to the original request path after authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import io.ktor.server.auth.authentication
import io.ktor.server.auth.jwt.JWTPrincipal
import io.ktor.server.auth.parseAuthorizationHeader

fun ApplicationCall.getTokenInfo(): Map<String, JsonNode>? = authentication
.principal<JWTPrincipal>()?.let { principal ->
fun ApplicationCall.getTokenInfo(): Map<String, JsonNode>? =
authentication.principal<JWTPrincipal>()?.let { principal ->
principal.payload.claims.entries.associate { claim ->
claim.key to claim.value.`as`(JsonNode::class.java)
}
}

fun ApplicationCall.requestHeaders(): Map<String, String> = request.headers.entries()
.associate { header -> header.key to header.value.joinToString() }
fun ApplicationCall.requestHeaders(): Map<String, String> =
request.headers.entries().associate { header -> header.key to header.value.joinToString() }

fun ApplicationCall.bearerToken(): String? = request
.parseAuthorizationHeader()
?.let { it as HttpAuthHeader.Single }
?.blob
fun ApplicationCall.bearerToken(): String? =
request
.parseAuthorizationHeader()
?.let { it as HttpAuthHeader.Single }
?.blob
26 changes: 14 additions & 12 deletions wonderwalled-common/src/main/kotlin/io/nais/common/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import io.ktor.client.request.get
import io.ktor.serialization.jackson.jackson
import kotlinx.coroutines.runBlocking

fun defaultHttpClient() = HttpClient(Apache) {
expectSuccess = true
install(ContentNegotiation) {
jackson {
deserializationConfig.apply {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
fun defaultHttpClient() =
HttpClient(Apache) {
expectSuccess = true
install(ContentNegotiation) {
jackson {
deserializationConfig.apply {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}
}
}
}

fun HttpClient.getOpenIdConfiguration(url: String): OpenIdConfiguration = runBlocking {
get(url)
.body<OpenIdConfiguration>()
.also { it.validate(url) }
}
fun HttpClient.getOpenIdConfiguration(url: String): OpenIdConfiguration =
runBlocking {
get(url)
.body<OpenIdConfiguration>()
.also { it.validate(url) }
}
17 changes: 9 additions & 8 deletions wonderwalled-idporten/src/main/kotlin/io/nais/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ data class Configuration(
val idporten: IdPorten = IdPorten(),
val tokenx: TokenX = TokenX(),
// optional, generally only needed when running locally
val ingress: String = config.getOrElse(
key = Key("wonderwall.ingress", stringType),
default = "",
),
val ingress: String =
config.getOrElse(
key = Key("wonderwall.ingress", stringType),
default = "",
),
) {
data class IdPorten(
val clientId: String = config[Key("idporten.client.id", stringType)],
val wellKnownConfigurationUrl: String = config[Key("idporten.well.known.url", stringType)],
val openIdConfiguration: OpenIdConfiguration = defaultHttpClient()
.getOpenIdConfiguration(wellKnownConfigurationUrl),
val openIdConfiguration: OpenIdConfiguration =
defaultHttpClient().getOpenIdConfiguration(wellKnownConfigurationUrl),
)

data class TokenX(
val clientId: String = config[Key("token.x.client.id", stringType)],
val privateJwk: String = config[Key("token.x.private.jwk", stringType)],
val wellKnownConfigurationUrl: String = config[Key("token.x.well.known.url", stringType)],
val openIdConfiguration: OpenIdConfiguration = defaultHttpClient()
.getOpenIdConfiguration(wellKnownConfigurationUrl),
val openIdConfiguration: OpenIdConfiguration =
defaultHttpClient().getOpenIdConfiguration(wellKnownConfigurationUrl),
val rsaKey: RSAKey = RSAKey.parse(privateJwk),
)
}
16 changes: 9 additions & 7 deletions wonderwalled-idporten/src/main/kotlin/io/nais/Wonderwalled.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ fun main() {

fun Application.wonderwalled(config: Configuration) {
val jwksURL = URI.create(config.idporten.openIdConfiguration.jwksUri).toURL()
val jwkProvider = JwkProviderBuilder(jwksURL)
.cached(10, 1, TimeUnit.HOURS)
.rateLimited(10, 1, TimeUnit.MINUTES)
.build()
val jwkProvider =
JwkProviderBuilder(jwksURL)
.cached(10, 1, TimeUnit.HOURS)
.rateLimited(10, 1, TimeUnit.MINUTES)
.build()

commonSetup()

Expand All @@ -58,9 +59,10 @@ fun Application.wonderwalled(config: Configuration) {

// challenge is called if the request authentication fails or is not provided
challenge { _, _ ->
val ingress = config.ingress.ifEmpty(defaultValue = {
"${call.request.local.scheme}://${call.request.host()}"
})
val ingress =
config.ingress.ifEmpty(defaultValue = {
"${call.request.local.scheme}://${call.request.host()}"
})

// redirect to login endpoint (wonderwall) and indicate that the user should be redirected back
// to the original request path after authentication
Expand Down

0 comments on commit 09e0051

Please sign in to comment.