Class: Rack::PrxAuth::TokenData

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/prx_auth/token_data.rb

Constant Summary collapse

WILDCARD_RESOURCE_NAME =
'*'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ TokenData

Returns a new instance of TokenData.

[View source]

8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/prx_auth/token_data.rb', line 8

def initialize(attrs = {})
  @attributes = attrs
  if attrs['aur']
    @authorized_resources = unpack_aur(attrs['aur']).freeze
  else
    @authorized_resources = {}.freeze
  end
  if attrs['scope']
    @scopes = attrs['scope'].split(' ').freeze
  else
    @scopes = [].freeze
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.


6
7
8
# File 'lib/rack/prx_auth/token_data.rb', line 6

def attributes
  @attributes
end

#authorized_resourcesObject (readonly)

Returns the value of attribute authorized_resources.


6
7
8
# File 'lib/rack/prx_auth/token_data.rb', line 6

def authorized_resources
  @authorized_resources
end

#scopesObject (readonly)

Returns the value of attribute scopes.


6
7
8
# File 'lib/rack/prx_auth/token_data.rb', line 6

def scopes
  @scopes
end

Instance Method Details

#authorized?(resource, scope = nil) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

26
27
28
29
30
31
32
33
34
# File 'lib/rack/prx_auth/token_data.rb', line 26

def authorized?(resource, scope=nil)
  if resource == WILDCARD_RESOURCE_NAME
    globally_authorized?(scope)
  elsif scope.nil?
    authorized_for_resource?(resource, scope)
  else
    authorized_for_resource?(resource, scope) || globally_authorized?(scope)
  end
end

#globally_authorized?(scope) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)
[View source]

36
37
38
39
40
# File 'lib/rack/prx_auth/token_data.rb', line 36

def globally_authorized?(scope)
  raise ArgumentError if scope.nil?

  authorized_for_resource?(WILDCARD_RESOURCE_NAME, scope)
end

#user_idObject

[View source]

22
23
24
# File 'lib/rack/prx_auth/token_data.rb', line 22

def user_id
  @attributes['sub']
end