Skip to content

Commit ccf06c2

Browse files
committed
Refactor token creation
1 parent d71c054 commit ccf06c2

File tree

10 files changed

+23
-86
lines changed

10 files changed

+23
-86
lines changed

app/controllers/devise_token_auth/confirmations_controller.rb

+3-11
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@ def show
44
@resource = resource_class.confirm_by_token(params[:confirmation_token])
55

66
if @resource && @resource.id
7-
# create client id
8-
client_id = SecureRandom.urlsafe_base64(nil, false)
9-
token = SecureRandom.urlsafe_base64(nil, false)
10-
token_hash = BCrypt::Password.create(token)
11-
expiry = (Time.now + @resource.token_lifespan).to_i
12-
13-
if defined? @resource.sign_in_count && @resource.sign_in_count > 0
7+
expiry = nil
8+
if defined?(@resource.sign_in_count) && @resource.sign_in_count > 0
149
expiry = (Time.now + 1.second).to_i
1510
end
1611

17-
@resource.tokens[client_id] = {
18-
token: token_hash,
19-
expiry: expiry
20-
}
12+
client_id, token = @resource.create_token expiry: expiry
2113

2214
sign_in(@resource)
2315
@resource.save!

app/controllers/devise_token_auth/omniauth_callbacks_controller.rb

+2-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def redirect_callbacks
2727

2828
def omniauth_success
2929
get_resource_from_auth_hash
30-
create_token_info
3130
set_token_on_resource
3231
create_auth_params
3332

@@ -156,14 +155,6 @@ def set_random_password
156155
@resource.password_confirmation = p
157156
end
158157

