Commit 07b11136e1b82ce390744998da1204c73d9fec3a

Authored by Jeff Lai
1 parent 32dd5f4b
Exists in master

Add signature helper to client

Showing 1 changed file with 19 additions and 1 deletions   Show diff stats
lib/dingtalk/client.rb
@@ -7,12 +7,17 @@ module Dingtalk @@ -7,12 +7,17 @@ module Dingtalk
7 end 7 end
8 8
9 def decrypt(echo_str) 9 def decrypt(echo_str)
10 - aes_key = Base64.decode64(Dingtalk.suite_aes_key + '=')  
11 content, status = Dingtalk::Prpcrypt.decrypt(aes_key, echo_str, Dingtalk.suite_key) 10 content, status = Dingtalk::Prpcrypt.decrypt(aes_key, echo_str, Dingtalk.suite_key)
12 # TODO check status 11 # TODO check status
13 JSON.parse(content) 12 JSON.parse(content)
14 end 13 end
15 14
  15 + def signature(return_str)
  16 + encrypt = Dingtalk::Prpcrypt.encrypt(aes_key, return_str, Dingtalk.suite_key)
  17 + sort_params = [suite.suite_access_token, timestamp, nonce, encrypt].sort.join
  18 + Digest::SHA1.hexdigest(sort_params)
  19 + end
  20 +
16 def suite 21 def suite
17 Api::Suite.new 22 Api::Suite.new
18 end 23 end
@@ -20,5 +25,18 @@ module Dingtalk @@ -20,5 +25,18 @@ module Dingtalk
20 def department 25 def department
21 Api::Department.new(@corp_id) 26 Api::Department.new(@corp_id)
22 end 27 end
  28 +
  29 + private
  30 + def aes_key
  31 + Base64.decode64(Dingtalk.suite_aes_key + '=')
  32 + end
  33 +
  34 + def timestamp
  35 + Time.now.to_i.to_s
  36 + end
  37 +
  38 + def nonce
  39 + SecureRandom.hex
  40 + end
23 end 41 end
24 end 42 end