-
Notifications
You must be signed in to change notification settings - Fork 532
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
Conversation
There was a problem hiding this 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
?
There was a problem hiding this 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.
542dcbe
to
b7cfb24
Compare
Jon this is now ready for review. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// there's character that need scaping. Newtonsoft seems to handle them | |
// there's a character that need escaping. Json.NET seems to handle them |
There was a problem hiding this comment.
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); | ||
} | ||
|
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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) => |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
b7cfb24
to
fa0c63c
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) => |
There was a problem hiding this comment.
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); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests added.
There was a problem hiding this 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)); |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
fa0c63c
to
04845ae
Compare
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.
Closes #2036