Class: Ree::Licensing::Decryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ree/licensing/decryptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aes_key:, iv:, expires_at:, client_id:) ⇒ Decryptor

Returns a new instance of Decryptor.



13
14
15
16
17
18
# File 'lib/ree/licensing/decryptor.rb', line 13

def initialize(aes_key:, iv:, expires_at:, client_id:)
  @aes_key = aes_key
  @iv = iv
  @expires_at = expires_at
  @client_id = client_id
end

Instance Attribute Details

#aes_keyObject (readonly)

Returns the value of attribute aes_key.



11
12
13
# File 'lib/ree/licensing/decryptor.rb', line 11

def aes_key
  @aes_key
end

#client_idObject (readonly)

Returns the value of attribute client_id.



11
12
13
# File 'lib/ree/licensing/decryptor.rb', line 11

def client_id
  @client_id
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



11
12
13
# File 'lib/ree/licensing/decryptor.rb', line 11

def expires_at
  @expires_at
end

#ivObject (readonly)

Returns the value of attribute iv.



11
12
13
# File 'lib/ree/licensing/decryptor.rb', line 11

def iv
  @iv
end

Class Method Details

.load_license(license_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ree/licensing/decryptor.rb', line 20

def self.load_license(license_path)
  unless File.exist?(license_path)
    raise Ree::Error.new("License file not found: #{license_path}")
  end

  license_data = JSON.parse(File.read(license_path))
  public_key_pem = license_data['public_key_pem']
  encrypted_payload = Base64.strict_decode64(license_data['encrypted_payload'])

  payload_json = Encryptor.rsa_public_decrypt(encrypted_payload, public_key_pem)
  payload = JSON.parse(payload_json)

  expires_at = Date.parse(payload['expires_at'])

  if expires_at < Date.today
    raise Ree::Error.new("License expired on #{payload['expires_at']}")
  end

  aes_key = [payload['aes_key_hex']].pack('H*')
  iv = [payload['iv_hex']].pack('H*')

  new(
    aes_key: aes_key,
    iv: iv,
    expires_at: expires_at,
    client_id: payload['client_id']
  )
end

Instance Method Details

#decrypt_file(encrypted_data) ⇒ Object



49
50
51
# File 'lib/ree/licensing/decryptor.rb', line 49

def decrypt_file(encrypted_data)
  Encryptor.aes_decrypt(encrypted_data, @aes_key, @iv)
end