wework 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da08105f38235bd4f06667e179d9c033a82146f1
4
- data.tar.gz: cb7194f21cbb79a9c1ffa8e88e8e7362a167b663
3
+ metadata.gz: b1665ffec3b44a0c13996c34dbe527cf8003f723
4
+ data.tar.gz: 86e3e8182349a38cb70e9dd5f10645ebce9dc2cd
5
5
  SHA512:
6
- metadata.gz: 7ca30a8f14e08e9a5e47a22e7bee4235da1849cbbae5044ea81a1385458880a1c6470f5428b66d47d912caa240639995f4a0e21a9ba28dba629870b837e1defb
7
- data.tar.gz: 720dc74a741a7e828c577326aafe2f76764bda9077efc2dcd04315e8b627599dae4444365099dad454794412cfcc86303d4af99e572ce3ec0fcc95c7222aa630
6
+ metadata.gz: d2b5df0b9ee2fd1447a2c2e9c83101cbe29a49a49bdb18aa318bf375c41cad7d915beb8642fcecfc473e34a3d992b2e1e28d4a334905d21a9e824006e99119f7
7
+ data.tar.gz: e28aabdd329db56fd5132b5da3ebd7053f8e171ccb9a35ab03b35d5076e20c2584319a47570eb87834e588a4a0d47a6fb604c07ab84a01a86e04467efe9c5e29
@@ -1,8 +1,33 @@
1
+ require "erb"
2
+
1
3
  module Wework
2
4
  module Api
3
5
  class Agent < Base
4
6
  # user agent: UA is mozilla/5.0 (iphone; cpu iphone os 10_2 like mac os x) applewebkit/602.3.12 (khtml, like gecko) mobile/14c92 wxwork/1.3.2 micromessenger/6.2
5
7
 
8
+ def authorize_url(redirect_uri, scope="snsapi_base", state="wxwork")
9
+ uri = ERB::Util.url_encode(redirect_uri)
10
+ "#{AUTHORIZE_ENDPOINT}?appid=#{corp_id}&redirect_uri=#{uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect"
11
+ end
12
+
13
+ def get_oauth_userinfo code
14
+ get 'user/getuserinfo', params: {code: code}
15
+ end
16
+
17
+ def get_jssign_package url
18
+ timestamp = Time.now.to_i
19
+ noncestr = SecureRandom.hex(8)
20
+ str = "jsapi_ticket=#{jsapi_ticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}"
21
+ {
22
+ "appId" => app_id,
23
+ "nonceStr" => noncestr,
24
+ "timestamp" => timestamp,
25
+ "url" => url,
26
+ "signature" => Digest::SHA1.hexdigest(str),
27
+ "rawString" => str
28
+ }
29
+ end
30
+
6
31
  def get_info
7
32
  get 'agent/get', params: {agentid: agent_id}
8
33
  end
@@ -63,7 +88,7 @@ module Wework
63
88
  end
64
89
 
65
90
  def agent_id
66
- @agent_id.to_i
91
+ @app_id.to_i
67
92
  end
68
93
  end
69
94
  end
@@ -1,18 +1,23 @@
1
1
  require 'wework/token/store'
2
2
  require 'wework/token/redis_store'
3
3
 
4
+ require 'wework/js_ticket/store'
5
+ require 'wework/js_ticket/redis_store'
6
+
4
7
  module Wework
5
8
  module Api
6
9
  class Base
7
- attr_reader :corp_id, :agent_id, :agent_secret
10
+
11
+ attr_reader :corp_id, :app_id, :app_secret
8
12
  attr_accessor :options
9
13
 
10
14
  delegate :access_token, to: :token_store
15
+ delegate :jsapi_ticket, to: :jsticket_store
11
16
 
12
- def initialize(corp_id, agent_id, agent_secret, options={})
17
+ def initialize(corp_id, app_id, app_secret, options={})
13
18
  @corp_id = corp_id
14
- @agent_id = agent_id
15
- @agent_secret = agent_secret
19
+ @app_id = app_id
20
+ @app_secret = app_secret
16
21
  @options = options
17
22
  end
18
23
 
@@ -20,6 +25,10 @@ module Wework
20
25
  @token_store ||= Token::RedisStore.new self
21
26
  end
22
27
 
28
+ def jsticket_store
29
+ @jsticket_store ||= JsTicket::RedisStore.new self
30
+ end
31
+
23
32
  def request
24
33
  @request ||= Wework::Request.new(API_ENDPOINT, false)
25
34
  end
