Api Token: store token in an encrypted form
This commit is contained in:
parent
1997f45d7e
commit
8c4f8347ca
7 changed files with 34 additions and 13 deletions
|
@ -4,8 +4,7 @@ module NewAdministrateur
|
|||
end
|
||||
|
||||
def renew_api_token
|
||||
current_administrateur.renew_api_token
|
||||
@token = current_administrateur.api_token
|
||||
@token = current_administrateur.renew_api_token
|
||||
flash.now.notice = 'Votre jeton a été regénéré.'
|
||||
render :show
|
||||
end
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
class Administrateur < ApplicationRecord
|
||||
include CredentialsSyncableConcern
|
||||
include EmailSanitizableConcern
|
||||
include ActiveRecord::SecureToken
|
||||
|
||||
devise :database_authenticatable, :registerable, :async,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
@ -36,7 +37,10 @@ class Administrateur < ApplicationRecord
|
|||
end
|
||||
|
||||
def renew_api_token
|
||||
update(api_token: generate_api_token)
|
||||
api_token = Administrateur.generate_unique_secure_token
|
||||
encrypted_token = BCrypt::Password.create(api_token)
|
||||
update(api_token: api_token, encrypted_token: encrypted_token)
|
||||
api_token
|
||||
end
|
||||
|
||||
def registration_state
|
||||
|
@ -109,13 +113,4 @@ class Administrateur < ApplicationRecord
|
|||
def owns?(procedure)
|
||||
id == procedure.administrateur_id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_api_token
|
||||
loop do
|
||||
token = SecureRandom.hex(20)
|
||||
break token if !Administrateur.find_by(api_token: token)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue