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)


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

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

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

Returns:



5
6
7
# File 'lib/ig_markets/password_encryptor.rb', line 5

def public_key
  @public_key
end

#time_stampString

Returns The time stamp used by #encrypt.

Returns:

  • (String)

    The time stamp used by #encrypt.



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

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.



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

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.



32
33
34
35
36
37
38
# File 'lib/ig_markets/password_encryptor.rb', line 32

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