Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created January 25, 2019 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylertreat/9beef9d923de5379491f5257893e2247 to your computer and use it in GitHub Desktop.
Save tylertreat/9beef9d923de5379491f5257893e2247 to your computer and use it in GitHub Desktop.
Method to exchange JWT for Google-signed OIDC token
private DecodedJWT getGoogleIdToken() throws IOException {
String jwt = getSignedJwt();
final GenericData tokenRequest = new GenericData()
.set("grant_type", JWT_BEARER_TOKEN_GRANT_TYPE)
.set("assertion", jwt);
final UrlEncodedContent content = new UrlEncodedContent(tokenRequest);
final HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
final HttpRequest request = requestFactory
.buildPostRequest(new GenericUrl(OAUTH_TOKEN_URI), content)
.setParser(new JsonObjectParser(JacksonFactory.getDefaultInstance()));
HttpResponse response = request.execute();
GenericData responseData = response.parseAs(GenericData.class);
String idToken = (String) responseData.get("id_token");
return JWT.decode(idToken);
}
@SrushtiGangireddy
Copy link

SrushtiGangireddy commented Sep 4, 2020

Is JWT_BEARER_TOKEN_GRANT_TYPE enum ?
Do you know what the value in here can be ? JWT_BEARER_TOKEN_GRANT_TYPE

@SrushtiGangireddy
Copy link

That's helpful. Thanks a ton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment