class Gamewisp::Authorizer

Attributes

authorize_path[RW]
gamewisp_url[RW]
host[RW]
port[RW]
redirect_uri[RW]
scopes[RW]
state[RW]
token_store[RW]

Public Class Methods

new(state, store) click to toggle source
Only works if this class is derived (includes) HTTParty

if ENV

debug_output $stdout

end

# File lib/gamewisp/authorizer.rb, line 32
def initialize state, store
  self.token_store = store

  self.gamewisp_url = 'api.gamewisp.com'
  self.authorize_path = "/pub/v1/oauth/authorize"
  self.host = token_store.endpoint_host
  self.port = token_store.endpoint_port
  self.redirect_uri = "http://#{host}:#{port}"

  self.scopes = "read_only,subscriber_read_full,user_read"
  self.state = state
end

Public Instance Methods

app_authorization_url() click to toggle source
# File lib/gamewisp/authorizer.rb, line 45
def app_authorization_url
  params = {
    response_type: "code",
    client_id: token_store.client_id,
    redirect_uri: redirect_uri,
    scope: scopes,
    state: state,
  }

  url = {
    host: gamewisp_url,
    path: authorize_path,
    query: URI.encode_www_form(params)
  }

  return URI::HTTPS.build(url)
end
create_server_instance() click to toggle source
# File lib/gamewisp/authorizer.rb, line 63
def create_server_instance
  return Server.new(port, state)
end
get_new_tokens_using_auth_code(code) click to toggle source
# File lib/gamewisp/authorizer.rb, line 91
def get_new_tokens_using_auth_code code
  url = "https://#{gamewisp_url}/pub/v1/oauth/token"

  response = HTTParty.post(url,
              :query => {
                :grant_type => 'authorization_code',
                :client_id => token_store.client_id,
                :client_secret => token_store.client_secret,
                :redirect_uri => redirect_uri,
                :code => code,
                :state => state,
              })
  return {:error => "#{response.code}: authorization error"} if response.code == 401

  return response
end
get_new_tokens_using_refresh_token(token) click to toggle source
# File lib/gamewisp/authorizer.rb, line 108
def get_new_tokens_using_refresh_token token
  url = "https://#{gamewisp_url}/pub/v1/oauth/token"

  # For some reason we have to post in the :body instead of :query (as above) or we'll
  # get an 'invalid refresh token' error response.
  #
  response = HTTParty.post(url,
              :body => {
                :grant_type => 'refresh_token',
                :client_id => token_store.client_id,
                :client_secret => token_store.client_secret,
                :redirect_uri => redirect_uri,
                :refresh_token => token,
              })

  return {:error => "#{response.code}: authorization error"} if response.code == 401

  return response
end
renew_tokens_with_auth_code(code) click to toggle source
# File lib/gamewisp/authorizer.rb, line 67
def renew_tokens_with_auth_code code
  response = get_new_tokens_using_auth_code code
  dbg("renew_tokens_with_auth_code", response)
  if response.code == 200
    token_store.save_access_token response["access_token"]
    token_store.save_refresh_token response["refresh_token"]
  else
    puts "Errors have occured during authentication code request:"
    puts response
  end
end
renew_tokens_with_refresh_token(token) click to toggle source
# File lib/gamewisp/authorizer.rb, line 79
def renew_tokens_with_refresh_token token
  response = get_new_tokens_using_refresh_token token
  dbg("renew_tokens_with_refresh_token", response)
  if response.code == 200
    token_store.save_access_token response["access_token"]
    token_store.save_refresh_token response["refresh_token"]
  else
    puts "Errors have occured during token refresh request:"
    puts response
  end
end