Class: Parse::Session
- Inherits:
-
Object
- Object
- Object
- Parse::Session
- Defined in:
- lib/parse/model/classes/session.rb,
lib/parse/stack/generators/templates/model_session.rb
Overview
CLP on _Session is mostly redundant: non-master find queries
are silently rewritten by Parse Server's REST layer
(RestQuery.js) to scope by user = <current user>, so a caller
never sees another user's sessions regardless of CLP. find also
requires a session token. You cannot grant cross-user session
visibility through Object.set_clp.
This class represents the data and columns contained in the standard Parse
_Session collection. The Session class maintains per-device (or website) authentication
information for a particular user. Whenever a User object is logged in, a new Session record, with
a session token is generated. You may use a known active session token to find the corresponding
user for that session. Deleting a Session record (and session token), effectively logs out the user, when making Parse requests
on behalf of the user using the session token.
The default schema for the Session class is as follows: class Parse::Session < Parse::Object # See Parse::Object for inherited properties...
property :session_token
property :created_with, :object
property :expires_at, :date
property :installation_id
property :restricted, :boolean
belongs_to :user
# Installation where the installation_id matches.
has_one :installation, ->{ where(installation_id: i.installation_id) }, scope_only: true
end
Constant Summary collapse
- RESET_INTERVAL =
Seconds between two full identity-cache resets triggered by a failed session lookup on the same client.
5
Instance Attribute Summary collapse
-
#created_with ⇒ Hash
Data on how this Session was created.
-
#expires_at ⇒ Parse::Date
When the session token expires.
-
#installation ⇒ Parse::Installation
readonly
Returns the Installation where the sessions installation_id field matches the installation_id field in the Installation collection.
-
#installation_id ⇒ String
The installation id from the Installation table.
-
#restricted ⇒ Boolean
readonly
Whether this session token is restricted.
-
#session_token ⇒ String
readonly
The session token for this installation and user pair.
-
#user ⇒ User
readonly
This property is mapped as a
belongs_toassociation with the User class.
Class Method Summary collapse
-
.active ⇒ Parse::Query
Query scope for active (non-expired) sessions.
-
.active_count_for_user(user) ⇒ Integer
Count active sessions for a specific user.
-
.expired ⇒ Parse::Query
Query scope for expired sessions.
-
.for_user(user) ⇒ Parse::Query
Query scope for sessions belonging to a specific user.
-
.revoke_all_for_user(user, except: nil) ⇒ Integer
Revoke (delete) all sessions for a specific user.
-
.session(token, **opts) ⇒ Session
Return the Session record for this session token.
Instance Method Summary collapse
-
#as_json(opts = nil) ⇒ Hash
Serialization omits
sessionTokenunlessinclude_session_token: trueis passed. -
#destroy(session: nil) ⇒ Boolean
Deletes the session and forgets it in the client's identity plane, so mongo-direct and Atlas Search reads stop resolving the token at once instead of when the cached entry expires.
-
#expired? ⇒ Boolean
Check if this session has expired.
-
#expires_within?(duration) ⇒ Boolean
Check if this session expires within the given duration.
-
#inspect ⇒ String
Redacts the session token from the default inspect output.
-
#revoke! ⇒ Boolean
Revoke (delete) this session, effectively logging out the user on this device.
-
#time_remaining ⇒ Float?
Get the remaining time until this session expires.
-
#valid? ⇒ Boolean
Check if this session is still valid (not expired).
Instance Attribute Details
#created_with ⇒ Hash
Returns data on how this Session was created.
43 |
# File 'lib/parse/model/classes/session.rb', line 43 property :created_with, :object |
#expires_at ⇒ Parse::Date
Returns when the session token expires.
47 |
# File 'lib/parse/model/classes/session.rb', line 47 property :expires_at, :date |
#installation ⇒ Parse::Installation (readonly)
Returns the Installation where the sessions installation_id field matches the installation_id field in the Installation collection. This is implemented as a has_one scope.
73 |
# File 'lib/parse/model/classes/session.rb', line 73 has_one :installation, -> { where(installation_id: i.installation_id) }, scope_only: true |
#installation_id ⇒ String
Returns The installation id from the Installation table.
52 |
# File 'lib/parse/model/classes/session.rb', line 52 property :installation_id |
#restricted ⇒ Boolean (readonly)
Returns whether this session token is restricted.
56 |
# File 'lib/parse/model/classes/session.rb', line 56 property :restricted, :boolean |
#session_token ⇒ String (readonly)
Returns the session token for this installation and user pair.
60 |
# File 'lib/parse/model/classes/session.rb', line 60 property :session_token |
Class Method Details
.active ⇒ Parse::Query
Query scope for active (non-expired) sessions.
104 105 106 |
# File 'lib/parse/model/classes/session.rb', line 104 def active query(:expires_at.gte => Time.now) end |
.active_count_for_user(user) ⇒ Integer
Count active sessions for a specific user.
149 150 151 |
# File 'lib/parse/model/classes/session.rb', line 149 def active_count_for_user(user) for_user(user).where(:expires_at.gte => Time.now).count end |
.expired ⇒ Parse::Query
Query scope for expired sessions.
112 113 114 |
# File 'lib/parse/model/classes/session.rb', line 112 def expired query(:expires_at.lt => Time.now) end |
.for_user(user) ⇒ Parse::Query
Query scope for sessions belonging to a specific user.
121 122 123 124 |
# File 'lib/parse/model/classes/session.rb', line 121 def for_user(user) user = Parse::User.pointer(user) if user.is_a?(String) query(user: user) end |
.revoke_all_for_user(user, except: nil) ⇒ Integer
Revoke (delete) all sessions for a specific user.
136 137 138 139 140 141 142 |
# File 'lib/parse/model/classes/session.rb', line 136 def revoke_all_for_user(user, except: nil) sessions = for_user(user) sessions = sessions.where(:session_token.ne => except) if except sessions_to_revoke = sessions.all sessions_to_revoke.each(&:destroy) sessions_to_revoke.count end |
.session(token, **opts) ⇒ Session
Return the Session record for this session token.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/parse/model/classes/session.rb', line 86 def session(token, **opts) # A stray :session_token in opts would be forwarded into the request # stack and silently override the positional token argument. Drop it # so the explicit token always wins. opts.delete(:session_token) response = client.fetch_session(token, **opts) if response.success? built = Parse::Session.build response.result _remember_owners!([built], client) return built end nil end |
Instance Method Details
#as_json(opts = nil) ⇒ Hash
Serialization omits sessionToken unless include_session_token: true
is passed. A session token is a bearer credential, and as_json is the
surface that reaches logs, API responses and agent tool output.
445 446 447 448 449 450 451 |
# File 'lib/parse/model/classes/session.rb', line 445 def as_json(opts = nil) opts = (opts || {}).dup include_token = opts.delete(:include_session_token) == true json = super(opts) return json if include_token || !json.is_a?(Hash) json.except("session_token", "sessionToken", :session_token, :sessionToken) end |
#destroy(session: nil) ⇒ Boolean
Deletes the session and forgets it in the client's identity plane, so
mongo-direct and Atlas Search reads stop resolving the token at once
instead of when the cached entry expires. Both the token and the owning
user's entries are dropped. A session that does not carry them (built
from its objectId, or fetched without the master key or with keys:
leaving them out) has them looked up first: with the master key as SDK
metadata when the client has one, otherwise with the session: passed
here. A lookup that finds no row leaves nothing to forget. Only a
lookup that fails outright drops every cached identity on the client,
at most once every RESET_INTERVAL seconds per client.
The entries are dropped when the delete succeeds or reports the row is already gone (or not visible to the caller). A delete that raises still drops the known token, which is harmless, but does not touch the owner's entries.
A batch delete through Array#destroy drops the same entries for
each session in it. With an identity plane that cannot drop a
single user's entries (a custom plane without bump_generation or
invalidate_value), a revoked token keeps resolving until its cached
entry expires.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/parse/model/classes/session.rb', line 233 def destroy(session: nil) # A session never saved is not deleted (see Parse::Object#destroy), so # there is nothing to forget either. return super if new? token = nil owner_id = nil begin Parse::Session.send(:_preload_identity_for_destroy!, [self], session_token: session) token, owner_id = _identity_for_destroy ensure # The looked-up token is a live credential: keep it in locals only. _clear_identity_for_destroy! end result = nil begin result = super ensure # `false` covers "object not found", which is either a session # already revoked elsewhere or one the caller cannot see; dropping # cached entries is idempotent in both cases. A raised delete still # drops the token it named but leaves the owner alone. A single # delete that returns false cannot tell "already gone" from # "denied", so it never resets and forgets nothing for a row the # caller could not read; one that returns true removed the row, so # the recorded owner (or the rate-limited reset) applies. if result.nil? _forget_identity!(token.is_a?(String) ? token : nil, nil) else deleted = result == true _forget_identity!(token, owner_id, reset_fallback: deleted, deleted: deleted) end end end |
#expired? ⇒ Boolean
Check if this session has expired.
164 165 166 167 |
# File 'lib/parse/model/classes/session.rb', line 164 def expired? return false if expires_at.nil? expires_at < Time.now end |
#expires_within?(duration) ⇒ Boolean
Check if this session expires within the given duration.
197 198 199 200 |
# File 'lib/parse/model/classes/session.rb', line 197 def expires_within?(duration) return false if expires_at.nil? expires_at < (Time.now + duration) end |
#inspect ⇒ String
Redacts the session token from the default inspect output.
455 456 457 458 |
# File 'lib/parse/model/classes/session.rb', line 455 def inspect looked_up = @_identity_for_destroy Parse::User.redact_session_token(super, @session_token, looked_up.is_a?(Array) ? looked_up[0] : nil) end |
#revoke! ⇒ Boolean
Revoke (delete) this session, effectively logging out the user on this device.
206 207 208 |
# File 'lib/parse/model/classes/session.rb', line 206 def revoke! destroy end |
#time_remaining ⇒ Float?
Get the remaining time until this session expires.
184 185 186 187 188 |
# File 'lib/parse/model/classes/session.rb', line 184 def time_remaining return nil if expires_at.nil? remaining = expires_at.to_time - Time.now remaining > 0 ? remaining : 0 end |
#valid? ⇒ Boolean
Check if this session is still valid (not expired).
175 176 177 |
# File 'lib/parse/model/classes/session.rb', line 175 def valid? !expired? end |