diff --git a/CHANGELOG.md b/CHANGELOG.md index ae950b08b..f77baf81d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ User-visible changes worth mentioning. ## main - [#1696] Add missing `#issued_token` method to `OAuth::TokenResponse` +- [#1697] Allow a TokenResponse body to be customized. ## 5.6.9 diff --git a/lib/doorkeeper/oauth/token_response.rb b/lib/doorkeeper/oauth/token_response.rb index a000b0112..a1d44b493 100644 --- a/lib/doorkeeper/oauth/token_response.rb +++ b/lib/doorkeeper/oauth/token_response.rb @@ -12,7 +12,7 @@ def initialize(token) end def body - { + @body ||= { "access_token" => token.plaintext_token, "token_type" => token.token_type, "expires_in" => token.expires_in_seconds, diff --git a/spec/lib/oauth/token_response_spec.rb b/spec/lib/oauth/token_response_spec.rb index 8de8e6ea7..70a7de0a0 100644 --- a/spec/lib/oauth/token_response_spec.rb +++ b/spec/lib/oauth/token_response_spec.rb @@ -55,6 +55,28 @@ end end + describe ".body attributes" do + subject(:token_response) { described_class.new(access_token) } + + let(:access_token) do + double :access_token, + plaintext_token: "some-token", + expires_in: "3600", + expires_in_seconds: "300", + scopes_string: "two scopes", + plaintext_refresh_token: "some-refresh-token", + token_type: "Bearer", + custom_parameter: "custom_value", + created_at: 0 + end + + it "can be augmented" do + token_response.body["custom_parameter"] = access_token.custom_parameter + + expect(token_response.body["custom_parameter"]).to eq("custom_value") + end + end + describe ".body filters out empty values" do subject(:body) { described_class.new(access_token).body }