Commit 94e46abe by lanrion

refactor with adapter storage

parent 993cee97
# 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
# 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
...@@ -3,22 +3,27 @@ module WeixinAuthorize ...@@ -3,22 +3,27 @@ module WeixinAuthorize
class Storage class Storage
attr_accessor :storage attr_accessor :client
def initialize(storage) def initialize(client)
@storage = storage @client = client
end end
def self.init_with(storage) def self.init_with(client)
if storage.is_a? Client if WeixinAuthorize.config.redis.nil?
ObjectStorage.new(storage) ClientStorage.new(client)
else else
RedisStorage.new(storage) RedisStorage.new(client)
end end
end end
def valid? 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 end
def token_expired? def token_expired?
...@@ -26,44 +31,28 @@ module WeixinAuthorize ...@@ -26,44 +31,28 @@ module WeixinAuthorize
end end
def authenticate def authenticate
raise NotImplementedError, "Subclasses must implement a authenticate method" raise "APPID or APPSECRET is invalid" if !valid?
set_access_token_for_client
end end
def get_access_token def access_token
authenticate if token_expired?
end end
end
class RedisStorage < Storage def set_access_token_for_client(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token
def valid? client.access_token = token_infos["access_token"]
storage.del(storage.redis_key) client.expired_at = Time.now.to_i + token_infos["expires_in"].to_i
end end
def token_expired? def http_get_access_token
storage.hvals(redis_key).empty? WeixinAuthorize.http_get_without_token("/token", authenticate_headers)
end end
def authenticate def authenticate_headers
{grant_type: "client_credential", appid: client.app_id, secret: client.app_secret}
end end
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 end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment