Module: Rack::OAuth2::Util

Defined in:
lib/rack/oauth2/util.rb

Class Method Summary collapse

Class Method Details

.base64_encode(text) ⇒ Object



11
12
13
# File 'lib/rack/oauth2/util.rb', line 11

def base64_encode(text)
  Base64.encode64(text).gsub(/\n/, '')
end

.compact_hash(hash) ⇒ Object



15
16
17
18
19
# File 'lib/rack/oauth2/util.rb', line 15

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

.parse_uri(uri) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/oauth2/util.rb', line 21

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

.redirect_uri(base_uri, location, params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/oauth2/util.rb', line 32

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

.rfc3986_encode(text) ⇒ Object



7
8
9
# File 'lib/rack/oauth2/util.rb', line 7

def rfc3986_encode(text)
  URI.encode(text, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

.uri_match?(base, given) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/oauth2/util.rb', line 43

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