third_party_wxa 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 280928b0721a0434248e99da225d36754c1ccfa37871ceff0da650149aeae8a4
4
- data.tar.gz: dd5825c6eef9f208ef353abc6b2375c7c93af1875654775f3aa09cedf97521c9
3
+ metadata.gz: d9a1361eab458413e4dbb16d7e239c177be96ebcf92dc89ab016e876c88647a8
4
+ data.tar.gz: 93a74483b992a8cb255c65555eb9476e1bafc3201e5557eb8f9527403cf6b7f9
5
5
  SHA512:
6
- metadata.gz: 7ccee3ffb383e9ae8325ef9162a872d129d3cea7a95c885eef036ff5b49d31167832c5265e35c5f1b7aaa4b7c4e7910474b88b4ef5205c02542d3d304190d949
7
- data.tar.gz: cc7b50cf0f3c00f56f1069d67d63aa4a26b51f1a2876be8cbe49499103efbe59403254fe349ae3bf23ae6b34ff6c08e4de3a7a129ea4ffd24950d7a8ea575767
6
+ metadata.gz: 10bc4b22d61c06e568609d88d4d41c6f13c3ba9f1ef3e11705a1773a809d3c4b338bb01a4f662f28029d77473c8018690a57ca3edd9f105d32ec6baea536cd2c
7
+ data.tar.gz: 7674429c7029324b75cf00cbc16b38815e2ed1d382068fbeb86642441d1dadc7210ac944b5dc55c87d5cdbaa753bce1609bbe0e25d621343a6231835268af216
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- third_party_wxa (0.1.4)
4
+ third_party_wxa (0.1.5)
5
5
  json (>= 1.8.6)
6
6
  redis
7
7
  rest-client (>= 2.0)
@@ -17,12 +17,12 @@ module ThirdPartyWxa
17
17
  end
18
18
 
19
19
  #https:// api.weixin.qq.com /cgi-bin/component/api_authorizer_token?component_access_token=xxxxx
20
- def authorizer_access_token_fresh
20
+ def authorizer_access_token_fresh auth_appid, refresh_token
21
21
 
22
22
  params = {
23
23
  "component_appid": appid,
24
- "authorizer_appid": wx_redis.hget(sign, 'appid'),
25
- "authorizer_refresh_token": wx_redis.hget(sign, 'refresh_token'),
24
+ "authorizer_appid": auth_appid,
25
+ "authorizer_refresh_token": refresh_token,
26
26
  }
27
27
  http_post 'cgi-bin', 'component/api_authorizer_token', params
28
28
  end
@@ -61,42 +61,24 @@ module ThirdPartyWxa
61
61
  self
62
62
  end
63
63
 
64
+ def token_store
65
+ Token::Store.init_with(self)
66
+ end
67
+
64
68
  def authorizer_access_token_valid? sign
65
- expire_at = wx_redis.hget sign, 'expire_at'
66
- return false if wx_redis.hget(sign, 'access_token').blank? || expire_at.blank?
67
- expire_at <= Time.now.to_i
69
+ token_store.authorizer_access_token_valid sign
68
70
  end
69
71
 
70
72
  def get_authorizer_access_token sign
71
- if !authorizer_access_token_valid? # raise exception?
72
- if wx_redis.hget(sign, 'refresh_token').present?
73
- refresh_authorizer_access_token
74
- else
75
- raise 'refresh token is missing, please authorize again'
76
- end
77
- end
78
- wx_redis.hget(sign, 'access_token')
79
- end
80
-
81
- def set_authorizer_access_token sign, code
82
- res = authorizer_access_token_api code
83
- authorizer_access_token = res['authorization_info']['authorizer_access_token']
84
- authorizer_appid = res['authorization_info']['authorizer_appid']
85
- authorizer_expire_at = ThirdPartyWxa.cal_expire_at res['authorization_info']['expire_in']
86
- authorizer_refresh_token = res['authorization_info']['authorizer_refresh_token']
87
- wx_redis.hset sign, 'access_token', authorizer_access_token, 'appid', authorizer_appid,
88
- 'expire_at', authorizer_expire_at, 'refresh_token', authorizer_refresh_token
89
- authorizer_access_token
90
- end
91
-
92
- def refresh_authorizer_access_token sign
93
- res = authorizer_access_token_fresh sign
94
- authorizer_access_token = res['authorizer_access_token']
95
- authorizer_expire_at = ThirdPartyWxa.cal_expire_at res['expires_in']
96
- authorizer_refresh_token = res['authorizer_refresh_token']
97
- wx_redis.hset sign, 'access_token', authorizer_access_token,
98
- 'expire_at', authorizer_expire_at, 'refresh_token', authorizer_refresh_token
99
- authorizer_access_token
73
+ token_store.get_authorizer_access_token sign
74
+ end
75
+
76
+ def exchange_authorizer_access_token code, sign = nil
77
+ token_store.exchange_authorizer_access_token code, sign
78
+ end
79
+
80
+ def refresh_authorizer_access_token auth_appid, refresh_token
81
+ token_store.refresh_authorizer_access_token auth_appid, refresh_token
100
82
  end
