Sha256: a9e9a5090089bfa46607d85e8c5a173e8f0fe8f5a670ab40e9d830ec53a90315

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module TelphinApi
  # A module containing the methods for authorization.
  #
  # @note `TelphinApi::Authorization` extends `TelphinApi` so these methods should be called from the latter.
  module Authorization
    # Authorization options.
    OPTIONS = {
        client: {
            token_url: '/oauth/token.php'
        },
        client_credentials: {
            'auth_scheme' => 'request_body'
        }
    }

    # Not used for this strategy
    #
    # @raise [NotImplementedError]
    def authorization_url
      fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
    end

    # Authorization (getting the access token and building a `TelphinApi::Client` with it).
    # @raise [ArgumentError] raises after receiving an unknown authorization type.
    # @return [TelphinApi::Client] An API client.
    def authorize(options = {})
      options[:client_id] ||= TelphinApi.app_key
      options[:client_secret] ||= TelphinApi.app_secret
      token = client.client_credentials.get_token(options, OPTIONS[:client_credentials].dup)
      Client.new(token)
    end

    private
    def client
      @client ||= OAuth2::Client.new(TelphinApi.app_key, TelphinApi.app_secret, {site: TelphinApi.site}.merge(OPTIONS[:client]))
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
telphin_api-1.0.4 lib/telphin_api/authorization.rb
telphin_api-1.0.3 lib/telphin_api/authorization.rb
telphin_api-1.0.2 lib/telphin_api/authorization.rb
telphin_api-1.0.1 lib/telphin_api/authorization.rb
telphin_api-1.0.0 lib/telphin_api/authorization.rb