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

Prefer HTTP basic authentication in OAuth2 client #9127

Merged
merged 17 commits into from
May 11, 2020

Conversation

crush-157
Copy link
Contributor

As discussed in forum post Can't get OAuth2 access token with grant type client credentials, the preferred behaviour should be to use HTTP Basic authentication to pass client credentials.

This PR provides the option to specify HTTP Basic or passing the credentials in the request body when creating the client, with HTTP Basic as the default.

@jhass jhass changed the title Auth scheme option Prefer HTTP basic authentication in OAuth2 client Apr 18, 2020
src/oauth2/auth_scheme.cr Outdated Show resolved Hide resolved
src/oauth2/client.cr Outdated Show resolved Hide resolved
src/oauth2/client.cr Outdated Show resolved Hide resolved
@crush-157 crush-157 requested review from jhass and asterite April 20, 2020 09:53
spec/std/oauth2/client_spec.cr Outdated Show resolved Hide resolved
src/oauth2/auth_scheme.cr Outdated Show resolved Hide resolved
src/oauth2/auth_scheme.cr Outdated Show resolved Hide resolved
src/oauth2/client.cr Outdated Show resolved Hide resolved
src/oauth2/client.cr Outdated Show resolved Hide resolved
src/oauth2/client.cr Outdated Show resolved Hide resolved
Comment on lines 57 to 59
private getter host, client_id, client_secret, port, scheme, authorize_uri,
redirect_uri, auth_scheme, token_uri

Copy link
Contributor

Choose a reason for hiding this comment

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

What's the point of this private getter instead of using instance variables?
Smells like YAGNI :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I always prefer to use getters/setters instead of accessing instance variables directly.

Copy link
Contributor

@Sija Sija May 5, 2020

Choose a reason for hiding this comment

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

Well, refactoring like this might be considered out of scope of this PR. Secondly it's more costly, so if there's no good reason I'd advise against that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My reason for preferring getters/setters over instance variables even privately is that if the feature needs to change from being a variable to a method then you restrict your changes to the getter/setter.

Context: I once "won" an argument over this on a project where my view was that we should access private instance variables directly, and then lived to regret it. Since then, I've always had a horror of accessing instance variables directly (outside of constructors/getters/setters).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OTOH if you have a horror of private accessors I can change them back. Your call.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest doing so, yes :)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@jhass I'd argue that in this case it's simply out of scope of this PR, but thanks for sharing this anyway ❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jhass Good to know

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Sija private accessors dropped for using instance variables directly. I won't tell Bertrand Meyer if you don't.

src/oauth2/client.cr Outdated Show resolved Hide resolved
@crush-157 crush-157 requested a review from jhass May 7, 2020 08:24
@jhass jhass requested a review from a team May 7, 2020 08:34
@jhass jhass removed the request for review from asterite May 7, 2020 08:34
token = client.get_access_token_using_refresh_token(scope: "read_posts", refresh_token: "some_refresh_token")
token.extra.not_nil!["body"].should eq %("grant_type=refresh_token&refresh_token=some_refresh_token&scope=read_posts")
token.access_token.should eq "access_token"
end
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Is a newline missing after this line?

@jhass jhass added this to the 0.35.0 milestone May 7, 2020
@waj waj merged commit b8d4ca0 into crystal-lang:master May 11, 2020
@crush-157 crush-157 deleted the auth-scheme-option branch May 11, 2020 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants