Class: Rack::JsonWebTokenAuth::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/json_web_token_auth/resource.rb', line 10

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 a :jwt option defined
    if @opts.key?(:jwt)
      raise 'unexpected :jwt option provided for unsecured resource'
    end

    # unsecured resources should not have a :methods option defined
    if @opts.key?(:methods)
      raise 'unexpected :methods option 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: Key } }))
      raise 'invalid or missing jwt options for secured resource'
    end

    # Don't allow providing other HTTP methods with :any
    if opts[:methods] && opts[:methods].include?(:any) && opts[:methods].size > 1
      raise 'unexpected additional methods provided with :any'
    end

    @methods = if opts[:methods].nil?
                 [:get]
               elsif opts[:methods] == [:any]
                 [:get, :head, :post, :put, :patch, :delete, :options]
               else
                 opts[:methods]
               end.map { |e| e.to_s }
  end
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



7
8
9
# File 'lib/rack/json_web_token_auth/resource.rb', line 7

def methods
  @methods
end

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/rack/json_web_token_auth/resource.rb', line 7

def opts
  @opts
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/rack/json_web_token_auth/resource.rb', line 7

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



7
8
9
# File 'lib/rack/json_web_token_auth/resource.rb', line 7

def pattern
  @pattern
end

#public_resourceObject (readonly)

Returns the value of attribute public_resource.



7
8
9
# File 'lib/rack/json_web_token_auth/resource.rb', line 7

def public_resource
  @public_resource
end

Instance Method Details

#invalid_http_method?(request_method) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rack/json_web_token_auth/resource.rb', line 59

def invalid_http_method?(request_method)
  request_method.nil? || !methods.include?(request_method.downcase)
end

#matches_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rack/json_web_token_auth/resource.rb', line 49

def matches_path?(path)
  pattern =~ path
end

#public_resource?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rack/json_web_token_auth/resource.rb', line 54

def public_resource?
  public_resource
end