Class: Redd::AuthStrategies::Web

Inherits:
AuthStrategy show all
Defined in:
lib/redd/auth_strategies/web.rb

Overview

A typical code-based authentication, for ‘web’ and ‘installed’ types.

Constant Summary

Constants inherited from AuthStrategy

AuthStrategy::AUTH_ENDPOINT

Constants inherited from Client

Client::USER_AGENT

Instance Method Summary collapse

Methods inherited from AuthStrategy

#revoke

Methods inherited from Client

#delete, #get, #patch, #post, #put, #request

Constructor Details

#initialize(client_id:, redirect_uri:, secret: '', **kwargs) ⇒ Web

Returns a new instance of Web.



9
10
11
12
# File 'lib/redd/auth_strategies/web.rb', line 9

def initialize(client_id:, redirect_uri:, secret: '', **kwargs)
  super(client_id: client_id, secret: secret, **kwargs)
  @redirect_uri = redirect_uri
end

Instance Method Details

#authenticate(code) ⇒ Access

Authenticate with a code using the “web” flow.

Parameters:

  • code (String)

    the code returned by reddit

Returns:

  • (Access)


17
18
19
# File 'lib/redd/auth_strategies/web.rb', line 17

def authenticate(code)
  request_access('authorization_code', code: code, redirect_uri: @redirect_uri)
end

#refresh(access) ⇒ Access

Refresh the authentication and return a new refreshed access

Returns:

  • (Access)

    the new access



23
24
25
26
27
28
# File 'lib/redd/auth_strategies/web.rb', line 23

def refresh(access)
  token = access.is_a?(String) ? refresh_token : access.refresh_token
  response = post('/api/v1/access_token', grant_type: 'refresh_token', refresh_token: token)
  # When refreshed, the response doesn't include an access token, so we have to add it.
  Models::Access.new(self, response.body.merge(refresh_token: token))
end