Module: RobustClientSocket::HTTP::PrivateClassMethods

Defined in:
lib/robust_client_socket/http/helpers.rb

Constant Summary collapse

MIN_KEY_SIZE =
2048

Instance Method Summary collapse

Instance Method Details

#app_tokenObject



59
60
61
# File 'lib/robust_client_socket/http/helpers.rb', line 59

def app_token
  "#{client_name}_#{time_now_in_utc}"
end

#encrypted_data(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/robust_client_socket/http/helpers.rb', line 25

def encrypted_data(data)
  validate_key_security!

  encrypted = rsa_key.public_encrypt(
    data,
    OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
  )

  ::Base64.strict_encode64(encrypted)
rescue OpenSSL::PKey::RSAError => e
  raise SecurityError, "Encryption failed: #{e.message}"
end

#public_keyObject



55
56
57
# File 'lib/robust_client_socket/http/helpers.rb', line 55

def public_key
  credentials[:public_key]
end

#robust_headersObject



13
14
15
16
17
18
19
# File 'lib/robust_client_socket/http/helpers.rb', line 13

def robust_headers
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'User-Agent' => "RobustClientSocket/#{RobustClientSocket::VERSION}"
  }
end

#rsa_keyObject



38
39
40
41
42
43
44
# File 'lib/robust_client_socket/http/helpers.rb', line 38

def rsa_key
  @rsa_key ||= begin
    key = OpenSSL::PKey::RSA.new(public_key)
    raise SecurityError, "Invalid public key" unless key.public?
    key
  end
end

#secure_tokenObject



21
22
23
# File 'lib/robust_client_socket/http/helpers.rb', line 21

def secure_token
  encrypted_data(app_token)
end

#time_now_in_utcObject



63
64
65
# File 'lib/robust_client_socket/http/helpers.rb', line 63

def time_now_in_utc
  Time.now.utc.to_i
end

#validate_key_security!Object



46
47
48
49
50
51
52
53
# File 'lib/robust_client_socket/http/helpers.rb', line 46

def validate_key_security!
  key_bits = rsa_key.n.num_bits

  if key_bits < MIN_KEY_SIZE
    raise SecurityError,
      "RSA key size (#{key_bits} bits) below minimum (#{MIN_KEY_SIZE} bits)"
  end
end