Module: OneTimePassword::OneTimeAuthenticationModel::ClassMethods
- Defined in:
- lib/one_time_password/one_time_authentication_model.rb
Instance Method Summary collapse
- #create_one_time_authentication(context, user_key, user_key_downcase: true) ⇒ Object
- #find_context(function_name) ⇒ Object
- #find_one_time_authentication(context, user_key, user_key_downcase: true) ⇒ Object
- #generate_random_password(length = 6) ⇒ Object
- #recent_failed_authenticate_password_count(user_key, time_ago) ⇒ Object
Instance Method Details
#create_one_time_authentication(context, user_key, user_key_downcase: true) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/one_time_password/one_time_authentication_model.rb', line 44 def create_one_time_authentication(context, user_key, user_key_downcase: true) if user_key.blank? raise OneTimePassword::Errors::NoUserKeyArgmentError, 'Not present user_key.' end user_key = user_key.downcase if user_key_downcase recent_failed_authenticate_password_count = OneTimeAuthentication .recent_failed_authenticate_password_count( user_key, context[:password_failed_period] ) if recent_failed_authenticate_password_count <= context[:password_failed_limit] one_time_authentication = OneTimeAuthentication.new( function_name: context[:function_name], user_key: user_key, password_length: context[:password_length], expires_seconds: context[:expires_in].to_i, max_authenticate_password_count: context[:max_authenticate_password_count], ) one_time_authentication.set_password_and_password_length(context[:password_length]) one_time_authentication.save! else one_time_authentication = nil end one_time_authentication end |
#find_context(function_name) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/one_time_password/one_time_authentication_model.rb', line 20 def find_context(function_name) context = OneTimePassword::CONTEXTS .select{ |context| context[:function_name] == function_name } .first if context.nil? raise ArgumentError, 'Not found context.' elsif context[:expires_in].class != ActiveSupport::Duration raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:expires_in].' elsif context[:max_authenticate_password_count].class != Integer raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:max_authenticate_password_count].' elsif context[:password_length].class != Integer raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_length].' elsif context[:password_failed_limit].class != Integer raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_failed_limit].' elsif context[:password_failed_period].class != ActiveSupport::Duration raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_failed_period].' end context end |
#find_one_time_authentication(context, user_key, user_key_downcase: true) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/one_time_password/one_time_authentication_model.rb', line 76 def find_one_time_authentication(context, user_key, user_key_downcase: true) if user_key.blank? raise OneTimePassword::Errors::NoUserKeyArgmentError, 'Not present user_key.' end user_key = user_key.downcase if user_key_downcase OneTimeAuthentication .where(function_name: context[:function_name]) .where(user_key: user_key) .last end |
#generate_random_password(length = 6) ⇒ Object
90 91 92 |
# File 'lib/one_time_password/one_time_authentication_model.rb', line 90 def generate_random_password(length=6) length.times.map{ SecureRandom.random_number(10) }.join end |
#recent_failed_authenticate_password_count(user_key, time_ago) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/one_time_password/one_time_authentication_model.rb', line 94 def recent_failed_authenticate_password_count(user_key, time_ago) OneTimeAuthentication .where(user_key: user_key) .recent(time_ago) .sum(:failed_count) end |