client.rb
2.07 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module Dingtalk
class Client
def initialize(corp = nil)
@corp = corp
end
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)
the_timestamp = timestamp
the_nonce = nonce
encrypted = encrypt(return_str)
{
msg_signature: signature(return_str, encrypted, the_timestamp, the_nonce),
encrypt: encrypted,
timeStamp: the_timestamp,
nonce: the_nonce
}
end
def encrypt(return_str)
encrypt = Dingtalk::Prpcrypt.encrypt(aes_key, return_str, Dingtalk.suite_key)
end
def signature(return_str, encrypted, the_timestamp, the_nonce)
sort_params = [Dingtalk.suite_token, the_timestamp, the_nonce, encrypted].sort.join
Digest::SHA1.hexdigest(sort_params)
end
def jssign_package(request_url)
return nil unless @corp
the_timestamp = timestamp
the_nonce = nonce
str = "jsapi_ticket=#{base.js_ticket}&noncestr=#{the_nonce}×tamp=#{the_timestamp}&url=#{CGI.unescape(request_url)}"
signature = Digest::SHA1.hexdigest(str)
{
js_ticket: base.js_ticket,
request_url: request_url,
corp_id: @corp.corp_id,
timeStamp: the_timestamp,
nonceStr: the_nonce,
signature: signature
}
end
def base
Api::Base.new(@corp)
end
def suite
Api::Suite.new
end
def sns
Api::Sns.new
end
def department
Api::Department.new(@corp)
end
def user
Api::User.new(@corp)
end
def attendance
Api::Attendance.new(@corp)
end
def message
Api::Message.new(@corp)
end
def micro_app
Api::MicroApp.new(@corp)
end
def call_back
Api::CallBack.new(@corp)
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