Class: Gamewisp::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/gamewisp/authorizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, store) ⇒ Authorizer

Only works if this class is derived (includes) HTTParty

if ENV

debug_output $stdout

end



32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#authorize_pathObject

Returns the value of attribute authorize_path.



18
19
20
# File 'lib/gamewisp/authorizer.rb', line 18

def authorize_path
  @authorize_path
end

#gamewisp_urlObject

Returns the value of attribute gamewisp_url.



17
18
19
# File 'lib/gamewisp/authorizer.rb', line 17

def gamewisp_url
  @gamewisp_url
end

#hostObject

Returns the value of attribute host.



20
21
22
# File 'lib/gamewisp/authorizer.rb', line 20

def host
  @host
end

#portObject

Returns the value of attribute port.



21
22
23
# File 'lib/gamewisp/authorizer.rb', line 21

def port
  @port
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



19
20
21
# File 'lib/gamewisp/authorizer.rb', line 19

def redirect_uri
  @redirect_uri
end

#scopesObject

Returns the value of attribute scopes.



22
23
24
# File 'lib/gamewisp/authorizer.rb', line 22

def scopes
  @scopes
end

#stateObject

Returns the value of attribute state.



23
24
25
# File 'lib/gamewisp/authorizer.rb', line 23

def state
  @state
end

#token_storeObject

Returns the value of attribute token_store.



16
17
18
# File 'lib/gamewisp/authorizer.rb', line 16

def token_store
  @token_store
end

Instance Method Details

#app_authorization_urlObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_instanceObject



63
64
65
# File 'lib/gamewisp/authorizer.rb', line 63

def create_server_instance
  return Server.new(port, state)
end

#get_new_tokens_using_auth_code(code) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# 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