Class: Parse::Authorization::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/authorization.rb

Overview

Per-client authorization state: the two caches, their TTLs, and the optional upstream-role reader.

One of these is owned by each Client. It deliberately does NOT own the HTTP client: it holds a back-reference and asks the client to make the /users/me call, so there is exactly one place that knows how to talk to a Parse application and it is the client itself.

Constant Summary collapse

DEFAULT_IDENTITY_TTL =
3600
DEFAULT_ROLE_TTL =
30
ROLE_GRAPH_MAX_DEPTH =

Depth cap for the role-graph walk. Bounds a cyclic or pathological hierarchy; see Role.all_for_user.

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Context

Returns a new instance of Context.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/parse/authorization.rb', line 227

def initialize(client:)
  @client = client
  @identity_cache = MemoryCache.new
  @role_cache = MemoryCache.new
  @identity_cache_ttl = DEFAULT_IDENTITY_TTL
  @role_cache_ttl = DEFAULT_ROLE_TTL
  @upstream_role_reader = nil
  @compare_upstream_roles = false
  # Bumped by every invalidation. A token resolution captures it before
  # calling `/users/me` and does not cache its answer when it moved, so
  # a resolve already in flight when a session is revoked cannot put
  # the revoked token back into the plane. A generation-capable plane
  # (the shared Redis identity plane) keeps a plane-wide counter too,
  # so the same holds across processes. See {#lookup_user_id}.
  @invalidation_epoch = 0
  @epoch_mutex = Mutex.new
  @session_owner_cache = SessionOwnerMap.new
end

Instance Attribute Details

#client ⇒ Parse::Client (readonly)

Returns the client this context authorizes for.

Returns:



218
219
220
# File 'lib/parse/authorization.rb', line 218

def client
  @client
end

#compare_upstream_roles ⇒ Boolean

Returns when true, and a reader is set, every role resolution also reads the upstream closure and emits a parse.cache.role_compare event. The comparison NEVER changes what #resolve returns. The upstream value would become an authorization input the moment it were consumed, and it comes from a database this SDK does not own, so it stays observable-only until the two closures have been reconciled against real traffic.

Returns:

  • (Boolean) —

    when true, and a reader is set, every role resolution also reads the upstream closure and emits a parse.cache.role_compare event. The comparison NEVER changes what #resolve returns. The upstream value would become an authorization input the moment it were consumed, and it comes from a database this SDK does not own, so it stays observable-only until the two closures have been reconciled against real traffic.



215
216
217
# File 'lib/parse/authorization.rb', line 215

def compare_upstream_roles
  @compare_upstream_roles
end

#identity_cache ⇒ Object

Returns the identity plane. Maps session token to user id.

Returns:

  • (Object) —

    the identity plane. Maps session token to user id.



193
194
195
# File 'lib/parse/authorization.rb', line 193

def identity_cache
  @identity_cache
end

#identity_cache_ttl ⇒ Integer

Returns identity-entry TTL in seconds.

Returns:

  • (Integer) —

    identity-entry TTL in seconds.



199
200
201
# File 'lib/parse/authorization.rb', line 199

def identity_cache_ttl
  @identity_cache_ttl
end

#role_cache ⇒ Object

Returns the role plane. Maps user id to a Set of role names.

Returns:

  • (Object) —

    the role plane. Maps user id to a Set of role names.



196
197
198
# File 'lib/parse/authorization.rb', line 196

def role_cache
  @role_cache
end

#role_cache_ttl ⇒ Integer

Returns role-entry TTL in seconds.

Returns:

  • (Integer) —

    role-entry TTL in seconds.



202
203
204
# File 'lib/parse/authorization.rb', line 202

def role_cache_ttl
  @role_cache_ttl
end

#session_owner_cache ⇒ Object

Where #remember_session_owner records which user owns a session objectId. A separate store from the identity plane, so a session token can never read a record. Defaults to a process-local SessionOwnerMap; set a shared store (anything with get, set with ttl:, invalidate, and clear) to share records across processes.

Returns:



253
254
255
# File 'lib/parse/authorization.rb', line 253

def session_owner_cache
  @session_owner_cache
end

#upstream_role_reader ⇒ #roles_for?

Returns read-only reader for Parse Server's own role cache. Never consumed for authorization; see #compare_upstream_roles.

