Class ServiceIdentities
java.lang.Object
com.kingsrook.qqq.backend.core.serviceidentity.ServiceIdentities
Run background work as a service principal (docs/SERVICE-IDENTITY.md).
ServiceIdentities.run("scheduler", () -> ...); // as the principal
ServiceIdentities.runOnBehalfOf("scheduler", userId, () -> ...); // as the user, via the principal
The instance's authentication module supplies the session: OAuth2 obtains a
real access token and builds a bearer session; local modules mint a
QServiceSession with the principal's declared local permissions. A missing,
unconfigured or refused principal throws - there is never a fallback to the
system user. Call per unit of work (a report, a delivery, a chunk), so the
work always holds a fresh session.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringattributionName(QSession session) Audit/log name: service:{name}, or {user} via service:{name}; null for other sessions.static <T> Tcall(String principalName, UnsafeSupplier<T, ? extends Exception> body) Run work as the principal and return its result.static booleanisDeclared(QInstance qInstance, String principalName) Whether the instance declares this principal (callers keep their previous behavior when it does not, so single-node defaults are unchanged).static booleanisMintedServiceSession(QSession session) Whether this is a principal's own session minted in this JVM (an OAuth2 service bearer session or a local QServiceSession) - as opposed to a request's bearer token or a hand-made session.static booleanisSessionValid(QInstance qInstance, QAuthenticationModuleInterface module, QSession session) Whether a session is valid for the module - the single place service and delegated sessions are judged (ActionHelper.validateSession): - a delegated session is valid while its parent is; - a QServiceSession only when the module mints local principals and this JVM minted it (never under OAuth2 or Auth0); - anything else is up to the module, unchanged.static StringprincipalNameFromId(String principalId) The principal name from a principal id (service:{name}), or null.static StringprincipalOf(QSession session) The principal a session runs as (service or delegated), or null.static voidrun(String principalName, UnsafeVoidVoidMethod<? extends Exception> body) Run work as the principal; the caller's context is restored afterwards.static voidrunOnBehalfOf(String principalName, String userId, UnsafeVoidVoidMethod<? extends Exception> body) Run work on behalf of a user, via the principal.static QSessionA fresh session for the principal.static QSessionA fresh session for the principal, in the current instance.static QSessionsessionOnBehalfOf(QInstance qInstance, String principalName, String userId) A session for work the principal does on behalf of a user: the user's CURRENT access (rebuilt by the authentication module now, so a removed or deactivated user fails) - by default exactly that access (owner decision 2026-10-16, D2 amended), or intersected with the principal's when the principal is declared INTERSECT_WITH_PRINCIPAL - attributed to the principal and the user.
-
Field Details
-
PRINCIPAL_ID_PREFIX
- See Also:
-
VALUE_PRINCIPAL
- See Also:
-
VALUE_ON_BEHALF_OF
- See Also:
-
CLIENT_CHANNEL
- See Also:
-
SCHEDULER
//////////////////////////////////////////////////////////////////////////- See Also:
-
-
Method Details
-
isDeclared
-
principalNameFromId
-
session
A fresh session for the principal, in the current instance.- Throws:
QException
-
session
A fresh session for the principal.- Throws:
QException
-
run
public static void run(String principalName, UnsafeVoidVoidMethod<? extends Exception> body) throws QException Run work as the principal; the caller's context is restored afterwards.- Throws:
QException
-
call
public static <T> T call(String principalName, UnsafeSupplier<T, ? extends Exception> body) throws QExceptionRun work as the principal and return its result.- Throws:
QException
-
sessionOnBehalfOf
public static QSession sessionOnBehalfOf(QInstance qInstance, String principalName, String userId) throws QException A session for work the principal does on behalf of a user: the user's CURRENT access (rebuilt by the authentication module now, so a removed or deactivated user fails) - by default exactly that access (owner decision 2026-10-16, D2 amended), or intersected with the principal's when the principal is declared INTERSECT_WITH_PRINCIPAL - attributed to the principal and the user. Valid only while the principal's own session is.- Throws:
QException
-
runOnBehalfOf
public static void runOnBehalfOf(String principalName, String userId, UnsafeVoidVoidMethod<? extends Exception> body) throws QException Run work on behalf of a user, via the principal.- Throws:
QException
-
isSessionValid
public static boolean isSessionValid(QInstance qInstance, QAuthenticationModuleInterface module, QSession session) Whether a session is valid for the module - the single place service and delegated sessions are judged (ActionHelper.validateSession): - a delegated session is valid while its parent is; - a QServiceSession only when the module mints local principals and this JVM minted it (never under OAuth2 or Auth0); - anything else is up to the module, unchanged. -
isMintedServiceSession
Whether this is a principal's own session minted in this JVM (an OAuth2 service bearer session or a local QServiceSession) - as opposed to a request's bearer token or a hand-made session. Delegated sessions are not. -
principalOf
-
attributionName
-