Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weixin_authorize
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
weixin_authorize
Commits
94e46abe
Commit
94e46abe
authored
Mar 31, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor with adapter storage
parent
993cee97
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
36 deletions
+82
-36
lib/weixin_authorize/adapter/client_storage.rb
+23
-0
lib/weixin_authorize/adapter/redis_storage.rb
+34
-0
lib/weixin_authorize/adapter/storage.rb
+25
-36
No files found.
lib/weixin_authorize/adapter/client_storage.rb
0 → 100644
View file @
94e46abe
# encoding: utf-8
module
WeixinAuthorize
class
ClientStorage
<
Storage
def
valid?
super
end
def
token_expired?
# 如果当前token过期时间小于现在的时间,则重新获取一次
client
.
expired_at
<=
Time
.
now
.
to_i
end
def
authenticate
super
end
def
access_token
super
client
.
access_token
end
end
end
lib/weixin_authorize/adapter/redis_storage.rb
0 → 100644
View file @
94e46abe
# encoding: utf-8
module
WeixinAuthorize
class
RedisStorage
<
Storage
def
valid?
weixin_redis
.
del
(
client
.
redis_key
)
super
end
def
token_expired?
weixin_redis
.
hvals
(
client
.
redis_key
).
empty?
end
def
authenticate
super
weixin_redis
.
hmset
(
client
.
redis_key
,
:access_token
,
client
.
access_token
,
:expired_at
,
client
.
expired_at
)
weixin_redis
.
expireat
(
client
.
redis_key
,
client
.
expired_at
.
to_i
-
10
)
# 提前10秒超时
end
def
access_token
super
client
.
access_token
=
weixin_redis
.
hget
(
client
.
redis_key
,
"access_token"
)
client
.
expired_at
=
weixin_redis
.
hget
(
client
.
redis_key
,
"expired_at"
)
client
.
access_token
end
def
weixin_redis
WeixinAuthorize
.
config
.
redis
end
end
end
lib/weixin_authorize/adapter/storage.rb
View file @
94e46abe
...
...
@@ -3,22 +3,27 @@ module WeixinAuthorize
class
Storage
attr_accessor
:
storage
attr_accessor
:
client
def
initialize
(
storage
)
@
storage
=
storage
def
initialize
(
client
)
@
client
=
client
end
def
self
.
init_with
(
storage
)
if
storage
.
is_a?
Client
ObjectStorage
.
new
(
storage
)
def
self
.
init_with
(
client
)
if
WeixinAuthorize
.
config
.
redis
.
nil?
ClientStorage
.
new
(
client
)
else
RedisStorage
.
new
(
storage
)
RedisStorage
.
new
(
client
)
end
end
def
valid?
raise
NotImplementedError
,
"Subclasses must implement a valid? method"
valid_result
=
http_get_access_token
if
valid_result
.
keys
.
include?
(
"access_token"
)
set_access_token_for_client
(
valid_result
)
return
true
end
false
end
def
token_expired?
...
...
@@ -26,44 +31,28 @@ module WeixinAuthorize
end
def
authenticate
raise
NotImplementedError
,
"Subclasses must implement a authenticate method"
raise
"APPID or APPSECRET is invalid"
if
!
valid?
set_access_token_for_client
end
def
get_
access_token
def
access_token
authenticate
if
token_expired?
end
end
class
RedisStorage
<
Storage
def
valid?
storage
.
del
(
storage
.
redis_key
)
def
set_access_token_for_client
(
access_token_infos
=
nil
)
token_infos
=
access_token_infos
||
http_get_access_token
client
.
access_token
=
token_infos
[
"access_token"
]
client
.
expired_at
=
Time
.
now
.
to_i
+
token_infos
[
"expires_in"
].
to_i
end
def
token_expired?
storage
.
hvals
(
redis_key
).
empty?
def
http_get_access_token
WeixinAuthorize
.
http_get_without_token
(
"/token"
,
authenticate_headers
)
end
def
authenticate
def
authenticate
_headers
{
grant_type:
"client_credential"
,
appid:
client
.
app_id
,
secret:
client
.
app_secret
}
end
end
class
ObjectStorage
<
Storage
def
valid?
end
def
token_expired?
# 如果当前token过期时间小于现在的时间,则重新获取一次
stoage
.
expired_at
<=
Time
.
now
.
to_i
end
def
authenticate
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment