|
| 1 | +package no.nav.security.mock.oauth2.e2e |
| 2 | + |
| 3 | +import com.nimbusds.jose.jwk.JWKSet |
| 4 | +import com.nimbusds.jwt.SignedJWT |
| 5 | +import com.nimbusds.oauth2.sdk.id.Issuer |
| 6 | +import io.kotest.assertions.asClue |
| 7 | +import io.kotest.matchers.collections.shouldContainExactly |
| 8 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 9 | +import io.kotest.matchers.shouldBe |
| 10 | +import io.kotest.matchers.string.shouldContain |
| 11 | +import io.kotest.matchers.string.shouldStartWith |
| 12 | +import no.nav.security.mock.oauth2.MockOAuth2Server |
| 13 | +import no.nav.security.mock.oauth2.extensions.verifySignatureAndIssuer |
| 14 | +import no.nav.security.mock.oauth2.http.OAuth2HttpResponse |
| 15 | +import no.nav.security.mock.oauth2.http.WellKnown |
| 16 | +import no.nav.security.mock.oauth2.http.route |
| 17 | +import no.nav.security.mock.oauth2.testutils.audience |
| 18 | +import no.nav.security.mock.oauth2.testutils.claims |
| 19 | +import no.nav.security.mock.oauth2.testutils.client |
| 20 | +import no.nav.security.mock.oauth2.testutils.get |
| 21 | +import no.nav.security.mock.oauth2.testutils.issuer |
| 22 | +import no.nav.security.mock.oauth2.testutils.parse |
| 23 | +import no.nav.security.mock.oauth2.testutils.post |
| 24 | +import no.nav.security.mock.oauth2.testutils.subject |
| 25 | +import no.nav.security.mock.oauth2.testutils.toTokenResponse |
| 26 | +import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback |
| 27 | +import no.nav.security.mock.oauth2.withMockOAuth2Server |
| 28 | +import okhttp3.HttpUrl.Companion.toHttpUrl |
| 29 | +import org.junit.jupiter.api.Test |
| 30 | +import java.time.Duration |
| 31 | + |
| 32 | +class MockOAuth2ServerIntegrationTest { |
| 33 | + private val client = client() |
| 34 | + |
| 35 | + @Test |
| 36 | + fun `server with addtional routes should serve wellknown and additional route`() { |
| 37 | + val s = MockOAuth2Server( |
| 38 | + route("/custom") { |
| 39 | + OAuth2HttpResponse(status = 200, body = "custom route") |
| 40 | + } |
| 41 | + ).apply { |
| 42 | + start() |
| 43 | + } |
| 44 | + client.get(s.wellKnownUrl("someissuer")).body?.string() shouldContain s.issuerUrl("someissuer").toString() |
| 45 | + client.get(s.url("/custom")).body?.string() shouldBe "custom route" |
| 46 | + client.get(s.url("/someissuer/custom")).body?.string() shouldBe "custom route" |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `server on fixed port should include fixed port in wellknown and issued token`() { |
| 51 | + val port = 1234 |
| 52 | + val server = MockOAuth2Server() |
| 53 | + server.start(port = port) |
| 54 | + |
| 55 | + val issuerUrl = "http://localhost:$port/issuer1" |
| 56 | + client.get("$issuerUrl/.well-known/openid-configuration".toHttpUrl()).asClue { |
| 57 | + it.parse<WellKnown>().issuer shouldBe issuerUrl |
| 58 | + server.issueToken("issuer1").issuer shouldBe issuerUrl |
| 59 | + } |
| 60 | + |
| 61 | + server.shutdown() |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `wellknown should include issuer id in urls`() { |
| 66 | + withMockOAuth2Server { |
| 67 | + val baseUrl = this.baseUrl().toString().removeSuffix("/") |
| 68 | + client.get(this.wellKnownUrl("default")).let { |
| 69 | + it.parse<WellKnown>() urlsShouldStartWith "$baseUrl/default" |
| 70 | + } |
| 71 | + client.get(this.wellKnownUrl("foo")).let { |
| 72 | + it.parse<WellKnown>() urlsShouldStartWith "$baseUrl/foo" |
| 73 | + } |
| 74 | + client.get(this.wellKnownUrl("path1/path2/path3")).let { |
| 75 | + it.parse<WellKnown>() urlsShouldStartWith "$baseUrl/path1/path2/path3" |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `token request with enqueued token callback should return claims from tokencallback (with exception of id_token and oidc rules)`() { |
| 82 | + val server = MockOAuth2Server().apply { start() } |
| 83 | + server.enqueueCallback( |
| 84 | + DefaultOAuth2TokenCallback( |
| 85 | + issuerId = "custom", |
| 86 | + subject = "yolo", |
| 87 | + audience = listOf("myaud") |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + client.post( |
| 92 | + server.tokenEndpointUrl("custom"), |
| 93 | + mapOf( |
| 94 | + "client_id" to "client1", |
| 95 | + "client_secret" to "secret", |
| 96 | + "grant_type" to "authorization_code", |
| 97 | + "scope" to "openid scope1", |
| 98 | + "redirect_uri" to "http://mycallback", |
| 99 | + "code" to "1234" |
| 100 | + ) |
| 101 | + ).toTokenResponse().asClue { |
| 102 | + it.idToken.shouldNotBeNull() |
| 103 | + it.idToken.subject shouldBe "yolo" |
| 104 | + it.idToken.audience shouldBe listOf("client1") |
| 105 | + it.accessToken.shouldNotBeNull() |
| 106 | + it.accessToken.subject shouldBe "yolo" |
| 107 | + it.accessToken.audience shouldBe listOf("myaud") |
| 108 | + } |
| 109 | + server.shutdown() |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `issue token directly from server should contain claims from callback and be verifiable with server jwks`() { |
| 114 | + withMockOAuth2Server { |
| 115 | + val signedJWT: SignedJWT = this.issueToken( |
| 116 | + "default", |
| 117 | + "client1", |
| 118 | + DefaultOAuth2TokenCallback( |
| 119 | + issuerId = "default", |
| 120 | + subject = "mysub", |
| 121 | + audience = listOf("myaud"), |
| 122 | + claims = mapOf("someclaim" to "claimvalue") |
| 123 | + ) |
| 124 | + ) |
| 125 | + val wellKnown = client.get(this.wellKnownUrl("default")).parse<WellKnown>() |
| 126 | + val jwks = client.get(wellKnown.jwksUri.toHttpUrl()).body?.let { JWKSet.parse(it.string()) } |
| 127 | + |
| 128 | + jwks.shouldNotBeNull() |
| 129 | + |
| 130 | + signedJWT.verifySignatureAndIssuer(Issuer(wellKnown.issuer), jwks).asClue { |
| 131 | + it.issuer shouldBe wellKnown.issuer |
| 132 | + it.subject shouldBe "mysub" |
| 133 | + it.audience shouldContainExactly listOf("myaud") |
| 134 | + it.claims["someclaim"] shouldBe "claimvalue" |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun `anyToken should issue token with claims from input and be verifyable by servers keys`() { |
| 141 | + withMockOAuth2Server { |
| 142 | + val customIssuer = "https://customissuer".toHttpUrl() |
| 143 | + val token = this.anyToken( |
| 144 | + customIssuer, |
| 145 | + mutableMapOf( |
| 146 | + "sub" to "mysub", |
| 147 | + "aud" to listOf("myapp"), |
| 148 | + "customInt" to 123, |
| 149 | + "customList" to listOf(1, 2, 3) |
| 150 | + ), |
| 151 | + Duration.ofSeconds(10) |
| 152 | + ) |
| 153 | + |
| 154 | + val wellKnown = client.get(this.wellKnownUrl("default")).parse<WellKnown>() |
| 155 | + val jwks = client.get(wellKnown.jwksUri.toHttpUrl()).body?.let { JWKSet.parse(it.string()) } |
| 156 | + |
| 157 | + jwks.shouldNotBeNull() |
| 158 | + |
| 159 | + token.verifySignatureAndIssuer(Issuer(customIssuer.toString()), jwks).asClue { |
| 160 | + it.issuer shouldBe customIssuer.toString() |
| 161 | + it.subject shouldBe "mysub" |
| 162 | + it.audience shouldContainExactly listOf("myapp") |
| 163 | + it.claims["customInt"] shouldBe 123 |
| 164 | + it.claims["customList"] to listOf(1, 2, 3) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private infix fun WellKnown.urlsShouldStartWith(url: String) { |
| 170 | + issuer shouldStartWith url |
| 171 | + authorizationEndpoint shouldStartWith url |
| 172 | + tokenEndpoint shouldStartWith url |
| 173 | + jwksUri shouldStartWith url |
| 174 | + endSessionEndpoint shouldStartWith url |
| 175 | + } |
| 176 | +} |
0 commit comments