Module: SimpleApiAuth::Authenticable::ClassMethods

Defined in:
lib/simple-api-auth/authenticable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#saa_optionsObject

Returns the value of attribute saa_options.



21
22
23
# File 'lib/simple-api-auth/authenticable.rb', line 21

def saa_options
  @saa_options
end

Instance Method Details

#api_authenticable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/simple-api-auth/authenticable.rb', line 23

def api_authenticable?
  true
end

#generate_saa_key(options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/simple-api-auth/authenticable.rb', line 41

def generate_saa_key(options = {})
  length = options[:length] || (Math.log(count + 1, 64) + 5)
  loop do
    key = SecureRandom.urlsafe_base64(length)
    break key unless exists?(saa_options[:saa_key] => key)
  end
end

#generate_saa_secret(options = {}) ⇒ Object



49
50
51
52
# File 'lib/simple-api-auth/authenticable.rb', line 49

def generate_saa_secret(options = {})
  length = options[:length] || 64
  SecureRandom.urlsafe_base64(length)
end

#saa_authenticate(request) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/simple-api-auth/authenticable.rb', line 27

def saa_authenticate(request)
  request = SimpleApiAuth::Request.create(request)
  entity = saa_find(request)
  return false if entity.nil?
  secret_key = entity.send(saa_options[:saa_secret])
  return false unless SimpleApiAuth.valid_signature?(request, secret_key)
  entity
end

#saa_find(request) ⇒ Object



36
37
38
39
# File 'lib/simple-api-auth/authenticable.rb', line 36

def saa_find(request)
  key = SimpleApiAuth.extract_key(request)
  find_by(saa_options[:saa_key] => key)
end