Sha256: 0f320dffae22fb60c4835f8a370a73d2e0280f13e7530e1b0632e99a1b8a4221

Contents?: true

Size: 1.67 KB

Versions: 16

Compression:

Stored size: 1.67 KB

Contents

require 'base64'

module Rack
  module OAuth2
    module Util
      class << self
        def www_form_url_encode(text)
          URI.encode_www_form_component(text)
        end

        def www_form_url_decode(text)
          URI.decode_www_form_component(text)
        end

        def base64_encode(text)
          Base64.encode64(text).delete("\n")
        end

        def urlsafe_base64_encode(text)
          Base64.urlsafe_encode64(text, padding: false)
        end

        def compact_hash(hash)
          hash.reject do |key, value|
            value.blank?
          end
        end

        def parse_uri(uri)
          case uri
          when URI::Generic
            uri
          when String
            URI.parse(uri)
          else
            raise "Invalid format of URI is given."
          end
        end

        def redirect_uri(base_uri, location, params)
          redirect_uri = parse_uri base_uri
          encoded_response_params = Util.compact_hash(params).to_query.gsub('+', '%20')
          case location
          when :query
            redirect_uri.query = [redirect_uri.query, encoded_response_params].compact.join('&')
          when :fragment
            redirect_uri.fragment = encoded_response_params
          end
          redirect_uri.to_s
        end

        def uri_match?(base, given)
          base = parse_uri(base)
          given = parse_uri(given)
          base.path = '/' if base.path.blank?
          given.path = '/' if given.path.blank?
          [:scheme, :host, :port].all? do |key|
            base.send(key) == given.send(key)
          end && !!(/^#{base.path}/ =~ given.path)
        rescue
          false
        end

      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rack-oauth2-2.2.1 lib/rack/oauth2/util.rb
rack-oauth2-2.2.0 lib/rack/oauth2/util.rb
rack-oauth2-2.1.0 lib/rack/oauth2/util.rb
rack-oauth2-2.0.1 lib/rack/oauth2/util.rb
rack-oauth2-2.0.0 lib/rack/oauth2/util.rb
rack-oauth2-2.0.0.rc3 lib/rack/oauth2/util.rb
rack-oauth2-2.0.0.rc2 lib/rack/oauth2/util.rb
rack-oauth2-2.0.0.rc1 lib/rack/oauth2/util.rb
rack-oauth2-1.21.3 lib/rack/oauth2/util.rb
rack-oauth2-1.21.2 lib/rack/oauth2/util.rb
rack-oauth2-1.21.1 lib/rack/oauth2/util.rb
rack-oauth2-1.21.0 lib/rack/oauth2/util.rb
rack-oauth2-1.20.0 lib/rack/oauth2/util.rb
rack-oauth2-1.19.0 lib/rack/oauth2/util.rb
rack-oauth2-1.18.0 lib/rack/oauth2/util.rb
rack-oauth2-1.17.0 lib/rack/oauth2/util.rb