Class: Zaikio::JWTAuth::TokenData
- Inherits:
-
Object
- Object
- Zaikio::JWTAuth::TokenData
- Defined in:
- lib/zaikio/jwt_auth/token_data.rb
Class Method Summary collapse
Instance Method Summary collapse
- #audience ⇒ Object
- #audiences ⇒ Object
- #expires_at ⇒ Object
-
#initialize(payload, token: nil) ⇒ TokenData
constructor
A new instance of TokenData.
- #jti ⇒ Object
- #on_behalf_of_id ⇒ Object
- #on_behalf_of_type ⇒ Object
- #scope ⇒ Object
-
#scope?(allowed_scopes, action_name, app_name: nil, type: nil, scope: nil) ⇒ Boolean
rubocop:disable Metrics/AbcSize.
-
#scope_by_configurations?(configuration, action_name) ⇒ Boolean
scope_options is an array of objects with: scope, app_name (optional), except/only (array, optional), type (read, write, readwrite).
- #subject ⇒ Object
- #subject_id ⇒ Object
- #subject_match ⇒ Object
- #subject_type ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(payload, token: nil) ⇒ TokenData
Returns a new instance of TokenData.
24 25 26 27 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 24 def initialize(payload, token: nil) @payload = payload @token = token end |
Class Method Details
.actions_by_permission ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 8 def self. { "r" => %w[show index], "w" => %w[update create destroy], "rw" => %w[show index update create destroy] }.freeze end |
.permissions_by_type ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 16 def self. { read: %w[r rw], write: %w[rw w], read_write: %w[r rw w] } end |
.subject_format ⇒ Object
4 5 6 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 4 def self.subject_format %r{^((\w+)/((\w|-)+)>)?(\w+)/((\w|-)+)$} end |
Instance Method Details
#audience ⇒ Object
33 34 35 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 33 def audience audiences.first end |
#audiences ⇒ Object
37 38 39 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 37 def audiences @payload["aud"] || [] end |
#expires_at ⇒ Object
49 50 51 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 49 def expires_at Time.zone.at(@payload["exp"]).to_datetime end |
#jti ⇒ Object
45 46 47 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 45 def jti @payload["jti"] end |
#on_behalf_of_id ⇒ Object
86 87 88 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 86 def on_behalf_of_id subject_match[3] end |
#on_behalf_of_type ⇒ Object
90 91 92 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 90 def on_behalf_of_type subject_match[2] end |
#scope ⇒ Object
41 42 43 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 41 def scope @payload["scope"] end |
#scope?(allowed_scopes, action_name, app_name: nil, type: nil, scope: nil) ⇒ Boolean
rubocop:disable Metrics/AbcSize
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 61 def scope?(allowed_scopes, action_name, app_name: nil, type: nil, scope: nil) # rubocop:disable Metrics/AbcSize app_name ||= Zaikio::JWTAuth.configuration.app_name scope ||= self.scope Array(allowed_scopes).map(&:to_s).any? do |allowed_scope| scope.any? do |s| parts = s.split(".") parts[0] == app_name && parts[1] == allowed_scope && action_permitted?(action_name, parts[2], type: type) end end end |
#scope_by_configurations?(configuration, action_name) ⇒ Boolean
scope_options is an array of objects with: scope, app_name (optional), except/only (array, optional), type (read, write, readwrite)
55 56 57 58 59 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 55 def scope_by_configurations?(configuration, action_name) return true unless configuration scope?(configuration[:scopes], action_name, app_name: configuration[:app_name], type: configuration[:type]) end |
#subject ⇒ Object
82 83 84 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 82 def subject "#{subject_type}/#{subject_id}" end |
#subject_id ⇒ Object
74 75 76 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 74 def subject_id subject_match[6] end |
#subject_match ⇒ Object
94 95 96 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 94 def subject_match self.class.subject_format.match(@payload["sub"]) || [] end |
#subject_type ⇒ Object
78 79 80 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 78 def subject_type subject_match[5] end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/zaikio/jwt_auth/token_data.rb', line 29 def to_s @token end |