-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: Verify audience claim matches client ID for OpenID provider
The client ID is generated by the OpenID provider on registration, so the OpenID provider configuration needs to include the client ID.
- Loading branch information
Showing
3 changed files
with
40 additions
and
54 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 |
---|---|---|
|
@@ -38,11 +38,11 @@ class OpenIdConnectAuthenticationTest { | |
fun `verifyAndDecodeBearerToken returns VerifiedToken`() { | ||
val issuer = "example.com" | ||
val subject = "[email protected]" | ||
val audience = "foobar" | ||
val clientId = "foobar" | ||
val scopes = setOf("foo.bar", "foo.baz") | ||
val openIdProvider = OpenIdProvider(issuer) | ||
val credentials = openIdProvider.generateCredentials(audience, subject, scopes) | ||
val auth = OpenIdConnectAuthentication(audience, listOf(openIdProvider.providerConfig)) | ||
val openIdProvider = OpenIdProvider(issuer, clientId) | ||
val credentials = openIdProvider.generateCredentials(subject, scopes) | ||
val auth = OpenIdConnectAuthentication(listOf(openIdProvider.providerConfig)) | ||
|
||
val token = auth.verifyAndDecodeBearerToken(extractHeaders(credentials)) | ||
|
||
|
@@ -53,17 +53,16 @@ class OpenIdConnectAuthenticationTest { | |
fun `verifyAndDecodeBearerToken throws UNAUTHENTICATED when token is expired`() { | ||
val issuer = "example.com" | ||
val subject = "[email protected]" | ||
val audience = "foobar" | ||
val clientId = "foobar" | ||
val scopes = setOf("foo.bar", "foo.baz") | ||
val openIdProvider = OpenIdProvider(issuer) | ||
val openIdProvider = OpenIdProvider(issuer, clientId) | ||
val credentials = | ||
openIdProvider.generateCredentials( | ||
audience, | ||
subject, | ||
scopes, | ||
Instant.now().minus(Duration.ofMinutes(5)), | ||
) | ||
val auth = OpenIdConnectAuthentication(audience, listOf(openIdProvider.providerConfig)) | ||
val auth = OpenIdConnectAuthentication(listOf(openIdProvider.providerConfig)) | ||
|
||
val exception = | ||
assertFailsWith<StatusException> { | ||
|
@@ -74,34 +73,15 @@ class OpenIdConnectAuthenticationTest { | |
assertThat(exception).hasMessageThat().ignoringCase().contains("expired") | ||
} | ||
|
||
@Test | ||
fun `verifyAndDecodeBearerToken throws UNAUTHENTICATED when audience does not match`() { | ||
val issuer = "example.com" | ||
val subject = "[email protected]" | ||
val audience = "foobar" | ||
val scopes = setOf("foo.bar", "foo.baz") | ||
val openIdProvider = OpenIdProvider(issuer) | ||
val credentials = openIdProvider.generateCredentials("bad-audience", subject, scopes) | ||
val auth = OpenIdConnectAuthentication(audience, listOf(openIdProvider.providerConfig)) | ||
|
||
val exception = | ||
assertFailsWith<StatusException> { | ||
auth.verifyAndDecodeBearerToken(extractHeaders(credentials)) | ||
} | ||
|
||
assertThat(exception.status.code).isEqualTo(Status.Code.UNAUTHENTICATED) | ||
assertThat(exception).hasMessageThat().ignoringCase().contains("audience") | ||
} | ||
|
||
@Test | ||
fun `verifyAndDecodeBearerToken throws UNAUTHENTICATED when provider not found for issuer`() { | ||
val issuer = "example.com" | ||
val subject = "[email protected]" | ||
val audience = "foobar" | ||
val clientId = "foobar" | ||
val scopes = setOf("foo.bar", "foo.baz") | ||
val openIdProvider = OpenIdProvider(issuer) | ||
val credentials = openIdProvider.generateCredentials(audience, subject, scopes) | ||
val auth = OpenIdConnectAuthentication(audience, emptyList()) | ||
val openIdProvider = OpenIdProvider(issuer, clientId) | ||
val credentials = openIdProvider.generateCredentials(subject, scopes) | ||
val auth = OpenIdConnectAuthentication(emptyList()) | ||
|
||
val exception = | ||
assertFailsWith<StatusException> { | ||
|
@@ -114,9 +94,8 @@ class OpenIdConnectAuthenticationTest { | |
|
||
@Test | ||
fun `verifyAndDecodeBearerToken throws UNAUTHENTICATED when token is not a valid JWT`() { | ||
val audience = "foobar" | ||
val credentials = BearerTokenCallCredentials("foo", false) | ||
val auth = OpenIdConnectAuthentication(audience, emptyList()) | ||
val auth = OpenIdConnectAuthentication(emptyList()) | ||
|
||
val exception = | ||
assertFailsWith<StatusException> { | ||
|
@@ -129,8 +108,7 @@ class OpenIdConnectAuthenticationTest { | |
|
||
@Test | ||
fun `verifyAndDecodeBearerToken throws UNAUTHENTICATED when header not found`() { | ||
val audience = "foobar" | ||
val auth = OpenIdConnectAuthentication(audience, emptyList()) | ||
val auth = OpenIdConnectAuthentication(emptyList()) | ||
|
||
val exception = assertFailsWith<StatusException> { auth.verifyAndDecodeBearerToken(Metadata()) } | ||
|
||
|