Class: Wix::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/wix/instance.rb

Constant Summary collapse

MISSING_WIX_SIGNED_INSTANCE =
"You must pass a wix_signed_instance value to parse"
INVALID_WIX_SIGNED_INSTANCE =
"The wix_signed_instance you passed is invalid"
INVALID_WIX_SIGNATURE =
"The signature from the wix_signed_instance you passed does not match your signature"

Class Method Summary collapse

Class Method Details

.needs_base64_padding(encoded_json) ⇒ Object



34
35
36
37
38
39
# File 'lib/wix/instance.rb', line 34

def self.needs_base64_padding(encoded_json)
  Base64.strict_decode64(encoded_json)
  false
rescue
  true 
end

.parse(wix_signed_instance) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wix/instance.rb', line 7

def self.parse(wix_signed_instance)
  wix_signed_instance = wix_signed_instance.to_s.strip
  raise WixError.new(MISSING_WIX_SIGNED_INSTANCE) if wix_signed_instance.empty?

  split_wix_signed_instance = wix_signed_instance.split('.')
  raise WixError.new(INVALID_WIX_SIGNED_INSTANCE) if split_wix_signed_instance.length != 2

  signature     = split_wix_signed_instance[0]
  encoded_json  = split_wix_signed_instance[1]
  raise WixError.new(INVALID_WIX_SIGNATURE) if !valid_signature(signature, encoded_json)

  encoded_json += '=' * (4 - encoded_json.length.modulo(4)) if needs_base64_padding(encoded_json)
  json_string      = Base64.decode64(encoded_json)
  hash             = JSON.parse(json_string)

  RecursiveOpenStruct.new(hash, :recurse_over_arrays => true)
rescue => error
  raise WixError.new(error.message)
end

.valid_signature(signature, encoded_json) ⇒ Object



27
28
29
30
31
32
# File 'lib/wix/instance.rb', line 27

def self.valid_signature(signature, encoded_json)
  hmac                    = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, Wix.app_secret_key, encoded_json)
  my_signature            = Base64.urlsafe_encode64(hmac).gsub('=','')

  signature == my_signature
end