Sha256: cb87276fc11e7a5e424e59bfcc822faee7d57360f1d966439b5202190ed0bc32

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require_relative 'auth_strategy'

module Redd
  module AuthStrategies
    # A typical code-based authentication, for 'web' and 'installed' types.
    class Web < AuthStrategy
      def initialize(client_id:, redirect_uri:, secret: '', **kwargs)
        super(client_id: client_id, secret: secret, **kwargs)
        @redirect_uri = redirect_uri
      end

      # Authenticate with a code using the "web" flow.
      # @param code [String] the code returned by reddit
      # @return [Access]
      def authenticate(code)
        request_access('authorization_code', code: code, redirect_uri: @redirect_uri)
      end

      # @return [Boolean] whether the access has a refresh token
      def refreshable?(access)
        access.permanent?
      end

      # Refresh the authentication and return a new refreshed access
      # @return [Access] the new access
      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(response.body.merge(refresh_token: token))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redd-0.9.0.pre.3 lib/redd/auth_strategies/web.rb
redd-0.9.0.pre.2 lib/redd/auth_strategies/web.rb
redd-0.9.0.pre.1 lib/redd/auth_strategies/web.rb