Sha256: 7e6d5a70e636d84adb637f0d36b3333b91ba2a3ff7386bb603522a648322529f

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module OAuth2
  module Grant
    # Authorization Code Grant
    # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1
    class AuthorizationCode < Base

      def response_type
        "code"
      end

      def grant_type
        "authorization_code"
      end

      # Authorization Request
      # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1.1
      def authorization_path(params={})
        params = params.merge(authorization_params)
        "#{@authorize_path}?#{to_query(params)}"
      end

      def authorization_url(params={})
        params = params.merge(authorization_params)
        build_url(host, :path => authorize_path, :params => params)
      end

      # Access Token Request
      # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1.3
      def token_path(params={})
        unless params.empty?
          return "#{@token_path}?#{to_query(params)}"
        end
        @token_path
      end

      # Retrieve page at authorization path
      #
      # @param [Hash] opts options
      def fetch_authorization_url(opts={})
        opts[:method] ||= :get
        opts[:params] ||= {}
        opts[:params].merge!(authorization_params)
        method = opts.delete(:method) || :get
        make_request(method, @authorize_path, opts)
      end

      # Retrieve an access token for a given auth code
      #
      # @param [String] code refresh token
      # @param [Hash]   params additional params
      # @param [Hash]   opts options
      def get_token(code, opts={})
        opts[:params] ||= {}
        opts[:params][:code] = code
        opts[:authenticate] ||= :headers
        method = opts.delete(:method) || :post
        make_request(method, token_path, opts)
      end

    private

      # Default authorization request parameters
      def authorization_params
        {
          :response_type => response_type,
          :client_id  => @client_id
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oauth2-client-1.0.0 lib/oauth2/grant/authorization_code.rb