Returns:

  • (#roles_for, nil) —

    read-only reader for Parse Server's own role cache. Never consumed for authorization; see #compare_upstream_roles.



206
207
208
# File 'lib/parse/authorization.rb', line 206

def upstream_role_reader
  @upstream_role_reader
end

Instance Method Details

#configure(identity_cache: nil, role_cache: nil, identity_cache_ttl: nil, role_cache_ttl: nil, upstream_role_reader: nil, compare_upstream_roles: nil) ⇒ self

Apply settings, leaving anything not passed unchanged.

Returns:

  • (self)


257
258
259
260
261
262
263
264
265
266
267
# File 'lib/parse/authorization.rb', line 257

def configure(identity_cache: nil, role_cache: nil,
              identity_cache_ttl: nil, role_cache_ttl: nil,
              upstream_role_reader: nil, compare_upstream_roles: nil)
  @identity_cache = identity_cache unless identity_cache.nil?
  @role_cache = role_cache unless role_cache.nil?
  @identity_cache_ttl = identity_cache_ttl unless identity_cache_ttl.nil?
  @role_cache_ttl = role_cache_ttl unless role_cache_ttl.nil?
  @upstream_role_reader = upstream_role_reader unless upstream_role_reader.nil?
  @compare_upstream_roles = compare_upstream_roles unless compare_upstream_roles.nil?
  self
end

#inspect ⇒ Object



398
399
400
# File 'lib/parse/authorization.rb', line 398

def inspect
  "#<Parse::Authorization::Context client=#{@client.respond_to?(:application_id) ? @client.application_id : @client.class}>"
end

#invalidate(session_token) ⇒ Object

Forget one session token. Call from a logout path that revokes out-of-band; Parse::Cache::Invalidation does this automatically from the _Session after_logout trigger when webhooks are installed.

The role plane is keyed by user id and is unaffected; use #invalidate_user_roles for that.

Parameters:



308
309
310
311
312
# File 'lib/parse/authorization.rb', line 308

def invalidate(session_token)
  return if session_token.nil?
  bump_invalidation_epoch!
  @identity_cache.invalidate(session_token.to_s)
end

#invalidate_all_roles ⇒ Object

Forget every cached role closure. Call after a _Role.roles hierarchy change, which can affect any user holding a role in that hierarchy.



354
355
356
# File 'lib/parse/authorization.rb', line 354

def invalidate_all_roles
  @role_cache.clear if @role_cache.respond_to?(:clear)
end

#invalidate_user(user_id)

This method returns an undefined value.

Forget every cached identity entry that resolves to user_id. Call after an event that revokes the user's sessions: a password change, account deletion, a session destroy, or "log out everywhere". The SDK calls it itself on those paths.

The identity plane is keyed by token, so there is no direct way to name a user's entries. A generation-capable plane (the keyspaced Redis identity plane) bumps the user's generation, which rejects every entry for that user, including tokens this process never resolved. The default MemoryCache drops the matching values. A custom plane that supports neither is left to its TTL.

The role entry is dropped too: it is cheap to rebuild and a deleted user should not keep a role closure around.

Parameters:



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/parse/authorization.rb', line 330

def invalidate_user(user_id)
  return if user_id.nil? || user_id.to_s.empty?
  uid = user_id.to_s
  bump_invalidation_epoch!
  cache = @identity_cache
  if generation_capable?(cache) && cache.respond_to?(:bump_generation)
    cache.bump_generation(uid)
  elsif cache.respond_to?(:invalidate_value)
    cache.invalidate_value(uid)
  end
  @role_cache.invalidate(uid)
  nil
end

#invalidate_user_roles(user_id) ⇒ Object

Forget one user's cached role closure. Call after any _Role.users mutation affecting them.

Parameters:



347
348
349
350
# File 'lib/parse/authorization.rb', line 347

def invalidate_user_roles(user_id)
  return if user_id.nil?
  @role_cache.invalidate(user_id.to_s)
end

#remember_session_owner(session_id, user_id)

This method returns an undefined value.

Record which user owns a session objectId, so a later delete of that session can drop the owner's cached identities even when the delete can no longer read the session row (it is already gone, or not visible to the caller). Only the owner's user id is stored, never the token. Called when a _Session row with both is loaded from the server. The entry lives as long as an identity entry.

Parameters:



378
379
380
381
382
383
384
# File 'lib/parse/authorization.rb', line 378

def remember_session_owner(session_id, user_id)
  return if session_id.to_s.empty? || user_id.to_s.empty?
  @session_owner_cache.set(session_id.to_s, { "session_owner" => user_id.to_s }, ttl: @identity_cache_ttl)
  nil
rescue StandardError
  nil
end

#reset_caches! ⇒ Object

Drop every entry in both planes.



359
360
361
362
363
364
365
366
367
# File 'lib/parse/authorization.rb', line 359

def reset_caches!
  bump_invalidation_epoch!
  @identity_cache.clear if @identity_cache.respond_to?(:clear)
  @role_cache.clear if @role_cache.respond_to?(:clear)
  # A plane clear takes the plane-wide marker with it. Bump it again so
  # it does not sit at a value a lookup that started before the reset
  # could have captured.
  bump_plane_epoch!
end

#resolve(session_token) ⇒ Resolved

Resolve a session token to the requesting user and the transitive set of role names whose role:NAME permission strings should be checked against _rperm.

A nil or empty token yields an anonymous Resolved. The caller decides whether that is acceptable; Parse::ACLScope.require_session_token and Parse::AtlasSearch.require_session_token are where that policy lives.

The two lookups are cached independently, so several sessions belonging to one user share a single role-graph walk.

Parameters:

Returns:

Raises:



284
285
286
287
288
289
# File 'lib/parse/authorization.rb', line 284

def resolve(session_token)
  return Resolved.new(nil, Set.new) if session_token.nil? || session_token.to_s.empty?

  user_id = lookup_user_id(session_token.to_s)
  Resolved.new(user_id, lookup_role_names(user_id))
end

#resolve_user(user_id) ⇒ Resolved

Resolve a user id that is already trusted, skipping /users/me. Used by the acl_user: path, which has a User pointer rather than a token.

Parameters:

Returns:



296
297
298
299
# File 'lib/parse/authorization.rb', line 296

def resolve_user(user_id)
  return Resolved.new(nil, Set.new) if user_id.nil? || user_id.to_s.empty?
  Resolved.new(user_id.to_s, lookup_role_names(user_id.to_s))
end

#session_owner(session_id) ⇒ String?

The owner recorded by #remember_session_owner, or nil.

Parameters:

Returns:



389
390
391
392
393
394
395
396
# File 'lib/parse/authorization.rb', line 389

def session_owner(session_id)
  return nil if session_id.to_s.empty?
  value = @session_owner_cache.get(session_id.to_s)
  owner = value.is_a?(Hash) ? value["session_owner"] : nil
  owner.is_a?(String) && !owner.empty? ? owner : nil
rescue StandardError
  nil
end