-
Notifications
You must be signed in to change notification settings - Fork 229
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
Consider implementing OpenID Connect ID Token-based credentials for service accounts #99
Comments
@broady @JustinBeckwith @ptone I can implement it in ImpersonatedCredentials.java pretty easily (its just an API call to iamcredentials.generateIdToken() however, i'd like confirmation the interface is ok below. Note, the
public interface IdTokenProvider {
class IdTokenProviderException extends RuntimeException {
String getIdToken(String target_audience);
String getIdToken(String target_audience, boolean include_email);
/* TODO: java 1.8+ supports statics in interface defintions.
* boolean verifyIdToken(String idToken, String audience);
*/
}
public String getIdToken(String target_audience) {
// issue idToken with given audience
return "theIdToken"
}
public String getIdToken(String target_audience, boolean include_email) {
// issue idToken with given audience and include the email claim
return "theIdToken"
}
public static boolean verifyIdToken(String idToken, String audience) {
// verify id token for signature, exp and audience
return true;
} |
I think @chingor13 is actively working on something in this space. |
ok, let me know if there's any part i can assit with, the impersonatedCredential's implementation of idtokens is here if you need https://gist.github.com/salrashid123/2cd2fb924fa9e4435273abae86b35597
|
To clarify, I have been actively working on the JWTCredentials implementation, but not the IdToken implementation. For the interface, this library does not generally like to return the primitive tokens as the primary return types. Instead, I'd prefer to return a So perhaps the interface should be something like: public interface IdTokenProvider {
IdTokenCredentials idTokenWithAudience(String audience);
} A few other things to consider:
Note that we do have an IdToken implementation in google-oauth-client that does include verification. |
re: re: verify step being done outside library note, i wrote up the flows here but so far just used a 3rd party library for java for serviceAccounts. Once impersonatedCredentials in any language is available, i'll add it in there Are you ok w/ a PR for starters that implements idtokens at the moment? |
Yep, let's go ahead and start on this. |
i've got the code ready but wanted confirmation
public static IdTokenCredentials create(GoogleCredentials sourceCredentials, String targetAudience,
List<String> options) {
IdToken idTokenWithAudience(String targetAudience, List<String> options); I wanted to confirm this is ok (i've got it working but before i file the PR and testcase... sample usage String credPath = "/svc.json";
String targetAudience = "https://myapp-6w42z6vi3q-uc.a.run.app";
// ADC (ServiceAccount)
// export GOOGLE_APPLICATION_CREDENTIALS=svc.json
GoogleCredentials adcCreds = GoogleCredentials.getApplicationDefault();
//IdTokenCredentials tokenCredential = IdTokenCredentials.create(adcCreds, targetAudience);
IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder()
.setSourceCredentials(adcCreds)
.setTargetAudience(targetAudience).build();
// ServiceAccountCredentials
ServiceAccountCredentials saCreds = ServiceAccountCredentials
.fromStream(new FileInputStream(credPath));
saCreds = (ServiceAccountCredentials) saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam"));
IdTokenCredentials tokenCredential = IdTokenCredentials.create(saCreds, targetAudience);
// ImpersonatedCredentials
ImpersonatedCredentials imCreds = ImpersonatedCredentials.create(saCreds,
"[email protected]", null,
Arrays.asList("https://www.googleapis.com/auth/userinfo.email"), 300);
IdTokenCredentials tokenCredential = IdTokenCredentials.create(imCreds,
targetAudience, Arrays.asList(ImpersonatedCredentials.INCLUDE_EMAIL));
// ComputeEngineCredentials
ComputeEngineCredentials caCreds = ComputeEngineCredentials.create();
IdTokenCredentials tokenCredential = IdTokenCredentials.create(caCreds,
targetAudience,
Arrays.asList(ComputeEngineCredentials.ID_TOKEN_FORMAT_FULL,
ComputeEngineCredentials.ID_TOKEN_LICENSES_TRUE));
// Invoke the API
GenericUrl genericUrl = new GenericUrl("https://myapp-6w42z6vi3q-uc.a.run.app");
HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential);
HttpTransport transport = new NetHttpTransport();
HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
String r = response.parseAsString();
System.out.println(r);
System.out.println(tokenCredential.getIdToken().getTokenValue());
System.out.println(tokenCredential.getIdToken().getExpirationTime());
System.out.println(tokenCredential.getIdToken().getAudience()); If this looks ok (vs something like deriving an idtokencredential directly from another credential |
This would satisfy the Identity-aware Proxy use case as well as a few other niche cases that have come up.
Context:
https://gist.github.com/jonparrott/cffca2fa7881e03fbe6ff7c25773c9cf
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/iap/make_iap_request.py#L121
See googleapis/google-auth-library-python#130 for additional context.
The text was updated successfully, but these errors were encountered: