Class: MultiPass
- Inherits:
-
Object
- Object
- MultiPass
- Defined in:
- lib/multipass.rb
Defined Under Namespace
Classes: DecryptError, ExpiredError, Invalid, JSONError
Class Method Summary collapse
Instance Method Summary collapse
-
#decode(data) ⇒ Object
Decrypts the given multipass string and parses it as JSON.
-
#encode(options = {}) ⇒ Object
Encrypts the given hash into a multipass string.
-
#initialize(site_key, api_key) ⇒ MultiPass
constructor
A new instance of MultiPass.
Constructor Details
#initialize(site_key, api_key) ⇒ MultiPass
Returns a new instance of MultiPass.
33 34 35 36 37 |
# File 'lib/multipass.rb', line 33 def initialize(site_key, api_key) @site_key = site_key @api_key = api_key @crypto_key = EzCrypto::Key.with_password(@site_key, @api_key) end |
Class Method Details
.decode(site_key, api_key, data) ⇒ Object
29 30 31 |
# File 'lib/multipass.rb', line 29 def self.decode(site_key, api_key, data) new(site_key, api_key).decode(data) end |
.encode(site_key, api_key, options = {}) ⇒ Object
25 26 27 |
# File 'lib/multipass.rb', line 25 def self.encode(site_key, api_key, = {}) new(site_key, api_key).encode() end |
Instance Method Details
#decode(data) ⇒ Object
Decrypts the given multipass string and parses it as JSON. Then, it checks for a valid expiration date.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/multipass.rb', line 51 def decode(data) json = @crypto_key.decrypt64(data) if json.nil? raise MultiPass::DecryptError end = decode_json(json) if !.is_a?(Hash) raise MultiPass::JSONError end .keys.each do |key| [key.to_sym] = .delete(key) end if [:expires].nil? || Time.now.utc > Time.parse([:expires]) raise MultiPass::ExpiredError end rescue OpenSSL::CipherError raise MultiPass::DecryptError end |
#encode(options = {}) ⇒ Object
Encrypts the given hash into a multipass string.
40 41 42 43 44 45 46 47 |
# File 'lib/multipass.rb', line 40 def encode( = {}) [:expires] = case [:expires] when Fixnum then Time.at([:expires]).to_s when Time, DateTime, Date then [:expires].to_s else [:expires].to_s end @crypto_key.encrypt64(.to_json) end |