End-User Security Context

The end-user security context is the security boundary through which the database identifies an end user and determines what they can see and do. When an application executes a database operation, it passes a set of identity and authorization details (such as tokens and data roles) as the payload. The database uses this payload to establish the end-user security context, which governs the end user’s access for the duration of the Oracle Deep Data Security (Deep Sec) session.

Traditionally, applications connect to databases using a highly privileged database user account. In contrast, the end-user security context constrains the privileges assigned to the database connection to strictly match those of the end user. Deep Sec enforces this by replacing the underlying database session’s security domain (the database user and their roles) with the end-user security context.

Once this replacement occurs, the database authorizes all SQL queries from the application based solely on the end user, their roles, and their attributes, rather than the broad privileges of the application’s database user account. Consequently, the database user account used by the application requires only basic privileges, such as CREATE SESSION and CREATE END USER SECURITY CONTEXT, effectively mitigating the risk of attacks like SQL injection.

Explore the following topics to learn more about the end-user security context.

See Also: About the End-User Security Context in Configure the Security Context.

Components of the Security Context

The end-user security context is an extensible, session-level object that functions as a security context scoped to an individual user or application for access control enforcement. It holds the active end user’s or application’s identity, their enabled data roles, and all live attribute values.

The database creates an end-user security context automatically when an application sends an EndUserSecurityContext payload with the following components:

As discussed previously, for the application to obtain the required tokens, you must perform the required configurations in your IAM. During token issuance, IAM authenticates and authorizes the application’s identity and, when an end user is involved, the end-user identity.

The Security Context Life Cycle

Learn how the end-user security context flows through a Deep Sec environment. Understand how your application builds the security context payload, propagates it to the database, and how the database establishes and manages that security context across SQL operations and connection reuse.

Security context population and transmission

The security context life cycle begins when the application initiates a database operation. During API calls, applications use an Oracle client driver to access data in the database. The Oracle client driver (such as JDBC, Python, or ODP.NET) allows application code to populate the end-user security context payload, which includes the end-user token (containing identity and roles) and application-specific attributes. Additionally, the application provides a database-access token. This token represents the application’s authorization to access the database. In a single-token flow, the end-user token can be omitted. For the payload components, see Components of the Security Context.

Once populated, the client driver attaches this end-user security context payload, including the database-access token, to the database connection as metadata. This occurs before the request is sent to the database. The driver forwards this payload to the database with every subsequent SQL request.

Database validation and session attachment

When the database receives an end-user security context payload containing OAuth 2.0 tokens, it validates the supplied token or tokens against the following criteria:

If validation succeeds and the tokens contain the required scopes, the database grants the application access, establishes the end-user security context on the session, and enables the required data roles in the security context. If the database detects an existing, identical end-user security context, it reuses it. Otherwise, it creates a new security context. All SQL operations in the session execute within the boundaries of this end-user security context (end user’s identity, roles, and attributes), ensuring strict policy enforcement throughout.

The Deep Sec security mechanism can’t be bypassed, as establishing a valid security context requires the application to present a valid database-access token and the database credentials for a connection that supports the end-user security context payload attachment. A separate end-user token is optional, but must be valid when supplied.

Security context detachment and cleanup

The database automatically manages the detachment and reattachment of an end-user security context to a database session through the following mechanisms:

Driver support and configuration

Oracle client drivers, namely JDBC, Python, and ODP.NET drivers, support propagating the end-user security context payload to the database. You can implement this support in your Java, Python, or .NET applications using either of the two methods:

For details, see Update Application Configuration with IAM Details.

End-User Contexts and Attributes

In Deep Sec, you use attributes associated with the active end user in data grant predicates to make authorization decisions at runtime. You can also use these attributes to drive application logic, simplify SQL queries, and avoid complex joins.

