Class: JwtBouncer::Request
- Inherits:
-
Object
- Object
- JwtBouncer::Request
- Defined in:
- lib/jwt_bouncer/request.rb
Constant Summary collapse
- HEADER =
'Authorization'.freeze
Class Method Summary collapse
-
.extract_token(request) ⇒ Object
extracts the encoded token from the given request.
Instance Method Summary collapse
- #account_reference ⇒ Object
- #actor ⇒ Object
- #authenticated? ⇒ Boolean
- #can?(action) ⇒ Boolean
-
#initialize(request, shared_secret: nil) ⇒ Request
constructor
A new instance of Request.
- #permissions ⇒ Object
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_reference ⇒ Object
26 27 28 |
# File 'lib/jwt_bouncer/request.rb', line 26 def account_reference decoded_token['account_reference'] end |
#actor ⇒ Object
22 23 24 |
# File 'lib/jwt_bouncer/request.rb', line 22 def actor decoded_token['actor'] end |
#authenticated? ⇒ 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
34 35 36 37 38 |
# File 'lib/jwt_bouncer/request.rb', line 34 def can?(action) = Permissions.destructure(action) = & == end |
#permissions ⇒ Object
30 31 32 |
# File 'lib/jwt_bouncer/request.rb', line 30 def @permissions ||= Permissions.decompress(decoded_token['permissions']) end |