Class: JwtBouncer::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_bouncer/request.rb

Constant Summary collapse

HEADER =
'Authorization'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, shared_secret: nil) ⇒ Request

Returns a new instance of Request.



11
12
13
14
# File 'lib/jwt_bouncer/request.rb', line 11

def initialize(request, shared_secret: nil)
  @encoded_token = Request.extract_token(request)
  @shared_secret = shared_secret
end

Class Method Details

.extract_token(request) ⇒ Object

extracts the encoded token from the given request



41
42
43
44
45
# File 'lib/jwt_bouncer/request.rb', line 41

def self.extract_token(request)
  return nil unless request.headers.key?(HEADER)
  matches = request.headers.fetch(HEADER).match(/\ABearer\s(.*)\z/i)
  matches[1] if matches
end

Instance Method Details

#account_referenceObject



26
27
28
# File 'lib/jwt_bouncer/request.rb', line 26

def 
  decoded_token['account_reference']
end

#actorObject



22
23
24
# File 'lib/jwt_bouncer/request.rb', line 22

def actor
  decoded_token['actor']
end

#authenticated?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/jwt_bouncer/request.rb', line 16

def authenticated?
  !!decoded_token
rescue JWT::DecodeError
  false
end

#can?(action) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/jwt_bouncer/request.rb', line 34

def can?(action)
  destructured_action_permissions = Permissions.destructure(action)
  matching_permissions = destructured_action_permissions & destructured_permissions
  matching_permissions == destructured_action_permissions
end

#permissionsObject



30
31
32
# File 'lib/jwt_bouncer/request.rb', line 30

def permissions
  @permissions ||= Permissions.decompress(decoded_token['permissions'])
end