Sha256: 098d159826e142f496bc48a5abaa7b762be4421aeadfe36104a69250deaad63c

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require_relative '../client'

module Redd
  module AuthStrategies
    # The API client for authentication to reddit.
    class AuthStrategy < Client
      # The API to make authentication requests to.
      AUTH_ENDPOINT = 'https://www.reddit.com'

      # @param client_id [String] the client id of the reddit app
      # @param secret [String] the app's secret string
      # @param endpoint [String] the url to contact for authentication requests
      # @param user_agent [String] the user agent to send with requests
      def initialize(client_id:, secret:, endpoint: AUTH_ENDPOINT, user_agent: USER_AGENT)
        super(endpoint: endpoint, user_agent: user_agent)
        @client_id = client_id
        @secret = secret
      end

      # @abstract Perform authentication and return the resulting access object
      # @return [Access] the access token object
      def authenticate(*)
        raise 'abstract method: this strategy cannot authenticate with reddit'
      end

      # @abstract Refresh the authentication and return the refreshed access
      # @return [Access] the new access
      def refresh(*)
        raise 'abstract method: this strategy cannot refresh access'
      end

      # Revoke the access token, making it invalid for future requests.
      # @param access [Access] the access object to revoke
      def revoke(access)
        token =
          if access.is_a?(String)
            access
          elsif access.respond_to?(:refresh_token)
            access.refresh_token
          else
            access.access_token
          end
        post('/api/v1/revoke_token', token: token).body
      end

      private

      def connection
        @connection ||= super.basic_auth(user: @client_id, pass: @secret)
      end

      def request_access(grant_type, options = {})
        response = post('/api/v1/access_token', { grant_type: grant_type }.merge(options))
        Models::Access.new(self, response.body)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redd-0.8.1 lib/redd/auth_strategies/auth_strategy.rb
redd-0.8.0 lib/redd/auth_strategies/auth_strategy.rb
redd-0.8.0.pre.2 lib/redd/auth_strategies/auth_strategy.rb
redd-0.8.0.pre.1 lib/redd/auth_strategies/auth_strategy.rb