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

Check self-signed ID token when access token is verified #43585

Merged

Conversation

sberyozkin
Copy link
Member

@sberyozkin sberyozkin commented Sep 29, 2024

Fixes #43583.

This PR is about

  1. Avoiding an internal ID token verification when JsonWebToken injected by mistake but not accessed from the code, even if the OAuth2 providers return binary/opaque access tokens

  2. Users getting a correct error message when they attempt to access the access token using JsonWebToken API but the OAuth2 providers return opaque/binary access tokens

For example, in case of Github, when they have

class GithubService {
@Inject JsonWebToken jwt;
}

then any method which attempts to access the Githib binary access token as jwt, will get, instead of the confusing error such as:

2024-09-27 16:21:07,764 ERROR [io.qua.oid.run.CodeAuthenticationMechanism] (vert.x-eventloop-thread-2) ID token verification has failed:
Token issued to client 686026e8cf211de572f8 can not be introspected because the introspection endpoint address is unknown - please check if your OpenId Connect Provider supports the token introspection

a more appropriate, here in devmode:

Exception in GithubService.java:26
	  24      @Path("/login")
	  25      public String userinfo() {
	→ 26  	    return jwt.getName() + userInfo.getUserInfoString();
	  27  	}
	  28      

The stacktrace below has been reversed to show the root cause first. [See the original stacktrace](http://localhost:8080/login)

io.quarkus.oidc.OIDCException: Opaque access token can not be converted to JsonWebToken
	at io.quarkus.oidc.runtime.OidcJsonWebTokenProducer.getTokenCredential(OidcJsonWebTokenProducer.java:67)

It will make it possible nearly immediately identify the problem which is about the user attempting to use a wrong API to access the binary token. Unfortunately we can't detect it at build time, for ex, we know, Github returns binary access tokens, but this is not part of any contract, the format can change any time.

So PR just makes sure that a check for a self-signed ID token is done correctly in both of the code branches where an ID tokne verification is attempted, with and without the initial code flow access token verification.

The added test confirms that, with a provider returning an opaque access token (value = alice - to match the OidcWiremock stub rule), the endpoint method works as expected if an inject JsonWebToken is not accessed, but fails with the server error otherwise.

It all may be a bit confusing, but all in all, it is really about improving the user experience when they make a mistake with trying to access opaque access tokens as JSON web tokens. The fix is very simple - encapsulate the code which checks how to proceed with the ID token verification into a utility method and call it from both places where it is necessary

Copy link
Contributor

@gastaldi gastaldi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, added one minor nitpick. Feel free to ignore if you are certain that won't happen

@sberyozkin sberyozkin merged commit ffe6bae into quarkusio:main Sep 30, 2024
23 checks passed
@sberyozkin sberyozkin deleted the oidc_self_signed_idtoken_check branch September 30, 2024 11:45
@quarkus-bot quarkus-bot bot added this to the 3.16 - main milestone Sep 30, 2024
@gsmet gsmet modified the milestones: 3.16.0.CR1, 3.15.2 Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated ID token can be attempted to be verified as if it was issued by the OIDC/OAuth2 provider
3 participants