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

Add custom attributes support to token generator #1652

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ User-visible changes worth mentioning.
## main

- [#ID] Add your PR description here.
- [#1652] Add custom attributes support to token generator

## 5.6.6

Expand Down
4 changes: 4 additions & 0 deletions lib/doorkeeper/models/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ def attributes_for_token_generator
if Doorkeeper.config.polymorphic_resource_owner?
attributes[:resource_owner] = resource_owner
end

Doorkeeper.config.custom_access_token_attributes.each do |attribute_name|
attributes[attribute_name] = public_send(attribute_name)
end
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/models/doorkeeper/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def self.generate(opts = {})
expect(token.token).to eq "custom_generator_token_#{created_at.to_i}"
end

it "allows the custom generator to access the custom attributes" do
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:tenant_name]}"
end
end

Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
custom_access_token_attributes [:tenant_name]
end

token = FactoryBot.create :access_token, tenant_name: "Tenant 1"
expect(token.token).to eq "custom_generator_token_Tenant 1"
end

it "raises an error if the custom object does not support generate" do
module NoGenerate
end
Expand Down