-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7159ea4
commit abbc992
Showing
16 changed files
with
163 additions
and
331 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
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 |
---|---|---|
@@ -1,45 +1,48 @@ | ||
package no.nav.spanner | ||
|
||
import com.auth0.jwk.JwkProvider | ||
import com.auth0.jwk.JwkProviderBuilder | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.natpryce.konfig.Configuration | ||
import com.natpryce.konfig.Key | ||
import com.natpryce.konfig.stringType | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.net.HttpURLConnection | ||
import java.net.URI | ||
import java.net.URL | ||
|
||
class AzureADConfig( | ||
val discoveryUrl: String, | ||
private val jwkProvider: JwkProvider, | ||
private val issuer: String, | ||
val tokenEndpoint: String, | ||
val clientId: String, | ||
val clientSecret: String, | ||
authorizationUrl: String? = null | ||
val clientSecret: String | ||
) { | ||
private val discovered = discoveryUrl.discover() | ||
val tokenEndpoint = discovered["token_endpoint"]?.textValue() | ||
?: throw RuntimeException("Unable to discover token endpoint") | ||
val authorizationEndpoint = authorizationUrl ?: discovered["authorization_endpoint"]?.textValue() | ||
?: throw RuntimeException("Unable to discover authorization endpoint") | ||
fun konfigurerJwtAuth(config: AuthenticationConfig) { | ||
config.jwt { | ||
verifier(jwkProvider, issuer) { | ||
withAudience(clientId) | ||
withClaimPresence("NAVident") | ||
withClaimPresence("preferred_username") | ||
withClaimPresence("name") | ||
} | ||
validate { credentials -> | ||
JWTPrincipal(credentials.payload) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
fun fromEnv(config: Configuration) = AzureADConfig( | ||
discoveryUrl = config[Key("AZURE_APP_WELL_KNOWN_URL", stringType)], | ||
jwkProvider = JwkProviderBuilder(URI(config[Key("AZURE_APP_WELL_KNOWN_URL", stringType)]).toURL()).build(), | ||
issuer = config[Key("AZURE_OPENID_CONFIG_ISSUER", stringType)], | ||
tokenEndpoint = config[Key("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT", stringType)], | ||
clientId = config[Key("AZURE_APP_CLIENT_ID", stringType)], | ||
clientSecret = config[Key("AZURE_APP_CLIENT_SECRET", stringType)], | ||
authorizationUrl = config.getOrNull(Key("AUTHORIZATION_URL", stringType)), | ||
) | ||
} | ||
} | ||
|
||
private fun String.discover(): JsonNode { | ||
val (responseCode, responseBody) = this.fetchUrl() | ||
if (responseCode >= 300 || responseBody == null) throw IOException("got status $responseCode from ${this}.") | ||
return jacksonObjectMapper().readTree(responseBody) | ||
} | ||
|
||
private fun String.fetchUrl() = with(URL(this).openConnection() as HttpURLConnection) { | ||
requestMethod = "GET" | ||
val stream: InputStream? = if (responseCode < 300) this.inputStream else this.errorStream | ||
responseCode to stream?.bufferedReader()?.readText() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
package no.nav.spanner | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.http.content.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
internal fun Route.frontendRouting() { | ||
get("/") { | ||
call.respondText( | ||
this::class.java.classLoader.getResource("static/index.html")!!.readText(), | ||
ContentType.Text.Html | ||
) | ||
} | ||
get("/person/*") { | ||
call.respondText( | ||
this::class.java.classLoader.getResource("static/index.html")!!.readText(), | ||
ContentType.Text.Html | ||
) | ||
} | ||
static("/") { | ||
resources("static/") | ||
} | ||
staticResources("/", "/static/") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.