Class: IGMarkets::PasswordEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/password_encryptor.rb

Overview

Encrypts account passwords in the manner required for authentication with the IG Markets API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoded_public_key = nil, time_stamp = nil) ⇒ PasswordEncryptor

Initializes this password encryptor with the specified encoded public key and timestamp.

Parameters:

  • encoded_public_key (String) (defaults to: nil)
  • time_stamp (String) (defaults to: nil)


20
21
22
23
# File 'lib/ig_markets/password_encryptor.rb', line 20

def initialize(encoded_public_key = nil, time_stamp = nil)
  self.encoded_public_key = encoded_public_key if encoded_public_key
  self.time_stamp = time_stamp
end

Instance Attribute Details

#public_keyOpenSSL::PKey::RSA

The public key used by #encrypt, can also be set using #encoded_public_key=.

Returns:

  • (OpenSSL::PKey::RSA)


9
10
11
# File 'lib/ig_markets/password_encryptor.rb', line 9

def public_key
  @public_key
end

#time_stampString

The time stamp used by #encrypt.

Returns:

  • (String)


14
15
16
# File 'lib/ig_markets/password_encryptor.rb', line 14

def time_stamp
  @time_stamp
end

Instance Method Details

#encoded_public_key=(encoded_public_key) ⇒ Object

Takes an encoded public key and calls #public_key= with the decoded key.

Parameters:

  • encoded_public_key (String)

    The public key encoded in Base64.



28
29
30
# File 'lib/ig_markets/password_encryptor.rb', line 28

def encoded_public_key=(encoded_public_key)
  self.public_key = OpenSSL::PKey::RSA.new Base64.strict_decode64 encoded_public_key
end

#encrypt(password) ⇒ String

Encrypts a password using this encryptor’s public key and time stamp, which must have been set prior to calling this method.

Parameters:

  • password (String)

    The password to encrypt.

Returns:

  • (String)

    The encrypted password encoded in Base64.



38
39
40
41
42
43
44
# File 'lib/ig_markets/password_encryptor.rb', line 38

def encrypt(password)
  encoded_password = Base64.strict_encode64 "#{password}|#{time_stamp}"

  encrypted_password = public_key.public_encrypt encoded_password

  Base64.strict_encode64 encrypted_password
end