Skip to content

Commit f9d5e3d

Browse files
committed
Merge pull request #736 from jimmykarily/fix_issue_661
Reuse the existing token in client_credentials flow
2 parents 61656be + 4d6b8f0 commit f9d5e3d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
User-visible changes worth mentioning.
44

55
---
6-
6+
- [#736] Existing valid tokens are now resused in client_credentials flow
77
- [#722] `doorkeeper_forbidden_render_options` now supports returning a 404 by
88
specifying `respond_not_found_when_forbidden: true` in the
99
`doorkeeper_forbidden_render_options` method.

lib/doorkeeper/oauth/client_credentials/creator.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ module OAuth
33
class ClientCredentialsRequest
44
class Creator
55
def call(client, scopes, attributes = {})
6-
AccessToken.create(attributes.merge(
7-
application_id: client.id,
8-
scopes: scopes.to_s
9-
))
6+
AccessToken.find_or_create_for(
7+
client, nil, scopes, attributes[:expires_in],
8+
attributes[:use_refresh_token])
109
end
1110
end
1211
end

spec/lib/oauth/client_credentials/creator_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,32 @@ class Doorkeeper::OAuth::ClientCredentialsRequest
1111
end.to change { Doorkeeper::AccessToken.count }.by(1)
1212
end
1313

14+
context "when reuse_access_token is true" do
15+
it "returns the existing valid token" do
16+
allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)
17+
existing_token = subject.call(client, scopes)
18+
19+
result = subject.call(client, scopes)
20+
21+
expect(Doorkeeper::AccessToken.count).to eq(1)
22+
expect(result).to eq(existing_token)
23+
end
24+
end
25+
26+
context "when reuse_access_token is false" do
27+
it "returns a new token" do
28+
allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(false)
29+
existing_token = subject.call(client, scopes)
30+
31+
result = subject.call(client, scopes)
32+
33+
expect(Doorkeeper::AccessToken.count).to eq(2)
34+
expect(result).not_to eq(existing_token)
35+
end
36+
end
37+
1438
it 'returns false if creation fails' do
15-
expect(Doorkeeper::AccessToken).to receive(:create).and_return(false)
39+
expect(Doorkeeper::AccessToken).to receive(:find_or_create_for).and_return(false)
1640
created = subject.call(client, scopes)
1741
expect(created).to be_falsey
1842
end

0 commit comments

Comments
 (0)