wework 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/wework/api/agent.rb +3 -8
- data/lib/wework/api/base.rb +20 -8
- data/lib/wework/api/contact.rb +27 -0
- data/lib/wework/cipher.rb +71 -0
- data/lib/wework/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2edfd8447ee2705141dc3e3b9f794ef762e0a52e
|
4
|
+
data.tar.gz: d24518f8cd4da0e921b3b4c0ad73894872569845
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce2c7829cbdedd5aab33764a7285820e169ed9ba299a4b7710f2a3199e2e76c48536a12a635ac4e31a73f22a39c5a52c37c2c55f9401665af839a41dea78b8cf
|
7
|
+
data.tar.gz: 29d325d1cd6aba6e2d9b2715f1b402e4e5c703a296db776d06f89327df17f57fc9c25844d8364a4f50e41f76ffe767eb51700e1e9c784a61a9f8859bc1892ca8
|
data/README.md
CHANGED
@@ -2,6 +2,23 @@
|
|
2
2
|
|
3
3
|
Wework is a ruby API wrapper for work wechat.
|
4
4
|
|
5
|
+
## Version 0.1.4
|
6
|
+
* 异步任务接口 [doc](https://work.weixin.qq.com/api/doc#10138)
|
7
|
+
|
8
|
+
## Version 0.1.3
|
9
|
+
|
10
|
+
* `access_token` 和 `jsapi_ticket` 管理存储 (默认 redis, 支持其它扩展)
|
11
|
+
* 通信录相关接口 [Wework::Api::Contact](https://github.com/mycolorway/wework/blob/master/lib/wework/api/contact.rb)
|
12
|
+
* 企业微信应用接口 [Wework::Api::Agent](https://github.com/mycolorway/wework/blob/master/lib/wework/api/agent.rb)
|
13
|
+
* API 集成 [Wework::Engine](https://github.com/mycolorway/wework/blob/master/lib/wework/engine.rb)
|
14
|
+
* 网页授权及[JS-SDK签名算法](https://work.weixin.qq.com/api/doc#10029/附录1-JS-SDK使用权限签名算法)
|
15
|
+
|
16
|
+
### TODO:
|
17
|
+
|
18
|
+
* 完善测试用例
|
19
|
+
* 消息接收 [doc](https://work.weixin.qq.com/api/doc#10427)
|
20
|
+
|
21
|
+
|
5
22
|
## Installation
|
6
23
|
|
7
24
|
Add this line to your application's Gemfile:
|
data/lib/wework/api/agent.rb
CHANGED
@@ -44,14 +44,6 @@ module Wework
|
|
44
44
|
get 'menu/delete', params: {agentid: agent_id}
|
45
45
|
end
|
46
46
|
|
47
|
-
def media_upload type, file
|
48
|
-
post_file 'media/upload', file, params: { type: type }
|
49
|
-
end
|
50
|
-
|
51
|
-
def media_get(media_id)
|
52
|
-
get 'media/get', params: { media_id: media_id }, as: :file
|
53
|
-
end
|
54
|
-
|
55
47
|
def message_send user_ids, department_ids, payload={}
|
56
48
|
payload[:agentid] = agent_id
|
57
49
|
payload[:touser] = Array.wrap(user_ids).join('|') if user_ids.present?
|
@@ -87,9 +79,12 @@ module Wework
|
|
87
79
|
message_send user_ids, department_ids, {news: {articles: news}, msgtype: 'news'}
|
88
80
|
end
|
89
81
|
|
82
|
+
private
|
83
|
+
|
90
84
|
def agent_id
|
91
85
|
@app_id.to_i
|
92
86
|
end
|
87
|
+
|
93
88
|
end
|
94
89
|
end
|
95
90
|
end
|
data/lib/wework/api/base.rb
CHANGED
@@ -7,6 +7,7 @@ require 'wework/js_ticket/redis_store'
|
|
7
7
|
module Wework
|
8
8
|
module Api
|
9
9
|
class Base
|
10
|
+
include Wework::Cipher
|
10
11
|
|
11
12
|
attr_reader :corp_id, :app_id, :app_secret
|
12
13
|
attr_accessor :options
|
@@ -21,14 +22,6 @@ module Wework
|
|
21
22
|
@options = options
|
22
23
|
end
|
23
24
|
|
24
|
-
def token_store
|
25
|
-
@token_store ||= Token::RedisStore.new self
|
26
|
-
end
|
27
|
-
|
28
|
-
def jsticket_store
|
29
|
-
@jsticket_store ||= JsTicket::RedisStore.new self
|
30
|
-
end
|
31
|
-
|
32
25
|
def request
|
33
26
|
@request ||= Wework::Request.new(API_ENDPOINT, false)
|
34
27
|
end
|
@@ -51,6 +44,17 @@ module Wework
|
|
51
44
|
end
|
52
45
|
end
|
53
46
|
|
47
|
+
# public API
|
48
|
+
def media_upload type, file
|
49
|
+
post_file 'media/upload', file, params: { type: type }
|
50
|
+
end
|
51
|
+
|
52
|
+
def media_get(media_id)
|
53
|
+
get 'media/get', params: { media_id: media_id }, as: :file
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
54
58
|
def with_access_token(params = {}, tries = 2)
|
55
59
|
params ||= {}
|
56
60
|
yield(params.merge(access_token: access_token))
|
@@ -58,6 +62,14 @@ module Wework
|
|
58
62
|
token_store.refresh_token
|
59
63
|
retry unless (tries -= 1).zero?
|
60
64
|
end
|
65
|
+
|
66
|
+
def token_store
|
67
|
+
@token_store ||= Token::RedisStore.new self
|
68
|
+
end
|
69
|
+
|
70
|
+
def jsticket_store
|
71
|
+
@jsticket_store ||= JsTicket::RedisStore.new self
|
72
|
+
end
|
61
73
|
end
|
62
74
|
end
|
63
75
|
end
|
data/lib/wework/api/contact.rb
CHANGED
@@ -49,6 +49,33 @@ module Wework
|
|
49
49
|
def department_list department_id=0
|
50
50
|
get 'department/list', params: {id: department_id}
|
51
51
|
end
|
52
|
+
|
53
|
+
def batch_syncuser media_id, callback_url=nil, token=nil, encodingaeskey=nil
|
54
|
+
post 'batch/syncuser', batch_params(media_id, callback_url, token, encodingaeskey)
|
55
|
+
end
|
56
|
+
|
57
|
+
def batch_replaceuser media_id, callback_url=nil, token=nil, encodingaeskey=nil
|
58
|
+
post 'batch/replaceuser', batch_params(media_id, callback_url, token, encodingaeskey)
|
59
|
+
end
|
60
|
+
|
61
|
+
def batch_replaceparty media_id, callback_url=nil, token=nil, encodingaeskey=nil
|
62
|
+
post 'batch/replaceparty', batch_params(media_id, callback_url, token, encodingaeskey)
|
63
|
+
end
|
64
|
+
|
65
|
+
def batch_getresult job_id
|
66
|
+
get 'batch/getresult', params: {jobid: job_id}
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def batch_params media_id, callback_url, token, encodingaeskey
|
72
|
+
params = {media_id: media_id}
|
73
|
+
if callback_url.present? && token.present? && encodingaeskey.present?
|
74
|
+
params[:callback] = {url: callback_url, token: token, encodingaeskey: encodingaeskey}
|
75
|
+
end
|
76
|
+
|
77
|
+
params
|
78
|
+
end
|
52
79
|
end
|
53
80
|
end
|
54
81
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'openssl/cipher'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Wework
|
5
|
+
module Cipher
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
BLOCK_SIZE = 32
|
9
|
+
CIPHER = 'AES-256-CBC'.freeze
|
10
|
+
|
11
|
+
def encrypt(plain, encoding_aes_key)
|
12
|
+
cipher = OpenSSL::Cipher.new(CIPHER)
|
13
|
+
cipher.encrypt
|
14
|
+
|
15
|
+
cipher.padding = 0
|
16
|
+
key_data = Base64.decode64(encoding_aes_key + '=')
|
17
|
+
cipher.key = key_data
|
18
|
+
cipher.iv = key_data[0..16]
|
19
|
+
|
20
|
+
cipher.update(plain) + cipher.final
|
21
|
+
end
|
22
|
+
|
23
|
+
def decrypt(msg, encoding_aes_key)
|
24
|
+
cipher = OpenSSL::Cipher.new(CIPHER)
|
25
|
+
cipher.decrypt
|
26
|
+
|
27
|
+
cipher.padding = 0
|
28
|
+
key_data = Base64.decode64(encoding_aes_key + '=')
|
29
|
+
cipher.key = key_data
|
30
|
+
cipher.iv = key_data[0..16]
|
31
|
+
|
32
|
+
plain = cipher.update(msg) + cipher.final
|
33
|
+
decode_padding(plain)
|
34
|
+
end
|
35
|
+
|
36
|
+
# app_id or corp_id
|
37
|
+
def pack(content, app_id)
|
38
|
+
random = SecureRandom.hex(8)
|
39
|
+
text = content.force_encoding('ASCII-8BIT')
|
40
|
+
msg_len = [text.length].pack('N')
|
41
|
+
|
42
|
+
encode_padding("#{random}#{msg_len}#{text}#{app_id}")
|
43
|
+
end
|
44
|
+
|
45
|
+
def unpack(msg)
|
46
|
+
msg = decode_padding(msg)
|
47
|
+
msg_len = msg[16, 4].reverse.unpack('V')[0]
|
48
|
+
content = msg[20, msg_len]
|
49
|
+
app_id = msg[(20 + msg_len)..-1]
|
50
|
+
|
51
|
+
[content, app_id]
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def encode_padding(data)
|
57
|
+
length = data.bytes.length
|
58
|
+
amount_to_pad = BLOCK_SIZE - (length % BLOCK_SIZE)
|
59
|
+
amount_to_pad = BLOCK_SIZE if amount_to_pad == 0
|
60
|
+
padding = ([amount_to_pad].pack('c') * amount_to_pad)
|
61
|
+
data + padding
|
62
|
+
end
|
63
|
+
|
64
|
+
def decode_padding(plain)
|
65
|
+
pad = plain.bytes[-1]
|
66
|
+
# no padding
|
67
|
+
pad = 0 if pad < 1 || pad > BLOCK_SIZE
|
68
|
+
plain[0...(plain.length - pad)]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/wework/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- seandong
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/wework/api/agent.rb
|
121
121
|
- lib/wework/api/base.rb
|
122
122
|
- lib/wework/api/contact.rb
|
123
|
+
- lib/wework/cipher.rb
|
123
124
|
- lib/wework/config.rb
|
124
125
|
- lib/wework/engine.rb
|
125
126
|
- lib/wework/js_ticket/redis_store.rb
|