client.rb
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
54
55
56
57
58
module Dingtalk
class Client
attr_accessor :corp_id
def decrypt(echo_str)
content, status = Dingtalk::Prpcrypt.decrypt(aes_key, echo_str, Dingtalk.suite_key)
# TODO check status
JSON.parse(content)
end
def response_json(return_str)
{
msg_signature: signature(return_str),
encrypt: encrypt(return_str),
timeStamp: timestamp,
nonce: nonce
}
end
def encrypt(return_str)
encrypt = Dingtalk::Prpcrypt.encrypt(aes_key, return_str, Dingtalk.suite_key)
end
def signature(return_str)
sort_params = [suite.suite_access_token, timestamp, nonce, encrypt(return_str)].sort.join
Digest::SHA1.hexdigest(sort_params)
end
def base
Api::Base.new(@corp_id)
end
def suite
Api::Suite.new
end
def department
Api::Department.new(@corp_id)
end
def user
Api::User.new(@corp_id)
end
private
def aes_key
Base64.decode64(Dingtalk.suite_aes_key + '=')
end
def timestamp
Time.now.to_i.to_s
end
def nonce
SecureRandom.hex
end
end
end