Module: SecondStep::Models::ApplicationUserLink

Included in:
ActiveRecordAdapter::ApplicationUserLink, SecondStep::MemoryAdapter::ApplicationUserLink
Defined in:
lib/second_step/models/application_user_link.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_entropy!Object



43
44
45
# File 'lib/second_step/models/application_user_link.rb', line 43

def check_entropy!
  raise NotEnoughEntropyForPhraseError unless enough_entropy?
end

.config_reader(name, config_property = name) ⇒ Object

ORM implementation:

Attributes: secret, uuid Belongs to: application_user



15
16
17
18
19
20
# File 'lib/second_step/models/application_user_link.rb', line 15

def self.config_reader(name, config_property=name)
  instance_eval { define_method name do
    SecondStep.config.send config_property
  end }
  module_function name
end

.enough_entropy?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/second_step/models/application_user_link.rb', line 38

def enough_entropy?
  entropy > min_entropy
end

Instance Method Details

#checksum_valid?(secret = self.secret) ⇒ Boolean

A simple check to see if the secret’s checksum is correct. This prevents a large amount of typos, but doesn’t do anything else.

Returns:

  • (Boolean)


50
51
52
# File 'lib/second_step/models/application_user_link.rb', line 50

def checksum_valid?(secret = self.secret)
  Digest::MD5.base64digest(secret.chop)[0] == secret[-1]
end

#decode_token(token) ⇒ Object



98
99
100
101
102
# File 'lib/second_step/models/application_user_link.rb', line 98

def decode_token(token)
  rsa_data = decode_and_verify_rsa_token! token
  hmac_data = decode_and_verify_hmac_token! rsa_data[:token]
  return rsa_data, hmac_data
end

#random_delimiterObject



67
68
69
# File 'lib/second_step/models/application_user_link.rb', line 67

def random_delimiter
  delimiters[SecureRandom.random_number(delimiters.length)]
end

#random_wordObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/second_step/models/application_user_link.rb', line 71

def random_word
  word_number = SecureRandom.random_number word_list_count
  File.open(word_list_path, 'r') do |list|
    word = nil
    # This iterates through the entire file to pre
    list.each_line.each_with_index do |w, i|
      w = w.chomp
      word = w if word_number == i
    end
    word
  end
end

#rsa_token(*args, **opts) ⇒ Object Also known as: token



93
94
95
# File 'lib/second_step/models/application_user_link.rb', line 93

def rsa_token(*args, **opts)
  JWT.encode self.rsa_token_payload(*args, **opts), private_key, jwt_rsa_algorithm
end

#secret=(t) ⇒ Object



54
55
56
# File 'lib/second_step/models/application_user_link.rb', line 54

def secret=(t)
  super t ? t << Digest::MD5.base64digest(t)[0] : t
end

#secret_phraseObject



62
63
64
65
# File 'lib/second_step/models/application_user_link.rb', line 62

def secret_phrase
  check_entropy!
  self.secret = phrase_length.times.map{ random_word }.join(random_delimiter) + ' '
end

#secret_qrObject



84
85
86
87
# File 'lib/second_step/models/application_user_link.rb', line 84

def secret_qr
  self.secret = SecureRandom.base64 qr_length - 1
  RQRCode::QRCode.new self.secret
end

#secret_stringObject



58
59
60
# File 'lib/second_step/models/application_user_link.rb', line 58

def secret_string
  self.secret = SecureRandom.base64 secret_string_length - 1
end

#uuidObject



89
90
91
# File 'lib/second_step/models/application_user_link.rb', line 89

def uuid
  super || self.uuid = SecureRandom.uuid
end