Class: Rack::JsonWebTokenAuth::Resource

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/rack/json_web_token_auth/resource.rb

Constant Summary collapse

C =
Contracts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_resource, path, opts = {}) ⇒ Resource

Returns a new instance of Resource.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/json_web_token_auth/resource.rb', line 12

def initialize(public_resource, path, opts = {})
  @public_resource = public_resource
  @path = path
  @pattern = compile(path)
  @opts = opts

  if public_resource
    # unsecured resources should not have any jwt options defined
    if @opts.key?(:jwt)
      raise 'unexpected jwt options provided for unsecured resource'
    end
  else
    # secured resources must have a :jwt hash with a :key
    unless Contract.valid?(@opts, ({ jwt: { key: nil, alg: 'none' } })) ||
           Contract.valid?(@opts, ({ jwt: { key: C::Key } }))
      raise 'invalid or missing jwt options for secured resource'
    end
  end
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



9
10
11
# File 'lib/rack/json_web_token_auth/resource.rb', line 9

def opts
  @opts
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/rack/json_web_token_auth/resource.rb', line 9

def path
  @path
end

#patternObject

Returns the value of attribute pattern.



9
10
11
# File 'lib/rack/json_web_token_auth/resource.rb', line 9

def pattern
  @pattern
end

#public_resourceObject

Returns the value of attribute public_resource.



9
10
11
# File 'lib/rack/json_web_token_auth/resource.rb', line 9

def public_resource
  @public_resource
end

Instance Method Details

#matches_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rack/json_web_token_auth/resource.rb', line 33

def matches_path?(path)
  pattern =~ path
end

#public_resource?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rack/json_web_token_auth/resource.rb', line 38

def public_resource?
  public_resource
end