class Haoyaoshi::Token::Store

Attributes

client[RW]

Public Class Methods

init_with(client) click to toggle source
# File lib/haoyaoshi/token/store.rb, line 12
def self.init_with(client)
  if Haoyaoshi.haoyaoshi_redis.nil?
    ObjectStore.new(client)
  else
    RedisStore.new(client)
  end
end
new(client) click to toggle source
# File lib/haoyaoshi/token/store.rb, line 8
def initialize(client)
  @client = client
end

Public Instance Methods

access_token() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 39
def access_token
  refresh_token if token_expired?
end
authenticate() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 24
def authenticate
  auth_result = http_get_access_token
  auth = false
  if auth_result.is_ok?
    set_access_token(auth_result.result)
    auth = true
  end
  {"valid" => auth, "handler" => auth_result}
end
authenticate_headers() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 58
def authenticate_headers
  {grant_type: client.grant_type, client_id: client.client_id, client_secret: client.client_secret, scope: client.scope}
end
http_get_access_token() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 54
def http_get_access_token
  Haoyaoshi.http_post_without_token("/auth/v1/oauth2/token", authenticate_headers,authenticate_headers)
end
refresh_token() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 34
def refresh_token
  handle_valid_exception
  set_access_token
end
set_access_token(access_token_infos=nil) click to toggle source
# File lib/haoyaoshi/token/store.rb, line 47
def set_access_token(access_token_infos=nil)
  token_infos = access_token_infos || http_get_access_token.result
  client.access_token = token_infos["access_token"]
  client.token_type = token_infos["token_type"]
  client.expired_at = Haoyaoshi.calculate_expire(token_infos["expires_in"])
end
token_expired?() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 43
def token_expired?
  raise NotImplementedError, "Subclasses must implement a token_expired? method"
end
valid?() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 20
def valid?
  authenticate["valid"]
end

Private Instance Methods

handle_valid_exception() click to toggle source
# File lib/haoyaoshi/token/store.rb, line 64
def handle_valid_exception
  auth_result = authenticate
  if !auth_result["valid"]
    result_handler = auth_result["handler"]
    raise ValidAccessTokenException, result_handler.full_error_message
  end
end