101
83
 
102
84
  def http_get scope, url, url_params={}
@@ -0,0 +1,26 @@
1
+ module ThirdPartyWxa
2
+ module Token
3
+
4
+ class LocalStore < Store
5
+
6
+ def authorizer_access_token_valid *args
7
+ raise 'local_store --- please check expire_time yourself'
8
+ end
9
+
10
+ def get_authorizer_access_token *args
11
+ raise 'local_store --- get token from your local store'
12
+ end
13
+
14
+ def exchange_authorizer_access_token code, sign=nil
15
+ plugin.authorizer_access_token_api code
16
+ end
17
+
18
+ def refresh_authorizer_access_token auth_appid, refresh_token
19
+ plugin.authorizer_access_token_fresh auth_appid, refresh_tokne
20
+ end
21
+
22
+
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ module ThirdPartyWxa
2
+ module Token
3
+
4
+ class RedisStore < Store
5
+
6
+
7
+ def authorizer_access_token_valid? sign
8
+ expire_at = wx_redis.hget sign, 'expire_at'
9
+ return false if wx_redis.hget(sign, 'access_token').blank? || expire_at.blank?
10
+ expire_at <= Time.now.to_i
11
+ end
12
+
13
+ def get_authorizer_access_token sign
14
+ if !authorizer_access_token_valid? # raise exception?
15
+ if wx_redis.hget(sign, 'refresh_token').present?
16
+ auth_appid = wx_redis.hget(sign, 'appid')
17
+ refresh_token = wx_redis.hget(sign, 'refresh_token')
18
+ refresh_authorizer_access_token auth_appid, refresh_token
19
+ else
20
+ raise 'refresh token is missing, please authorize again'
21
+ end
22
+ end
23
+ wx_redis.hget(sign, 'access_token')
24
+ end
25
+
26
+ def exchange_authorizer_access_token code, sign
27
+ res = plugin.authorizer_access_token_api code
28
+ authorizer_access_token = res['authorization_info']['authorizer_access_token']
29
+ authorizer_appid = res['authorization_info']['authorizer_appid']
30
+ authorizer_expire_at = ThirdPartyWxa.cal_expire_at res['authorization_info']['expire_in']
31
+ authorizer_refresh_token = res['authorization_info']['authorizer_refresh_token']
32
+ wx_redis.hset sign, 'access_token', authorizer_access_token, 'appid', authorizer_appid,
33
+ 'expire_at', authorizer_expire_at, 'refresh_token', authorizer_refresh_token
34
+ authorizer_access_token
35
+ end
36
+
37
+ def refresh_authorizer_access_token auth_appid, refresh_tokne
38
+ res = plugin.authorizer_access_token_fresh auth_appid, refresh_tokne
39
+ authorizer_access_token = res['authorizer_access_token']
40
+ authorizer_expire_at = ThirdPartyWxa.cal_expire_at res['expires_in']
41
+ authorizer_refresh_token = res['authorizer_refresh_token']
42
+ wx_redis.hset sign, 'access_token', authorizer_access_token,
43
+ 'expire_at', authorizer_expire_at, 'refresh_token', authorizer_refresh_token
44
+ authorizer_access_token
45
+ end
46
+
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ module ThirdPartyWxa
2
+ module Token
3
+
4
+ class Store
5
+ attr_accessor :plugin
6
+
7
+ def initialize plugin
8
+ @plugin = plugin
9
+ end
10
+
11
+ def self.init_with plugin
12
+ if ThirdPartyWxa.wx_redis.nil?
13
+ LocalStore.new plugin
14
+ else
15
+ RedisStore.new plugin
16
+ end
17
+ end
18
+
19
+ def wx_redis
20
+ ThirdPartyWxa.wx_redis
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module ThirdPartyWxa
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -5,6 +5,14 @@ require 'third_party_wxa/config'
5
5
  require "third_party_wxa/api"
6
6
  require 'third_party_wxa/plugin'
7
7
  module ThirdPartyWxa
8
+
9
+ module Token
10
+ autoload(:Store, "third_party_wxa/token/store")
11
+ autoload(:LocalStore, "third_party_wxa/token/local_store")
12
+ autoload(:RedisStore, "third_party_wxa/token/redis_store")
13
+ end
14
+
15
+
8
16
  BASE = 'https://api.weixin.qq.com/'.freeze
9
17
  # scope => [cgi-bin, wxa]
10
18
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: third_party_wxa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - hzy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -104,6 +104,9 @@ files:
104
104
  - lib/third_party_wxa/api/token.rb
105
105
  - lib/third_party_wxa/config.rb
106
106
  - lib/third_party_wxa/plugin.rb
107
+ - lib/third_party_wxa/token/local_store.rb
108
+ - lib/third_party_wxa/token/redis_store.rb
109
+ - lib/third_party_wxa/token/store.rb
107
110
  - lib/third_party_wxa/version.rb
108
111
  - third_party_wxa.gemspec
109
112
  homepage: https://github.com/cs-cj/third_party_wxa