Class: BorrowDirect::Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/borrow_direct/encryption.rb

Overview

Implements Relais’ encryption protocol relais.atlassian.net/wiki/display/ILL/Encryption

value = BorrowDirect::Encryption.new(public_key_string).encode_with_ts(api_key_or_other_data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_public_key) ⇒ Encryption

Returns a new instance of Encryption.



12
13
14
# File 'lib/borrow_direct/encryption.rb', line 12

def initialize(a_public_key)
  @public_key_str = a_public_key
end

Instance Attribute Details

#public_key_strObject (readonly)

Returns the value of attribute public_key_str.



10
11
12
# File 'lib/borrow_direct/encryption.rb', line 10

def public_key_str
  @public_key_str
end

Instance Method Details

#encode_with_ts(value) ⇒ Object

Will add on timestamp according to Relais protocol, encrypt, and Base64-encode, all per Relais protocol.



18
19
20
21
22
23
24
25
26
27
# File 'lib/borrow_direct/encryption.rb', line 18

def encode_with_ts(value)
  # Not sure if this object is thread-safe, so we re-create
  # each time. 

  public_key = OpenSSL::PKey::RSA.new(self.public_key_str)

  payload = "#{value}|#{self.now_timestamp}"

  return Base64.encode64(public_key.public_encrypt(payload))
end

#now_timestampObject

Timestamp formatted how Relais wants it, in UTC



30
31
32
# File 'lib/borrow_direct/encryption.rb', line 30

def now_timestamp
  Time.now.getutc.strftime("%Y%m%d %H%M%S")
end