Module: Auth::Helpers

Included in:
Auth
Defined in:
lib/auth/helpers.rb

Instance Method Summary collapse

Instance Method Details

#decode(object) ⇒ Object

Given a string, returns a Ruby object.



40
41
42
43
44
45
46
# File 'lib/auth/helpers.rb', line 40

def decode(object)
  return unless object
  begin
    JSON.parse(object)
  rescue JSON::ParserError
  end
end

#decode_scopes(scopes) ⇒ Object

Decode a space delimited string of security scopes and return an array



49
50
51
52
53
54
55
# File 'lib/auth/helpers.rb', line 49

def decode_scopes(scopes)
  if scopes.is_a?(Array)
    scopes.map {|s| s.to_s.strip }
  else
    scopes.to_s.split(' ').map {|s| s.strip }
  end
end

#encode(object) ⇒ Object

Given a Ruby object, returns a string suitable for storage in a queue.



35
36
37
# File 'lib/auth/helpers.rb', line 35

def encode(object)
  object.to_json
end

#encode_scopes(*scopes) ⇒ Object



57
58
59
60
# File 'lib/auth/helpers.rb', line 57

def encode_scopes(*scopes)
  scopes = scopes.flatten.compact
  scopes.map {|s| s.to_s.strip.gsub(' ','_') }.sort.join(' ')
end

#encrypt_password(password, salt, hash) ⇒ Object

Obfuscate a password using a salt and a cryptographic hash function



24
25
26
27
28
29
30
31
# File 'lib/auth/helpers.rb', line 24

def encrypt_password(password, salt, hash)
  case hash.to_s
  when 'sha256'
    Digest::SHA256.hexdigest("#{password}-#{salt}")
  else
    raise 'Unsupported hash algorithm'
  end
end

#generate_secretObject

Generate a unique cryptographically secure secret



13
14
15
16
17
18
19
20
21
# File 'lib/auth/helpers.rb', line 13

def generate_secret
  if defined?(SecureRandom)
    SecureRandom.urlsafe_base64(32)
  else
    Base64.encode64(
      Digest::SHA256.digest("#{Time.now}-#{Time.now.usec}-#{$$}-#{rand}")
    ).gsub('/','-').gsub('+','_').gsub('=','').strip
  end
end