Skip to content

Commit

Permalink
Merge pull request #1652 from raphaelcosta/feature/add-custom-attribu…
Browse files Browse the repository at this point in the history
…tes-to-token-generator

Add custom attributes support to token generator
  • Loading branch information
nbulaj committed Apr 18, 2023
2 parents 986115c + 4e95934 commit 1371bd4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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

0 comments on commit 1371bd4

Please sign in to comment.