@@ -0,0 +1,39 @@
1
+ module Wework
2
+ module JsTicket
3
+ class RedisStore < Store
4
+
5
+ def initialize(agent)
6
+ raise RedisNotConfigException if redis.nil?
7
+ super
8
+ end
9
+
10
+ def jsapi_ticket
11
+ super
12
+ redis.hget(key, "jsapi_ticket")
13
+ end
14
+
15
+ def expired?
16
+ redis.hvals(key).empty?
17
+ end
18
+
19
+ def refresh_token
20
+ result = super
21
+ expires_at = Time.now.to_i + result['expires_in'].to_i - 100
22
+ redis.hmset(
23
+ key,
24
+ "jsapi_ticket", result['ticket'],
25
+ "expires_at", expires_at
26
+ )
27
+
28
+ redis.expireat(key, expires_at)
29
+ end
30
+
31
+ private
32
+
33
+ def redis
34
+ Wework.redis
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ module Wework
2
+ module JsTicket
3
+ class Store
4
+
5
+ attr_accessor :app
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def jsapi_ticket
12
+ refresh_token if expired?
13
+ end
14
+
15
+ def expired?
16
+ raise NotImplementedError, "Subclasses must implement a token_expired? method"
17
+ end
18
+
19
+ def refresh_token
20
+ app.get 'get_jsapi_ticket'
21
+ end
22
+
23
+ private
24
+
25
+ def key
26
+ @key ||= Digest::MD5.hexdigest("#{JSAPI_TOKEN_PREFIX}_#{app.app_id}_#{app.app_secret}")
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -2,10 +2,10 @@ module Wework
2
2
  module Token
3
3
  class Store
4
4
 
5
- attr_accessor :agent
5
+ attr_accessor :app
6
6
 
7
- def initialize(agent)
8
- @agent = agent
7
+ def initialize(app)
8
+ @app = app
9
9
  end
10
10
 
11
11
  def access_token
@@ -17,13 +17,13 @@ module Wework
17
17
  end
18
18
 
19
19
  def refresh_token
20
- agent.request.get 'gettoken', params: {corpid: agent.corp_id, corpsecret: agent.agent_secret}
20
+ app.request.get 'gettoken', params: {corpid: app.corp_id, corpsecret: app.app_secret}
21
21
  end
22
22
 
23
23
  private
24
24
 
25
25
  def key
26
- @key ||= Digest::MD5.hexdigest("#{ACCESS_TOKEN_PREFIX}_#{agent.agent_id}_#{agent.agent_secret}")
26
+ @key ||= Digest::MD5.hexdigest("#{ACCESS_TOKEN_PREFIX}_#{app.app_id}_#{app.app_secret}")
27
27
  end
28
28
 
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module Wework
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/lib/wework.rb CHANGED
@@ -12,7 +12,9 @@ require 'wework/api/contact'
12
12
 
13
13
  module Wework
14
14
  API_ENDPOINT = 'https://qyapi.weixin.qq.com/cgi-bin/'.freeze
15
- ACCESS_TOKEN_PREFIX = 'WEWORK'.freeze
15
+ AUTHORIZE_ENDPOINT = 'https://open.weixin.qq.com/connect/oauth2/authorize'.freeze
16
+ ACCESS_TOKEN_PREFIX = 'TOKEN'.freeze
17
+ JSAPI_TOKEN_PREFIX = 'JST'.freeze
16
18
  CONTACT_AGENT_ID = 'CONTACT'.freeze
17
19
  HTTP_OK_STATUS = [200, 201].freeze
18
20
  SUCCESS_CODE = 0
data/wework.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["sindon@gmail.com"]
11
11
 
12
12
  spec.summary = spec.description = %q{Ruby API wrapper for work wechat.}
13
- spec.homepage = "https://github.com/seandong/wework"
13
+ spec.homepage = "https://github.com/mycolorway/wework"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
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.2
4
+ version: 0.1.3
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-19 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -122,12 +122,14 @@ files:
122
122
  - lib/wework/api/contact.rb
123
123
  - lib/wework/config.rb
124
124
  - lib/wework/engine.rb
125
+ - lib/wework/js_ticket/redis_store.rb
126
+ - lib/wework/js_ticket/store.rb
125
127
  - lib/wework/request.rb
126
128
  - lib/wework/token/redis_store.rb
127
129
  - lib/wework/token/store.rb
128
130
  - lib/wework/version.rb
129
131
  - wework.gemspec
130
- homepage: https://github.com/seandong/wework
132
+ homepage: https://github.com/mycolorway/wework
131
133
  licenses:
132
134
  - MIT
133
135
  metadata: {}