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: Adds GoogleCredential.FromJsonParameters #2037

Merged
merged 1 commit into from
Feb 17, 2022

Conversation

amanda-tarafa
Copy link
Contributor

Closes #2036

@amanda-tarafa amanda-tarafa requested a review from jskeet January 31, 2022 14:51
@amanda-tarafa amanda-tarafa self-assigned this Jan 31, 2022
Copy link
Collaborator

@jskeet jskeet left a comment

Choose a reason for hiding this comment

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

With this in place, will it then be really simple to load auth from usersecrets.json?

Copy link
Contributor Author

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

For using with user secrets, you can do something like:

JsonCredentialParameters credentialParams = Configuration.GetSection("GoogleCredential").Get<JsonCredentialParameters>();
GoogleCredential credential = GoogleCredential.FromJsonParameters(credentialParams);

The method above that extracts the config will attempt to match the properties by name (or binder options may be provided). The drawback here is that the name of the properties in the credential JSON file (the one from Google) do not match the property names in JsonCredentialParameters. But storing the credential parameters in the configuration is supposed to be a one time thing, so this shouldn' be very bad.

@amanda-tarafa amanda-tarafa marked this pull request as ready for review February 8, 2022 22:38
@amanda-tarafa amanda-tarafa requested a review from a team as a code owner February 8, 2022 22:38
@amanda-tarafa
Copy link
Contributor Author

Jon this is now ready for review.

Copy link
Collaborator

@jskeet jskeet left a comment

Choose a reason for hiding this comment

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

Generally fine - just some nits and test suggestions


private IConfiguration BuildConfigurationForCredential(string json) =>
// We parse the string and call ToString after because within the key
// there's character that need scaping. Newtonsoft seems to handle them
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// there's character that need scaping. Newtonsoft seems to handle them
// there's a character that need escaping. Json.NET seems to handle them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Assert.Equal("CLIENT_EMAIL", serviceCred.Id);
Assert.Equal("PROJECT_ID", serviceCred.ProjectId);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Add tests for a) invalid JSON; b) valid JSON that doesn't contain everything we expect; c) valid JSON that doesn't conform with the types in our class? (I'd expect these to be pretty simple, but they'd document the expected behavior.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests added.

private static GoogleCredential CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters)
{
switch (credentialParameters.Type)
internal GoogleCredential CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters) =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a nullity check? I suspect we previously "knew" that it would never be null, but we don't any more due to this effectively being exposed publicly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

Jon, second commit contains all review changes. PTAL.


private IConfiguration BuildConfigurationForCredential(string json) =>
// We parse the string and call ToString after because within the key
// there's character that need scaping. Newtonsoft seems to handle them
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

private static GoogleCredential CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters)
{
switch (credentialParameters.Type)
internal GoogleCredential CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Assert.Equal("CLIENT_EMAIL", serviceCred.Id);
Assert.Equal("PROJECT_ID", serviceCred.ProjectId);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests added.

Copy link
Collaborator

@jskeet jskeet left a comment

Choose a reason for hiding this comment

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

Great, thanks!

}).Build();

JsonCredentialParameters credentialParameters = config.GetSection("GoogleCredential").Get<JsonCredentialParameters>();
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => GoogleCredential.FromJsonParameters(credentialParameters));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting - ArgumentNullException here is unexpected for me, but that's why it's good to have the test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it was unexpected for me as well. Will rebase and squash soon.

@amanda-tarafa amanda-tarafa merged commit 5ef4957 into googleapis:main Feb 17, 2022
@amanda-tarafa amanda-tarafa deleted the credentials-init branch February 17, 2022 17:16
amanda-tarafa added a commit to amanda-tarafa/google-api-dotnet-client that referenced this pull request Feb 18, 2022
Bug fixes:
- googleapis#1991. Disables self signed JWTs for domain-wide delegation.
- googleapis#2023. Observes execeptions in refresh token tasks.
Features:
- googleapis#2037. Adds GoogleCredential.FromJsonParameters.
- googleapis#2046. Improves error messages for JWT validation.
Docs:
- googleapis#2044. Adds links to OAuth2 docs in FAQs.
amanda-tarafa added a commit that referenced this pull request Feb 18, 2022
Bug fixes:
- #1991. Disables self signed JWTs for domain-wide delegation.
- #2023. Observes execeptions in refresh token tasks.
Features:
- #2037. Adds GoogleCredential.FromJsonParameters.
- #2046. Improves error messages for JWT validation.
Docs:
- #2044. Adds links to OAuth2 docs in FAQs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alternative authorisation using code or dotnet app settings.
2 participants