use multiple trusted_device_token

This commit is contained in:
simon lehericey 2019-02-01 18:11:55 +01:00
parent bee9a108c5
commit b9b83cca3a
4 changed files with 35 additions and 11 deletions

View file

@ -1,7 +1,6 @@
class Gestionnaire < ApplicationRecord class Gestionnaire < ApplicationRecord
include CredentialsSyncableConcern include CredentialsSyncableConcern
include EmailSanitizableConcern include EmailSanitizableConcern
include ActiveRecord::SecureToken
LOGIN_TOKEN_VALIDITY = 45.minutes LOGIN_TOKEN_VALIDITY = 45.minutes
LOGIN_TOKEN_YOUTH = 15.minutes LOGIN_TOKEN_YOUTH = 15.minutes
@ -20,6 +19,7 @@ class Gestionnaire < ApplicationRecord
has_many :followed_dossiers, through: :follows, source: :dossier has_many :followed_dossiers, through: :follows, source: :dossier
has_many :avis has_many :avis
has_many :dossiers_from_avis, through: :avis, source: :dossier has_many :dossiers_from_avis, through: :avis, source: :dossier
has_many :trusted_device_tokens
def visible_procedures def visible_procedures
procedures.merge(Procedure.avec_lien.or(Procedure.archivees)) procedures.merge(Procedure.avec_lien.or(Procedure.archivees))
@ -136,17 +136,15 @@ class Gestionnaire < ApplicationRecord
end end
def login_token! def login_token!
login_token = Gestionnaire.generate_unique_secure_token trusted_device_token = trusted_device_tokens.create
encrypted_login_token = BCrypt::Password.create(login_token) trusted_device_token.token
update(encrypted_login_token: encrypted_login_token, login_token_created_at: Time.zone.now)
login_token
end end
def login_token_valid?(login_token) def login_token_valid?(login_token)
BCrypt::Password.new(encrypted_login_token) == login_token && trusted_device_token = trusted_device_tokens.find_by(token: login_token)
LOGIN_TOKEN_VALIDITY.ago < login_token_created_at
rescue BCrypt::Errors::InvalidHash trusted_device_token.present? &&
false LOGIN_TOKEN_VALIDITY.ago < trusted_device_token.created_at
end end
def dossiers_id_with_notifications(dossiers) def dossiers_id_with_notifications(dossiers)
@ -213,8 +211,9 @@ class Gestionnaire < ApplicationRecord
end end
def young_login_token? def young_login_token?
login_token_created_at.present? && trusted_device_token = trusted_device_tokens.order(created_at: :desc).first
LOGIN_TOKEN_YOUTH.ago < login_token_created_at trusted_device_token.present? &&
LOGIN_TOKEN_YOUTH.ago < trusted_device_token.created_at
end end
private private

View file

@ -0,0 +1,4 @@
class TrustedDeviceToken < ApplicationRecord
belongs_to :gestionnaire
has_secure_token
end

View file

@ -0,0 +1,11 @@
class CreateTrustedDeviceTokens < ActiveRecord::Migration[5.2]
def change
create_table :trusted_device_tokens do |t|
t.string :token, null: false
t.references :gestionnaire, foreign_key: true
t.timestamps
end
add_index :trusted_device_tokens, :token, unique: true
end
end

View file

@ -520,6 +520,15 @@ ActiveRecord::Schema.define(version: 2019_02_13_144145) do
t.string "version", null: false t.string "version", null: false
end end
create_table "trusted_device_tokens", force: :cascade do |t|
t.string "token", null: false
t.bigint "gestionnaire_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["gestionnaire_id"], name: "index_trusted_device_tokens_on_gestionnaire_id"
t.index ["token"], name: "index_trusted_device_tokens_on_token", unique: true
end
create_table "types_de_champ", id: :serial, force: :cascade do |t| create_table "types_de_champ", id: :serial, force: :cascade do |t|
t.string "libelle" t.string "libelle"
t.string "type_champ" t.string "type_champ"
@ -611,6 +620,7 @@ ActiveRecord::Schema.define(version: 2019_02_13_144145) do
add_foreign_key "received_mails", "procedures" add_foreign_key "received_mails", "procedures"
add_foreign_key "refused_mails", "procedures" add_foreign_key "refused_mails", "procedures"
add_foreign_key "services", "administrateurs" add_foreign_key "services", "administrateurs"
add_foreign_key "trusted_device_tokens", "gestionnaires"
add_foreign_key "types_de_champ", "types_de_champ", column: "parent_id" add_foreign_key "types_de_champ", "types_de_champ", column: "parent_id"
add_foreign_key "without_continuation_mails", "procedures" add_foreign_key "without_continuation_mails", "procedures"
end end