Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client for fetching public keys #94

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions adapter-sdk-kotlin-ktor2-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import com.adarshr.gradle.testlogger.TestLoggerExtension
import com.adarshr.gradle.testlogger.TestLoggerPlugin

plugins {
`java-library`
`maven-publish`
idea

id("org.jetbrains.kotlin.jvm")
id("com.adarshr.test-logger")
id("java")
}

description = "Ktor client for fetching public keys"
val ktorVersion = "2.0.1"


dependencies {
implementation("org.slf4j:slf4j-api:1.7.36")
api("com.nimbusds:nimbus-jose-jwt:9.22")

implementation("io.ktor:ktor-client-json:$ktorVersion")
implementation("io.ktor:ktor-client-core-jvm:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")

testImplementation("org.slf4j:slf4j-simple:1.7.36")
testImplementation("org.assertj:assertj-core:3.22.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
}

plugins.withType<TestLoggerPlugin> {
configure<TestLoggerExtension> {
theme = com.adarshr.gradle.testlogger.theme.ThemeType.MOCHA_PARALLEL
slowThreshold = 5000
showStackTraces = true
showCauses = true
}
}

tasks.test {
useJUnitPlatform()
reports {
html.required.set(false)
junitXml.required.set(true)
}
}

publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.oslokommune.oslonokkelen.adapter.tokens.client

import com.nimbusds.jose.jwk.JWKSet
import com.nimbusds.jose.jwk.source.DefaultJWKSetCache
import com.nimbusds.jose.jwk.source.JWKSetCache
import com.nimbusds.jose.jwk.source.JWKSource
import com.nimbusds.jose.jwk.source.RemoteJWKSet
import com.nimbusds.jose.proc.SecurityContext
import com.nimbusds.jose.util.Resource
import com.nimbusds.jose.util.ResourceRetriever
import io.ktor.client.HttpClient
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.http.isSuccess
import kotlinx.coroutines.runBlocking
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.URI
import java.net.URL
import java.util.concurrent.TimeUnit

class OslonokkelenJWKSourceFactory(
private val client: HttpClient,
private val backendRootUri: URI,
private val keyCache: JWKSetCache = DefaultJWKSetCache(24, -1, TimeUnit.HOURS)
) : ResourceRetriever {

private val log: Logger = LoggerFactory.getLogger(OslonokkelenJWKSourceFactory::class.java)

fun createSource(): JWKSource<SecurityContext> {
val jwkUrl = URL("$backendRootUri/adapter/v2/keys")
return RemoteJWKSet(jwkUrl, this, keyCache)
}

override fun retrieveResource(targetUrl: URL): Resource {
return runBlocking {
log.info("Downloading all the keys from {}", targetUrl)
val response = client.get(targetUrl) {
accept(ContentType.parse(JWKSet.MIME_TYPE))
}

if (response.status.isSuccess()) {
val rawJson = response.bodyAsText()
val contentType = response.contentType().toString()

Resource(rawJson, contentType)
} else {
throw IOException("Failed to download keys: ${response.status}")
}
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ rootProject.name = "oslonokkelen-adapter"
include(":adapter-docs")
include(":adapter-protobuf-java")
include(":adapter-sdk-kotlin")
include(":adapter-sdk-kotlin-ktor2-client")