159-
def create_token_info
160-
# create token info
161-
@client_id = SecureRandom.urlsafe_base64(nil, false)
162-
@token = SecureRandom.urlsafe_base64(nil, false)
163-
@expiry = (Time.now + @resource.token_lifespan).to_i
164-
@config = omniauth_params['config_name']
165-
end
166-
167158
def create_auth_params
168159
@auth_params = {
169160
auth_token: @token,
@@ -177,10 +168,8 @@ def create_auth_params
177168
end
178169

179170
def set_token_on_resource
180-
@resource.tokens[@client_id] = {
181-
token: BCrypt::Password.create(@token),
182-
expiry: @expiry
183-
}
171+
@config = omniauth_params['config_name']
172+
@client_id, @token, @expiry = @resource.create_token
184173
end
185174

186175
def render_data(message, data)

app/controllers/devise_token_auth/passwords_controller.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,7 @@ def edit
5555
@resource = with_reset_password_token(resource_params[:reset_password_token])
5656

5757
if @resource && @resource.reset_password_period_valid?
58-
client_id = SecureRandom.urlsafe_base64(nil, false)
59-
token = SecureRandom.urlsafe_base64(nil, false)
60-
token_hash = BCrypt::Password.create(token)
61-
expiry = (Time.now + @resource.token_lifespan).to_i
62-
63-
@resource.tokens[client_id] = {
64-
token: token_hash,
65-
expiry: expiry
66-
}
58+
client_id, token = @resource.create_token
6759

6860
# ensure that user is confirmed
6961
@resource.skip_confirmation! if confirmable_enabled? && !@resource.confirmed_at

app/controllers/devise_token_auth/registrations_controller.rb

+1-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ def create
5454

5555
else
5656
# email auth has been bypassed, authenticate user
57-
@client_id = SecureRandom.urlsafe_base64(nil, false)
58-
@token = SecureRandom.urlsafe_base64(nil, false)
59-
60-
@resource.tokens[@client_id] = {
61-
token: BCrypt::Password.create(@token),
62-
expiry: (Time.now + @resource.token_lifespan).to_i
63-
}
57+
@client_id, @token = @resource.create_token
6458

6559
@resource.save!
6660

app/controllers/devise_token_auth/sessions_controller.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ def create
2525
render_create_error_bad_credentials
2626
return
2727
end
28-
# create client id
29-
@client_id = SecureRandom.urlsafe_base64(nil, false)
30-
@token = SecureRandom.urlsafe_base64(nil, false)
31-
32-
@resource.tokens[@client_id] = {
33-
token: BCrypt::Password.create(@token),
34-
expiry: (Time.now + @resource.token_lifespan).to_i
35-
}
28+
@client_id, @token = @resource.create_token
3629
@resource.save
3730

3831
sign_in(:user, @resource, store: false, bypass: false)

app/controllers/devise_token_auth/unlocks_controller.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@ def show
3535
@resource = resource_class.unlock_access_by_token(params[:unlock_token])
3636

3737
if @resource && @resource.id
38-
client_id = SecureRandom.urlsafe_base64(nil, false)
39-
token = SecureRandom.urlsafe_base64(nil, false)
40-
token_hash = BCrypt::Password.create(token)
41-
expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
42-
43-
@resource.tokens[client_id] = {
44-
token: token_hash,
45-
expiry: expiry
46-
}
47-
38+
client_id, token = @resource.create_token
4839
@resource.save!
4940
yield @resource if block_given?
5041

app/models/devise_token_auth/concerns/user.rb

+11
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ def send_unlock_instructions(opts=nil)
106106
send_devise_notification(:unlock_instructions, raw, opts)
107107
raw
108108
end
109+
110+
def create_token(client_id: nil, token: nil, expiry: nil)
111+
client_id ||= SecureRandom.urlsafe_base64(nil, false)
112+
token ||= SecureRandom.urlsafe_base64(nil, false)
113+
expiry ||= (Time.now + token_lifespan).to_i
114+
tokens[client_id] = {
115+
token: BCrypt::Password.create(token),
116+
expiry: expiry
117+
}
118+
[client_id, token, expiry]
119+
end
109120
end
110121

111122
module ClassMethods

test/dummy/app/controllers/overrides/confirmations_controller.rb

+1-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@ def show
44
@resource = resource_class.confirm_by_token(params[:confirmation_token])
55

66
if @resource and @resource.id
7-
# create client id
8-
client_id = SecureRandom.urlsafe_base64(nil, false)
9-
token = SecureRandom.urlsafe_base64(nil, false)
10-
token_hash = BCrypt::Password.create(token)
11-
expiry = (Time.now + @resource.token_lifespan).to_i
12-
13-
@resource.tokens[client_id] = {
14-
token: token_hash,
15-
expiry: expiry
16-
}
17-
7+
client_id, token = @resource.create_token
188
@resource.save!
199

2010
redirect_header_options = {

test/dummy/app/controllers/overrides/passwords_controller.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ def edit
99
})
1010

1111
if @resource and @resource.id
12-
client_id = SecureRandom.urlsafe_base64(nil, false)
13-
token = SecureRandom.urlsafe_base64(nil, false)
14-
token_hash = BCrypt::Password.create(token)
15-
expiry = (Time.now + @resource.token_lifespan).to_i
16-
17-
@resource.tokens[client_id] = {
18-
token: token_hash,
19-
expiry: expiry
20-
}
12+
client_id, token = @resource.create_token
2113

2214
# ensure that user is confirmed
2315
@resource.skip_confirmation! unless @resource.confirmed_at

test/dummy/app/controllers/overrides/sessions_controller.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ def create
66
@resource = resource_class.find_by(email: resource_params[:email])
77

88
if @resource and valid_params?(:email, resource_params[:email]) and @resource.valid_password?(resource_params[:password]) and @resource.confirmed?
9-
# create client id
10-
@client_id = SecureRandom.urlsafe_base64(nil, false)
11-
@token = SecureRandom.urlsafe_base64(nil, false)
12-
13-
@resource.tokens[@client_id] = {
14-
token: BCrypt::Password.create(@token),
15-
expiry: (Time.now + @resource.token_lifespan).to_i
16-
}
9+
@client_id, @token = @resource.create_token
1710
@resource.save
1811

1912
render json: {

0 commit comments

Comments
 (0)