Sub topic 11
Invoker-Rights Packages with `AUTHID CURRENT_USER`
Prerequisites: you should understand package specifications and bodies, standalone PL/SQL subprograms, SQL privileges, and basic exception handling.
Definer rights and invoker rights
A package normally runs with definer rights. Definer rights means that SQL inside the package uses the privileges of the package owner. The caller needs permission to execute the package, but does not usually need direct permission on every table that the package accesses.
An invoker-rights package runs with the privileges of the user who calls it. You request this behavior by adding AUTHID CURRENT_USER to the package specification.
CREATE OR REPLACE PACKAGE emp_api
AUTHID CURRENT_USER
AS
FUNCTION get_salary(
p_employee_id IN employees.employee_id%TYPE
) RETURN employees.salary%TYPE;
END emp_api;
/The body implements the specification normally:
CREATE OR REPLACE PACKAGE BODY emp_api AS
FUNCTION get_salary(
p_employee_id IN employees.employee_id%TYPE
) RETURN employees.salary%TYPE
IS
l_salary employees.salary%TYPE;
BEGIN
SELECT salary
INTO l_salary
FROM employees
WHERE employee_id = p_employee_id;
RETURN l_salary;
END get_salary;
END emp_api;
/The AUTHID clause belongs on the specification. The body does not repeat it.
What changes at runtime
Suppose the package belongs to HR_OWNER, and REPORT_USER calls emp_api.get_salary.
With the default definer-rights behavior:
- SQL inside the package runs with
HR_OWNERprivileges. - The package can access objects available to
HR_OWNER. REPORT_USERgenerally needsEXECUTEon the package, not directSELECTonemployees.
With AUTHID CURRENT_USER:
- SQL inside the package runs with
REPORT_USERprivileges. REPORT_USERmust have the required privileges on referenced objects.- Name resolution uses the invoker's current schema.
- Enabled roles can contribute privileges to the invocation.
The following diagram shows the difference:
| Package declaration | SQL privilege context | Typical use |
|---|---|---|
| No `AUTHID` clause | Package owner | Controlled APIs that centralize access |
| `AUTHID CURRENT_USER` | Calling user | Generic utilities that act on the caller's objects |
Name resolution and privileges
Invoker rights affects more than permission checks. An unqualified object name can resolve in the invoker's schema. For example, a query against orders may refer to REPORT_USER.ORDERS when REPORT_USER invokes the package, rather than HR_OWNER.ORDERS.
This behavior is useful for reusable utilities. A package can process tables belonging to different users, provided each caller has the necessary privileges and the expected objects exist.
It can also cause runtime failures. A package may compile successfully under its owner but fail when a caller lacks a direct privilege or has no matching object in the current schema.
Granting EXECUTE on an invoker-rights package does not grant access to the tables inside it:
GRANT EXECUTE ON emp_api TO report_user;
GRANT SELECT ON employees TO report_user;The second grant is required for the example package to read employees.
Choosing the right model
Choose definer rights when:
- The package is a controlled gateway to protected data.
- Callers should receive one consistent result regardless of their schema.
- You want the owner to control the implementation's table privileges.
Choose invoker rights when:
- The operation must respect the caller's privileges.
- The package is a general-purpose utility.
- The same code should operate on objects owned by different users.
Test both successful and unauthorized calls. Also test the package from each supported schema, because object resolution can differ between callers.
Exercise
A package belongs to APPOWNER and is declared with AUTHID CURRENTUSER. ANALYST has EXECUTE on the package but no SELECT privilege on the table used by its function. What should you expect?
Show answer
The call can start, but the table access fails because the function runs with ANALYST's privileges. Granting EXECUTE on the package does not grant SELECT on the table.