Class: Gamewisp::Authorizer
- Inherits:
-
Object
- Object
- Gamewisp::Authorizer
- Defined in:
- lib/gamewisp/authorizer.rb
Instance Attribute Summary collapse
-
#authorize_path ⇒ Object
Returns the value of attribute authorize_path.
-
#gamewisp_url ⇒ Object
Returns the value of attribute gamewisp_url.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
-
#state ⇒ Object
Returns the value of attribute state.
-
#token_store ⇒ Object
Returns the value of attribute token_store.
Instance Method Summary collapse
- #app_authorization_url ⇒ Object
- #create_server_instance ⇒ Object
- #get_new_tokens_using_auth_code(code) ⇒ Object
- #get_new_tokens_using_refresh_token(token) ⇒ Object
-
#initialize(state, store) ⇒ Authorizer
constructor
Only works if this class is derived (includes) HTTParty.
- #renew_tokens_with_auth_code(code) ⇒ Object
- #renew_tokens_with_refresh_token(token) ⇒ Object
Constructor Details
#initialize(state, store) ⇒ Authorizer
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. = "/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_path ⇒ Object
Returns the value of attribute authorize_path.
18 19 20 |
# File 'lib/gamewisp/authorizer.rb', line 18 def @authorize_path end |
#gamewisp_url ⇒ Object
Returns the value of attribute gamewisp_url.
17 18 19 |
# File 'lib/gamewisp/authorizer.rb', line 17 def gamewisp_url @gamewisp_url end |
#host ⇒ Object
Returns the value of attribute host.
20 21 22 |
# File 'lib/gamewisp/authorizer.rb', line 20 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
21 22 23 |
# File 'lib/gamewisp/authorizer.rb', line 21 def port @port end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
19 20 21 |
# File 'lib/gamewisp/authorizer.rb', line 19 def redirect_uri @redirect_uri end |
#scopes ⇒ Object
Returns the value of attribute scopes.
22 23 24 |
# File 'lib/gamewisp/authorizer.rb', line 22 def scopes @scopes end |
#state ⇒ Object
Returns the value of attribute state.
23 24 25 |
# File 'lib/gamewisp/authorizer.rb', line 23 def state @state end |
#token_store ⇒ Object
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_url ⇒ Object
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 params = { response_type: "code", client_id: token_store.client_id, redirect_uri: redirect_uri, scope: scopes, state: state, } url = { host: gamewisp_url, path: , query: URI.encode_www_form(params) } return URI::HTTPS.build(url) end |
#create_server_instance ⇒ Object
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 |