Class: Kennedy::Ticket
- Inherits:
-
Object
- Object
- Kennedy::Ticket
- Defined in:
- lib/kennedy/ticket.rb
Overview
A ticket represents a time-constrained period in which an authenticated person can access a service
Constant Summary collapse
- DefaultExpiry =
In seconds
30
Instance Attribute Summary collapse
-
#identifier ⇒ Object
Returns the value of attribute identifier.
Class Method Summary collapse
-
.create(args = {}) ⇒ Object
Creates a new ticket with the given arguments.
-
.from_encrypted(args = {}) ⇒ Object
Decrypts a ticket from the given arguments.
Instance Method Summary collapse
-
#decrypt(data) ⇒ Object
Decrypts the given ticket data.
- #expired? ⇒ Boolean
-
#initialize(args = {}) ⇒ Ticket
constructor
A new instance of Ticket.
-
#to_encrypted ⇒ String
Generates an encrypted chunk of JSON with the identifier and expiration time for this ticket encoded in.
Constructor Details
#initialize(args = {}) ⇒ Ticket
Returns a new instance of Ticket.
47 48 49 50 51 |
# File 'lib/kennedy/ticket.rb', line 47 def initialize(args = {}) @iv = args[:iv] || raise(ArgumentError, "Ticket encryption IV must be given as :iv") @passphrase = args[:passphrase] || raise(ArgumentError, "Ticket encryption passphrase must be given as :passphrase") @expiry = args[:expiry] || DefaultExpiry end |
Instance Attribute Details
#identifier ⇒ Object
Returns the value of attribute identifier.
12 13 14 |
# File 'lib/kennedy/ticket.rb', line 12 def identifier @identifier end |
Class Method Details
.create(args = {}) ⇒ Object
Creates a new ticket with the given arguments
25 26 27 28 29 30 |
# File 'lib/kennedy/ticket.rb', line 25 def self.create(args = {}) identifier = args[:identifier] || raise(ArgumentError, "Ticket identifier must be given as :identifier") ticket = new(:iv => args[:iv], :passphrase => args[:passphrase], :expiry => args[:expiry]) ticket.identifier = identifier ticket end |
.from_encrypted(args = {}) ⇒ Object
Decrypts a ticket from the given arguments
37 38 39 40 41 42 |
# File 'lib/kennedy/ticket.rb', line 37 def self.from_encrypted(args = {}) data = args[:data] || raise(ArgumentError, "Data must be given as :data") ticket = new(:iv => args[:iv], :passphrase => args[:passphrase]) ticket.decrypt(data) ticket end |
Instance Method Details
#decrypt(data) ⇒ Object
Decrypts the given ticket data
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kennedy/ticket.rb', line 72 def decrypt(data) cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.decrypt cipher.key = @passphrase cipher.iv = @iv decrypted = cipher.update(data) decrypted << cipher.final json = JSON.parse(decrypted) self.identifier = json['identifier'] @expiry = Time.parse(json['expiry']) rescue OpenSSL::Cipher::CipherError => e raise Kennedy::BadTicketException, "Given data was not decryptable" end |
#expired? ⇒ Boolean
86 87 88 |
# File 'lib/kennedy/ticket.rb', line 86 def expired? !@expiry.nil? && (@expiry < Time.now) end |
#to_encrypted ⇒ String
Generates an encrypted chunk of JSON with the identifier and expiration time for this ticket encoded in
60 61 62 63 64 65 66 67 68 |
# File 'lib/kennedy/ticket.rb', line 60 def to_encrypted cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.encrypt cipher.key = @passphrase cipher.iv = @iv encrypted = cipher.update(to_expiring_json) encrypted << cipher.final encrypted end |