Class: JWTSessions::Session
- Inherits:
-
Object
- Object
- JWTSessions::Session
- Defined in:
- lib/jwt_sessions/session.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#csrf_token ⇒ Object
readonly
Returns the value of attribute csrf_token.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#refresh_by_access_allowed ⇒ Object
Returns the value of attribute refresh_by_access_allowed.
-
#refresh_payload ⇒ Object
Returns the value of attribute refresh_payload.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#store ⇒ Object
Returns the value of attribute store.
Class Method Summary collapse
Instance Method Summary collapse
- #flush_by_access_payload ⇒ Object
-
#flush_by_token(token) ⇒ Object
flush the session by refresh token.
-
#flush_by_uid(uid) ⇒ Object
flush the session by refresh token uid.
- #flush_namespaced ⇒ Object
-
#flush_namespaced_access_tokens ⇒ Object
flush access tokens only and keep refresh.
-
#initialize(options = {}) ⇒ Session
constructor
A new instance of Session.
- #login ⇒ Object
- #masked_csrf(access_token) ⇒ Object
- #refresh(refresh_token, &block) ⇒ Object
- #refresh_by_access_payload(&block) ⇒ Object
- #session_exists?(token, token_type = :access) ⇒ Boolean
- #valid_access_request?(external_csrf_token, external_payload) ⇒ Boolean
- #valid_csrf?(token, csrf_token, token_type = :access) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Session
Returns a new instance of Session.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jwt_sessions/session.rb', line 15 def initialize( = {}) @store = .fetch(:store, JWTSessions.token_store) @refresh_payload = .fetch(:refresh_payload, {}) @payload = .fetch(:payload, {}) @access_claims = .fetch(:access_claims, {}) @refresh_claims = .fetch(:refresh_claims, {}) @namespace = .fetch(:namespace, nil) @refresh_by_access_allowed = .fetch(:refresh_by_access_allowed, false) @_access_exp = .fetch(:access_exp, nil) @_refresh_exp = .fetch(:refresh_exp, nil) end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
5 6 7 |
# File 'lib/jwt_sessions/session.rb', line 5 def access_token @access_token end |
#csrf_token ⇒ Object (readonly)
Returns the value of attribute csrf_token.
5 6 7 |
# File 'lib/jwt_sessions/session.rb', line 5 def csrf_token @csrf_token end |
#namespace ⇒ Object
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/jwt_sessions/session.rb', line 9 def namespace @namespace end |
#payload ⇒ Object
Returns the value of attribute payload.
9 10 11 |
# File 'lib/jwt_sessions/session.rb', line 9 def payload @payload end |
#refresh_by_access_allowed ⇒ Object
Returns the value of attribute refresh_by_access_allowed.
9 10 11 |
# File 'lib/jwt_sessions/session.rb', line 9 def refresh_by_access_allowed @refresh_by_access_allowed end |
#refresh_payload ⇒ Object
Returns the value of attribute refresh_payload.
9 10 11 |
# File 'lib/jwt_sessions/session.rb', line 9 def refresh_payload @refresh_payload end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
5 6 7 |
# File 'lib/jwt_sessions/session.rb', line 5 def refresh_token @refresh_token end |
#store ⇒ Object
Returns the value of attribute store.
9 10 11 |
# File 'lib/jwt_sessions/session.rb', line 9 def store @store end |
Class Method Details
.flush_all(store = JWTSessions.token_store) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/jwt_sessions/session.rb', line 105 def self.flush_all(store = JWTSessions.token_store) tokens = RefreshToken.all(nil, store) tokens.each do |token| AccessToken.destroy(token.access_uid, store) token.destroy end.count end |
Instance Method Details
#flush_by_access_payload ⇒ Object
65 66 67 68 69 |
# File 'lib/jwt_sessions/session.rb', line 65 def flush_by_access_payload raise Errors::InvalidPayload if payload.nil? ruid = retrieve_val_from(payload, :access, "ruid", "refresh uid") flush_by_uid(ruid) end |
#flush_by_token(token) ⇒ Object
flush the session by refresh token
72 73 74 75 |
# File 'lib/jwt_sessions/session.rb', line 72 def flush_by_token(token) uid = token_uid(token, :refresh, @refresh_claims) flush_by_uid(uid) end |
#flush_by_uid(uid) ⇒ Object
flush the session by refresh token uid
78 79 80 81 82 83 |
# File 'lib/jwt_sessions/session.rb', line 78 def flush_by_uid(uid) token = retrieve_refresh_token(uid) AccessToken.destroy(token.access_uid, store) token.destroy end |
#flush_namespaced ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/jwt_sessions/session.rb', line 96 def flush_namespaced return 0 unless namespace tokens = RefreshToken.all(namespace, store) tokens.each do |token| AccessToken.destroy(token.access_uid, store) token.destroy end.count end |
#flush_namespaced_access_tokens ⇒ Object
flush access tokens only and keep refresh
86 87 88 89 90 91 92 93 94 |
# File 'lib/jwt_sessions/session.rb', line 86 def flush_namespaced_access_tokens return 0 unless namespace tokens = RefreshToken.all(namespace, store) tokens.each do |token| AccessToken.destroy(token.access_uid, store) # unlink refresh token from the current access token token.update(0, 0, token.csrf) end.count end |
#login ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/jwt_sessions/session.rb', line 27 def login create_csrf_token create_access_token create_refresh_token tokens_hash end |
#masked_csrf(access_token) ⇒ Object
46 47 48 |
# File 'lib/jwt_sessions/session.rb', line 46 def masked_csrf(access_token) csrf(access_token).token end |
#refresh(refresh_token, &block) ⇒ Object
50 51 52 53 |
# File 'lib/jwt_sessions/session.rb', line 50 def refresh(refresh_token, &block) refresh_token_data(refresh_token) refresh_by_uid(&block) end |
#refresh_by_access_payload(&block) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/jwt_sessions/session.rb', line 55 def refresh_by_access_payload(&block) raise Errors::InvalidPayload if payload.nil? ruid = retrieve_val_from(payload, :access, "ruid", "refresh uid") retrieve_refresh_token(ruid) check_access_uid_within_refresh_token(&block) if block_given? refresh_by_uid(&block) end |
#session_exists?(token, token_type = :access) ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/jwt_sessions/session.rb', line 39 def session_exists?(token, token_type = :access) send(:"#{token_type}_token_data", token, true) true rescue Errors::Unauthorized false end |
#valid_access_request?(external_csrf_token, external_payload) ⇒ Boolean
113 114 115 116 117 118 119 120 121 |
# File 'lib/jwt_sessions/session.rb', line 113 def valid_access_request?(external_csrf_token, external_payload) ruid = retrieve_val_from(external_payload, :access, "ruid", "refresh uid") uid = retrieve_val_from(external_payload, :access, "uid", "access uid") refresh_token = RefreshToken.find(ruid, JWTSessions.token_store, first_match: true) return false unless uid == refresh_token.access_uid CSRFToken.new(refresh_token.csrf).valid_authenticity_token?(external_csrf_token) end |
#valid_csrf?(token, csrf_token, token_type = :access) ⇒ Boolean
35 36 37 |
# File 'lib/jwt_sessions/session.rb', line 35 def valid_csrf?(token, csrf_token, token_type = :access) send(:"valid_#{token_type}_csrf?", token, csrf_token) end |