Class: RoadForest::Authorization::GrantsHolder

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/authorization/grants-holder.rb

Overview

Caches the obfuscated tokens used to identify permission grants

Constant Summary collapse

PERCENT_ENCODINGS =
Hash.new do |h,k|
  h[k] = k.force_encoding("US-ASCII").getbyte(0).to_s(16)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(salt, hash_function) ⇒ GrantsHolder

Returns a new instance of GrantsHolder.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/roadforest/authorization/grants-holder.rb', line 6

def initialize(salt, hash_function)
  digester = OpenSSL::HMAC.new(salt, hash_function)
  @conceal = true
  @grants_cache = Hash.new do |h, k| #XXX potential resource exhaustion here - only accumulate auth'd results
    if conceal
      digester.reset
      digester << token_for(k)
      h[k] = digester.hexdigest
    else
      token_for(k)
    end
  end
end

Instance Attribute Details

#concealObject

Returns the value of attribute conceal.



19
20
21
# File 'lib/roadforest/authorization/grants-holder.rb', line 19

def conceal
  @conceal
end

Instance Method Details

#build_grants {|builder| ... } ⇒ Object

Yields:

  • (builder)


51
52
53
54
55
# File 'lib/roadforest/authorization/grants-holder.rb', line 51

def build_grants
  builder = GrantBuilder.new(self)
  yield builder
  return builder.list
end

#get(key) ⇒ Object Also known as: []



46
47
48
# File 'lib/roadforest/authorization/grants-holder.rb', line 46

def get(key)
  @grants_cache[key]
end

#group(list, sep, replace) ⇒ Object



32
33
34
# File 'lib/roadforest/authorization/grants-holder.rb', line 32

def group(list, sep, replace)
  list.map{|part| part.to_s.gsub(sep, replace)}.join(sep)
end

#percent_encode(string) ⇒ Object



40
41
42
43
44
# File 'lib/roadforest/authorization/grants-holder.rb', line 40

def percent_encode(string)
  string.gsub(%r|[\[\]:/?#@!$&'()*+;=]|) do |match|
    PERCENT_ENCODINGS[match]
  end
end

#token_for(grant) ⇒ Object

For use in URIs, per RFC3986: Cannot use: “:/?#[]@!$&‘()*+;=” Percent encoding uses % Can use: “.,$^*_-|<>~`” Grants are of the form [:name, [:key, value]*]



26
27
28
29
30
# File 'lib/roadforest/authorization/grants-holder.rb', line 26

def token_for(grant)
  name, attrs = *grant
  attrs = (attrs || []).map{|pair| group(pair, "_", "~")}
  percent_encode(group([name] + attrs, ".", "-"))
end