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

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 timestamp used by #encrypt.

Returns:

  • (String)

    The timestamp 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.



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

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.



23
24
25
26
27
28
29
# File 'lib/ig_markets/password_encryptor.rb', line 23

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