Skip to content

Reuse the existing token in client_credentials flow #736

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

Merged
merged 1 commit into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
User-visible changes worth mentioning.

---

- [#736] Existing valid tokens are now resused in client_credentials flow
- [#722] `doorkeeper_forbidden_render_options` now supports returning a 404 by
specifying `respond_not_found_when_forbidden: true` in the
`doorkeeper_forbidden_render_options` method.
Expand Down
7 changes: 3 additions & 4 deletions lib/doorkeeper/oauth/client_credentials/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module OAuth
class ClientCredentialsRequest
class Creator
def call(client, scopes, attributes = {})
AccessToken.create(attributes.merge(
application_id: client.id,
scopes: scopes.to_s
))
AccessToken.find_or_create_for(
client, nil, scopes, attributes[:expires_in],
attributes[:use_refresh_token])
end
end
end
Expand Down
26 changes: 25 additions & 1 deletion spec/lib/oauth/client_credentials/creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,32 @@ class Doorkeeper::OAuth::ClientCredentialsRequest
end.to change { Doorkeeper::AccessToken.count }.by(1)
end

context "when reuse_access_token is true" do
it "returns the existing valid token" do
allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)
existing_token = subject.call(client, scopes)

result = subject.call(client, scopes)

expect(Doorkeeper::AccessToken.count).to eq(1)
expect(result).to eq(existing_token)
end
end

context "when reuse_access_token is false" do
it "returns a new token" do
allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(false)
existing_token = subject.call(client, scopes)

Choose a reason for hiding this comment

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

Useless assignment to variable - existing_token.


result = subject.call(client, scopes)

expect(Doorkeeper::AccessToken.count).to eq(2)
expect(result).not_to eq(existing_token)
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest the following rewrite of this spec:

context "when reuse_access_token is true" do
  it "returns the existing valid token" do
    allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)
    existing_token = subject.call(client, scopes)

    result = subject.call(client, scopes)

    expect(Doorkeeper::AccessToken.count).to eq(1)
    expect(result).to eq(existing_token)
  end
end

context "when reuse_access_token is false" do
  it "returns a new token" do
    allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(false)
    existing_token = subject.call(client, scopes)

    result = subject.call(client, scopes)

    expect(Doorkeeper::AccessToken.count).to eq(2)
    expect(result).not_to eq(existing_token)
  end
end
  • It follows the setup/execute/assertions structure, making it easier to read
  • It removes the unneeded before block
  • It makes both options look more alike, to better illustrate the difference (with the config on or off, it's easier to visualize how behavior changes)
  • It corrects the description for the second test, and adjusts the first one
  • It introduces an explaining variable for existing_token in the second spec
  • It consistently uses double quotes

Could you update your copy? It might not be correct syntax (don't have my tests set up right now), and Hound might have one or two complaints, but I'd like to keep these advantages.

Thank you!


it 'returns false if creation fails' do
expect(Doorkeeper::AccessToken).to receive(:create).and_return(false)
expect(Doorkeeper::AccessToken).to receive(:find_or_create_for).and_return(false)
created = subject.call(client, scopes)
expect(created).to be_falsey
end
Expand Down