To support Deep Sec data grants, Oracle AI Database provides a flexible end-user context model that lets you define context attributes in the database, pass their runtime values in a JSON-formatted payload, and reference them by using JSON-style dot-path notation.

This model relies on two distinct but complementary objects: a database-side definition and its runtime counterpart.

Note: If your data grant or application logic references custom context attributes, you must first create the corresponding end-user context definition in the database before those attributes are available within a runtime end-user security context. See End-user context definition.

Data grants in Deep Sec often reference multiple contexts, such as user, application, and system contexts. These contexts are composed of attribute-value pairs sourced from IAM systems, application logic, or the database itself.

Examples of context attributes include:

End-user context definition

Before an application can pass custom attributes in the end-user security context payload at runtime, you must define the corresponding attributes and end-user contexts in the database. You can do this by creating an END USER CONTEXT object, which acts as a template that specifies allowed attributes, data types, and associated logic.

Oracle AI Database supports two types of end-user contexts: custom (user-defined) and default (system-defined).

For further details, see Configure End-User Contexts and Attributes.

The ORA_END_USER_CONTEXT function

You can reference end-user context attributes using JSON-style dot-path notation with the SQL function, ORA_END_USER_CONTEXT. For example, you can reference a system attribute (the current end-user name) from the USER.DEFAULT context in a data grant as follows:

ORA_END_USER_CONTEXT.username

Similarly, if an HCM application instantiates an end-user context hr.hcm_context with an attribute org_id, the application code can refer to the org_id value as follows:

ORA_END_USER_CONTEXT.hr.hcm_context.org_id

For the complete syntax details, see Read End-User Context Attributes.

Privilege Elevation

An application can dynamically enable additional data roles within an end-user security context to temporarily elevate privileges.

This allows the application to perform restricted database operations on behalf of users who are not directly authorized to perform them, without permanently granting those privileges to the users.

For example, a product marketing manager may need to view total sales for a product but must not have access to individual orders or customer data. The application temporarily elevates privileges in the end-user security context to run a sales-summary query, then reverts to the user’s original access level, ensuring that the underlying granular data remains protected.

The following sections illustrate how you can implement privilege elevation for this example scenario.

Configure data roles and grants

To implement privilege elevation, you must first create a data role managed locally that contains the necessary privileges. You can configure these roles to be disabled by default, ensuring they are only active when explicitly required by the application.

Use the following command to create a data role as disabled specifically for viewing the sales summary:

CREATE OR REPLACE DATA ROLE summarize_sales_role DISABLED;

Next, define a data grant to assign specific permissions to the data role.

In this example, the oe.order_items table contains individual order line items, including product, quantity, and unit price. The sales-summary query aggregates this data without exposing individual order details.

The following command grants the data role permission to select from the oe.order_items table:

CREATE OR REPLACE DATA GRANT SummarizeSalesOrderItems
  AS SELECT
  ON oe.order_items
  TO summarize_sales_role;

Establish and authorize the application identity

To authorize the application, first create an application identity in the database that maps to the application’s external client identifier (such as its client ID in Microsoft Entra ID). Then, grant the required data roles to that identity. The application can only enable data roles that have been explicitly granted to its identity.

Use the following command to create an application identity hcm_app, and map it to a specific client ID in Microsoft Entra ID:

CREATE OR REPLACE APPLICATION IDENTITY hcm_app
MAPPED TO 'AZURE_CLIENT_ID=f1fab37e-7aa2-4ff8-849c-7e731fea3b48';

Grant the previously created data role to the application identity, so the application can elevate privileges at runtime.

GRANT DATA ROLE summarize_sales_role TO hcm_app;

Runtime execution

At runtime, the application enables its assigned data roles through Oracle client-driver APIs, such as the JDBC API and the Python driver API. The application elevates privileges only for the duration of a specific query, establishing a confined security context for that execution.

This approach provides two key security benefits:

For an example of controlled privilege elevation, see Configure Oracle Deep Data Security for a Sample Application.