Class: Wework::Provider

Inherits:
Object
  • Object
show all
Includes:
Cipher
Defined in:
lib/wework/provider.rb

Constant Summary

Constants included from Cipher

Cipher::BLOCK_SIZE, Cipher::CIPHER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cipher

#decrypt, #encrypt, #pack, #unpack

Constructor Details

#initialize(options = {}) ⇒ Provider



7
8
9
10
11
12
# File 'lib/wework/provider.rb', line 7

def initialize(options={})
  @corp_id = options[:corp_id]
  @provider_secret = options[:provider_secret]
  @encoding_aes_key = options[:encoding_aes_key]
  @token = options[:token]
end

Instance Attribute Details

#corp_idObject

Returns the value of attribute corp_id.



5
6
7
# File 'lib/wework/provider.rb', line 5

def corp_id
  @corp_id
end

#encoding_aes_keyObject

Returns the value of attribute encoding_aes_key.



5
6
7
# File 'lib/wework/provider.rb', line 5

def encoding_aes_key
  @encoding_aes_key
end

#provider_secretObject

Returns the value of attribute provider_secret.



5
6
7
# File 'lib/wework/provider.rb', line 5

def provider_secret
  @provider_secret
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/wework/provider.rb', line 5

def token
  @token
end

Instance Method Details

#generate_xml(msg, timestamp, nonce) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/wework/provider.rb', line 29

def generate_xml(msg, timestamp, nonce)
  encrypt = msg_encrypt(msg)
  {
    Encrypt: encrypt,
    MsgSignature: signature(timestamp, nonce, encrypt),
    TimeStamp: timestamp,
    Nonce: nonce
  }.to_xml(root: 'xml', children: 'item', skip_instruct: true, skip_types: true)
end

#msg_decrypt(msg) ⇒ Object



14
15
16
# File 'lib/wework/provider.rb', line 14

def msg_decrypt msg
  unpack(decrypt(Base64.decode64(msg), self.encoding_aes_key))[0]
end

#msg_encrypt(msg) ⇒ Object



18
19
20
# File 'lib/wework/provider.rb', line 18

def msg_encrypt msg
  Base64.strict_encode64(encrypt(pack(msg, self.corp_id), self.encoding_aes_key))
end

#signature(timestamp, nonce, encrypt) ⇒ Object



22
23
24
25
26
27
# File 'lib/wework/provider.rb', line 22

def signature(timestamp, nonce, encrypt)
  array = [self.token, timestamp, nonce]
  array << encrypt unless encrypt.nil?
  dev_msg_signature = array.compact.collect(&:to_s).sort.join
  Digest::SHA1.hexdigest(dev_msg_signature)
end