Sha256: 28ed87333217839e258d096fcf985182b7f5355f4e619d86d6cb7154aa552219

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module Rack
  module OAuth2
    module Server

      class Error < StandardError
        attr_accessor :code, :error, :description, :uri, :redirect_uri, :state

        def initialize(code, error, description = "", options = {})
          @code         = code
          @error        = error
          @description  = description
          @uri          = options[:uri]
          @state        = options[:state]
          @redirect_uri = Util.parse_uri(options[:redirect_uri]) if options[:redirect_uri]
        end

        def finish
          params = {
            :error             => error,
            :error_description => description,
            :error_uri         => uri,
            :state             => state
          }.delete_if do |key, value|
            value.blank?
          end
          if redirect_uri
            redirect_uri.query = if redirect_uri.query
              [redirect_uri.query, params.to_query].join('&')
            else
              params.to_query
            end
            response = Rack::Response.new
            response.redirect redirect_uri.to_s
            response.finish
          else
            [code, {'Content-Type' => 'application/json'}, params.to_json]
          end
        end
      end

      class Unauthorized < Error
        def initialize(error, description = "", options = {})
          super(401, error, description, options)
        end
      end

      class BadRequest < Error
        def initialize(error, description = "", options = {})
          super(400, error, description, options)
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-oauth2-0.0.6 lib/rack/oauth2/server/error.rb
rack-oauth2-0.0.5 lib/rack/oauth2/server/error.rb
rack-oauth2-0.0.4 lib/rack/oauth2/server/error.rb