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

Allow extra data for new token creation method #1570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ def token_can_be_reused?(token, client)
end

# update user's auth token (should happen on each request)
def create_new_auth_token(client = nil)
def create_new_auth_token(client = nil, extra_data: {})
now = Time.zone.now

token = create_token(
client: client,
previous_token: tokens.fetch(client, {})['token'],
last_token: tokens.fetch(client, {})['previous_token'],
updated_at: now
updated_at: now,
**extra_data
)

update_auth_header(token.token, token.client)
Expand Down
17 changes: 17 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ class UserTest < ActiveSupport::TestCase
end
end

describe 'token with extra data' do
let(:extra_data) { { scope: "write:profile" } }
before do
@resource = create(:user, :confirmed)

@auth_headers = @resource.create_new_auth_token(extra_data)

@token = @auth_headers['access-token']
@client_id = @auth_headers['client']
end

test 'should extra data will be stored with token info' do
assert @resource.token_is_current?(@token, @client_id)
assert @resource.tokens[@client_id]["scope"], extra_data["scope"]
end
end

describe 'previous token' do
before do
@resource = create(:user, :confirmed)
Expand Down