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.



31
32
33
34
35
36
37
# File 'lib/auth/helpers.rb', line 31

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



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

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.



26
27
28
# File 'lib/auth/helpers.rb', line 26

def encode(object)
  object.to_json
end

#encode_scopes(*scopes) ⇒ Object



48
49
50
51
# File 'lib/auth/helpers.rb', line 48

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



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

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



8
9
10
11
12
# File 'lib/auth/helpers.rb', line 8

def generate_secret
  Base64.encode64(
    Digest::SHA256.digest("#{Time.now}-#{rand}")
  ).gsub('/','x').gsub('+','y').gsub('=','').strip
end