Commit e9eac006 by lanrion

refactor storage

parent 1c7e65c2
...@@ -11,7 +11,7 @@ module WeixinAuthorize ...@@ -11,7 +11,7 @@ module WeixinAuthorize
client.expired_at <= Time.now.to_i client.expired_at <= Time.now.to_i
end end
def authenticate def refresh_token
super super
end end
......
...@@ -12,7 +12,7 @@ module WeixinAuthorize ...@@ -12,7 +12,7 @@ module WeixinAuthorize
weixin_redis.hvals(client.redis_key).empty? weixin_redis.hvals(client.redis_key).empty?
end end
def authenticate def refresh_token
super super
weixin_redis.hmset(client.redis_key, :access_token, client.access_token, weixin_redis.hmset(client.redis_key, :access_token, client.access_token,
:expired_at, client.expired_at) :expired_at, client.expired_at)
......
...@@ -18,25 +18,29 @@ module WeixinAuthorize ...@@ -18,25 +18,29 @@ module WeixinAuthorize
end end
def valid? def valid?
valid["valid"]
end
def valid
valid_result = http_get_access_token valid_result = http_get_access_token
if valid_result.code == OK_CODE if valid_result.code == OK_CODE
set_access_token_for_client(valid_result.result) set_access_token_for_client(valid_result.result)
return true return {"valid" => true, "handler" => valid_result}
end end
false {"valid" => false, "handler" => valid_result}
end end
def token_expired? def token_expired?
raise NotImplementedError, "Subclasses must implement a token_expired? method" raise NotImplementedError, "Subclasses must implement a token_expired? method"
end end
def authenticate def refresh_token
raise "APPID or APPSECRET is invalid" if !valid? handle_valid_exception
set_access_token_for_client set_access_token_for_client
end end
def access_token def access_token
authenticate if token_expired? refresh_token if token_expired?
end end
def set_access_token_for_client(access_token_infos=nil) def set_access_token_for_client(access_token_infos=nil)
...@@ -53,6 +57,16 @@ module WeixinAuthorize ...@@ -53,6 +57,16 @@ module WeixinAuthorize
{grant_type: "client_credential", appid: client.app_id, secret: client.app_secret} {grant_type: "client_credential", appid: client.app_id, secret: client.app_secret}
end end
private
def handle_valid_exception
valid_result = valid
if !valid_result["valid"]
result_handler = valid_result["handler"]
raise ValidAccessTokenException, "#{result_handler.code}:#{result_handler.en_msg}(#{result_handler.cn_msg})"
end
end
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