Sha256: 5d400ee6fa0c744bf276701b9dfe3bf31543a8c7e21cf2f2af4c61ea6dcbc59e

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 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 = 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_ = case redirect_uri
            when URI::Generic
              redirect_uri
            when String
              URI.parse(redirect_uri)
            else
              raise "Invalid redirect_uri is given. String or URI::Generic is require."
            end
            _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.3 lib/rack/oauth2/server/error.rb
rack-oauth2-0.0.2 lib/rack/oauth2/server/error.rb
rack-oauth2-0.0.1 lib/rack/oauth2/server/error.rb