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

feat: add api key credential as client library authorization type #1483

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ldetmer
Copy link

@ldetmer ldetmer commented Aug 28, 2024

Add new ApiKeyCredentials that will pass the appropriate api-key header for authorizing api requests from client libraries

Note: ApiKeyCredentials extends from the base Credential class as Api Keys are not considered oauth2 credentials since there is no access token which is required by GoogleCredentials/OAuth2Credentials. This also follows definition of oauth2 credentials as specified by Google Identity Protocols that require an access token when using Oauth 2.0 to access google APIs.

Copy link

conventional-commit-lint-gcf bot commented Aug 28, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Sep 16, 2024
@ldetmer ldetmer changed the title [POC] add api key credential feat: add api key credential as authorization type Sep 16, 2024
@ldetmer ldetmer marked this pull request as ready for review September 16, 2024 20:38
@ldetmer ldetmer requested review from a team as code owners September 16, 2024 20:38
@ldetmer ldetmer changed the title feat: add api key credential as authorization type feat: add api key credential as client library authorization type Sep 16, 2024
@@ -0,0 +1,83 @@
/*
* Copyright 2015, Google Inc. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think:

Suggested change
* Copyright 2015, Google Inc. All rights reserved.
* Copyright 2024, Google Inc. All rights reserved.

Copy link
Author

Choose a reason for hiding this comment

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

thanks good catch

Comment on lines 56 to 58
public static ApiKeyCredentials create(String apiKey) {
return new ApiKeyCredentials(apiKey);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to check if the user passes in null or empty string ("")?

Copy link
Author

Choose a reason for hiding this comment

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

sounds like a good idea, added. used Precondition as that's what I see in other apis, however it did force a new import, so let me know if you would prefer for me to check without using Precondition

@lqiu96
Copy link
Contributor

lqiu96 commented Sep 17, 2024

I think the changes LGTM. I'll need to double check the authenticationType being "" and the refresh() behavior.

@ldetmer
Copy link
Author

ldetmer commented Sep 17, 2024

I think the changes LGTM. I'll need to double check the authenticationType being "" and the refresh() behavior.

FYI, Blake and I discussed refresh with Carl and he agreed no op was correct.

*/
package com.google.auth;

import com.google.api.client.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace all wildcard imports with explicit imports.

go/java-style#s3.3.1-wildcard-imports

credentials/pom.xml Outdated Show resolved Hide resolved
@lqiu96
Copy link
Contributor

lqiu96 commented Sep 17, 2024

I think the changes LGTM. I'll need to double check the authenticationType being "" and the refresh() behavior.

FYI, Blake and I discussed refresh with Carl and he agreed no op was correct.

Yep. I think it makes sense just wanted to confirm the behavior and I think it works as intended. I'll ask about the authenticationType with the Auth Team and once I get their input on this, I think we should be good to go.

Comment on lines +39 to +40
* Credentials class for calling Google APIs using an API key.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a small snippet in the javadocs (or perhaps anywhere else) explaining why this extends directly from Credentials and not GoogleCredentials/ Oauth2Credentials?

I believe the original reason is that Api Keys do not have a concept of Access Tokens, but not sure if there were additional reasons behind this.

Copy link
Author

Choose a reason for hiding this comment

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

I added it in the PR description as its seems a design decision and wouldn't be part of java docs, but happy to add there if think it would be helpful

credentials/java/com/google/auth/ApiKeyCredentials.java Outdated Show resolved Hide resolved
* Credentials credentials = ApiKeyCredentials.create("your api key");
* </code></pre>
*/
public class ApiKeyCredentials extends Credentials {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose this class is in google-auth-library-credentials module unlike other credential classes we have in google-auth-library-oauth2-http module because it is not an OAuth2 credential?
Do you mind adding some context or links to the description section so that I can understand a bit more on this and for future tracking purposes? I find context and links to docs very helpful when tracking back changes in the past.

Copy link
Author

Choose a reason for hiding this comment

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

added note in description, let me know if you think it need any additional information

Copy link

sonarcloud bot commented Sep 18, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants