generated from navikt/dp-service-template
-
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.
Co-authored-by: Giao The Cung <giaothe@gmail.com>
- Loading branch information
1 parent
b27bb67
commit ff966b6
Showing
8 changed files
with
109 additions
and
6 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
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/no/nav/dagpenger/oppslag/journalpost/id/api/Auth.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,14 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id.api | ||
|
||
import io.ktor.server.auth.AuthenticationConfig | ||
import io.ktor.server.auth.jwt.JWTPrincipal | ||
import io.ktor.server.auth.jwt.jwt | ||
|
||
fun AuthenticationConfig.jwt(name: String) { | ||
jwt(name) { | ||
verifier(AzureAd) | ||
validate { jwtClaims -> | ||
JWTPrincipal(jwtClaims.payload) | ||
} | ||
} | ||
} |
12 changes: 8 additions & 4 deletions
12
...ppslag/journalpost/id/JournalpostIdApi.kt → ...ag/journalpost/id/api/JournalpostIdApi.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
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/no/nav/dagpenger/oppslag/journalpost/id/api/NaisJWTProviders.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,32 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id.api | ||
|
||
import com.auth0.jwk.JwkProviderBuilder | ||
import io.ktor.server.auth.jwt.JWTAuthenticationProvider | ||
import java.net.URL | ||
|
||
fun JWTAuthenticationProvider.Config.verifier(type: NaisJWTProviders) { | ||
type.createVerifier(this) | ||
} | ||
|
||
abstract class NaisJWTProviders private constructor( | ||
private val jwksUri: URL, | ||
private val issuer: String, | ||
private val clientId: String, | ||
) { | ||
constructor(jwksUri: String, issuer: String, clientId: String) : this( | ||
jwksUri = URL(getEnvOrSystem(jwksUri)), | ||
issuer = getEnvOrSystem(issuer), | ||
clientId = getEnvOrSystem(clientId), | ||
) | ||
|
||
fun createVerifier(config: JWTAuthenticationProvider.Config) { | ||
config.verifier( | ||
JwkProviderBuilder(jwksUri).build(), | ||
issuer, | ||
) { this.withAudience(clientId) } | ||
} | ||
} | ||
|
||
private fun getEnvOrSystem(name: String) = System.getenv(name) ?: System.getProperty(name) | ||
|
||
object AzureAd : NaisJWTProviders("AZURE_OPENID_CONFIG_JWKS_URI", "AZURE_OPENID_CONFIG_ISSUER", "AZURE_APP_CLIENT_ID") |
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
28 changes: 28 additions & 0 deletions
28
src/test/kotlin/no/nav/dagpenger/oppslag/journalpost/id/MockAzure.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,28 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id | ||
|
||
import no.nav.security.mock.oauth2.MockOAuth2Server | ||
|
||
class MockAzure { | ||
companion object { | ||
private const val AZURE_APP_CLIENT_ID = "test_client_id" | ||
private const val AZURE_OPENID_CONFIG_ISSUER = "test_issuer" | ||
private val mockOAuth2Server: MockOAuth2Server by lazy { | ||
MockOAuth2Server().also { server -> | ||
server.start() | ||
} | ||
} | ||
} | ||
|
||
init { | ||
System.setProperty("AZURE_APP_CLIENT_ID", AZURE_APP_CLIENT_ID) | ||
System.setProperty("AZURE_OPENID_CONFIG_ISSUER", "${mockOAuth2Server.issuerUrl(AZURE_OPENID_CONFIG_ISSUER)}") | ||
System.setProperty("AZURE_OPENID_CONFIG_JWKS_URI", "${mockOAuth2Server.jwksUrl(AZURE_OPENID_CONFIG_ISSUER)}") | ||
} | ||
|
||
fun lagToken(): String { | ||
return mockOAuth2Server.issueToken( | ||
audience = AZURE_APP_CLIENT_ID, | ||
issuerId = AZURE_OPENID_CONFIG_ISSUER, | ||
).serialize() | ||
} | ||
} |