Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/architecture/DATA_RIGHTS_AUTHORIZATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Data-rights authorization boundary

- Maturity: **PARTIAL** — the domain composition exists, while hosted route/repository integration and external identity-proof execution remain outside this slice.
- Ownership: `ContextualWisdomLab/psychometrics-commons`
- Governing contracts: PRD §11; TRD §10, §11, and §13; `docs/architecture/SECURITY_AND_DATA.md`
Comment thread
seonghobae marked this conversation as resolved.
Resolved

## Purpose

A data export or deletion request is sensitive product state. `DataRightsRequest` is the authoritative product record for the request's tenant, participant owner, and opaque `request_ref`. Authorization must therefore bind to that stored record rather than to copies of those identifiers supplied in a URL, request body, or authenticated actor context.

`src/data_rights_authorization.rs::authorize_data_rights_request` is the composition boundary. It derives a participant-owned `ResourceScope` only from the stored `DataRightsRequest` and evaluates the existing `ManageOwnDataRights` permission. `src/lib.rs` exposes that module as product API surface; it does not transfer ownership to Keyverse or to a transport adapter.

## Trust boundary

```text
authenticated actor context
|
v
stored DataRightsRequest ------------------+
(tenant_ref, participant_ref, request_ref) |
| |
+---------- authoritative identity --+
v
participant-owned ResourceScope
|
v
ManageOwnDataRights
|
allow only exact owner/tenant
```

The actor proves who is asking; the stored request proves which product resource is being asked about. The server never substitutes the actor's own tenant, participant, or path parameter for a stored request attribute. If any required binding is missing, malformed, cross-tenant, or owned by another participant, authorization denies access.

## Unchanged architecture

This change does **not** add a new permission, credential system, data-rights lifecycle state, database object, cross-service database access, or identity-provider responsibility. Keyverse remains the authentication/federation owner; Psychometrics Commons remains the owner of product authorization and the data-rights aggregate. Because ownership, dependency direction, state machine, and persistence schema are unchanged, no superseding ADR is required. The implementation narrows an existing TRD §11 authorization invariant at the adapter-facing boundary.

## Required evidence

- `tests/data_rights_authorization_binding.rs` proves exact stored-owner success and fail-closed cross-tenant, cross-participant, and missing-identity cases.
- `src/data_rights_authorization.rs` unit tests prove malformed stored identity cannot construct an authorization scope.
- `tests/data_rights_authorization_documentation.rs` prevents the trust-boundary mapping from disappearing silently.
- Hosted HTTP/repository adapters must call this stored-record composition before reading, exporting, deleting, or disclosing request state; transport-level negative tests remain required before GA.
120 changes: 120 additions & 0 deletions src/data_rights_authorization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! Authorization for one participant-owned data-rights request using stored identity.
//!
//! A [`DataRightsRequest`] is the authoritative stored record for its tenant, participant
//! owner, and request identifier. That identifier is *opaque*: callers treat it as an
//! issued label and must not infer meaning or sequence from it. API or persistence boundary
//! code (often called an *adapter*) must use those stored values instead of rebuilding the
//! authorization target from URL fields, request bodies, or the authenticated actor.
//!
//! Authorization evaluates a *resource scope*: the tenant, participant owner, resource
//! kind, and exact request identifier that together describe what is being accessed. Using
//! the stored request prevents a *confused deputy* defect, where trusted server code is
//! tricked into acting on a different request because caller-controlled identity fields were
//! substituted for the stored ones. Invalid or incomplete bindings *fail closed*: access is
//! denied rather than guessed or defaulted.
//!
//! The architecture boundary and unchanged ownership are documented in
//! `docs/architecture/DATA_RIGHTS_AUTHORIZATION.md`. This module adds no credential,
//! permission, lifecycle, or database ownership; it composes the existing product-owned
//! `ManageOwnDataRights` permission with the stored data-rights request.

use crate::authorization::{
authorize, AuthorizationContext, AuthorizationError, ProductPermission, ResourceKind,
ResourceScope,
};
use crate::data_rights::DataRightsRequest;

/// Authorize the authenticated participant to manage one stored data-rights request.
///
/// The stored request is the domain record (sometimes called an *aggregate*) that owns the
/// tenant, participant, and request identifiers used here. The function builds the
/// authorization target only from that record, then evaluates [`ProductPermission::ManageOwnDataRights`].
/// It does not trust copies of those identifiers supplied by a caller.
///
/// # Errors
///
/// Returns [`AuthorizationError`] when the authenticated tenant or participant does not own
/// the stored request, participant identity is missing, an identifier is invalid, or another
/// authorization invariant cannot be proven. In all such cases access is denied instead of
/// falling back to a guessed/default identity.
pub fn authorize_data_rights_request(
actor: &AuthorizationContext,
request: &DataRightsRequest,
) -> Result<(), AuthorizationError> {
authorize_bound_data_rights_request(
actor,
request.tenant_ref(),
request.participant_ref(),
request.request_ref(),
)
}
Comment thread
seonghobae marked this conversation as resolved.
Resolved

#[allow(clippy::question_mark)]
fn authorize_bound_data_rights_request(
actor: &AuthorizationContext,
tenant_ref: &str,
participant_ref: &str,
request_ref: &str,
) -> Result<(), AuthorizationError> {
let resource = match ResourceScope::participant_owned(
ResourceKind::DataRightsRequest,
tenant_ref,
participant_ref,
request_ref,
) {
Ok(resource) => resource,
Err(error) => return Err(error),
};
authorize(actor, &resource, ProductPermission::ManageOwnDataRights)
}

#[cfg(test)]
mod tests {
use super::authorize_bound_data_rights_request;
use crate::authorization::{AuthorizationContext, AuthorizationError, ProductRole};

fn actor() -> AuthorizationContext {
AuthorizationContext::new(
"tenant_alpha",
"subject_alpha",
Some("participant_alpha"),
&[ProductRole::Participant],
)
.unwrap()
}

#[test]
fn bound_request_rejects_invalid_resource_references() {
let actor = actor();

for (tenant_ref, participant_ref, request_ref) in [
("", "participant_alpha", "data_rights_request_alpha"),
("tenant_alpha", "", "data_rights_request_alpha"),
("tenant_alpha", "participant_alpha", ""),
] {
assert_eq!(
authorize_bound_data_rights_request(
&actor,
tenant_ref,
participant_ref,
request_ref,
),
Err(AuthorizationError::InvalidReference)
);
}
}

#[test]
fn bound_request_authorizes_the_exact_stored_identity() {
let actor = actor();
assert_eq!(
authorize_bound_data_rights_request(
&actor,
"tenant_alpha",
"participant_alpha",
"data_rights_request_alpha",
),
Ok(())
);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod anonymous_session;
pub mod authorization;
pub mod consent;
pub mod data_rights;
pub mod data_rights_authorization;
pub mod deterministic_narrative;
pub mod health;
pub mod instrument;
Expand Down
66 changes: 66 additions & 0 deletions tests/data_rights_authorization_binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Regression tests for data-rights access bound to the stored request identity.

use psychometrics_commons_runtime::authorization::{
AuthorizationContext, AuthorizationError, ProductRole,
};
use psychometrics_commons_runtime::data_rights::{DataRightsRequest, DataRightsRequestKind};
use psychometrics_commons_runtime::data_rights_authorization::authorize_data_rights_request;

fn request() -> DataRightsRequest {
DataRightsRequest::new(
"data_rights_request_alpha",
"tenant_alpha",
"participant_alpha",
DataRightsRequestKind::Export,
"participant_export_scope_v1",
1_786_240_000_000,
)
.unwrap()
}

fn actor(tenant_ref: &str, participant_ref: Option<&str>) -> AuthorizationContext {
AuthorizationContext::new(
tenant_ref,
"subject_alpha",
participant_ref,
&[ProductRole::Participant],
)
.unwrap()
}

#[test]
fn data_rights_access_uses_tenant_owner_and_identity_from_the_stored_request() {
let actor = actor("tenant_alpha", Some("participant_alpha"));

assert_eq!(authorize_data_rights_request(&actor, &request()), Ok(()));
}

#[test]
fn data_rights_access_rejects_cross_tenant_actor_context() {
let actor = actor("tenant_beta", Some("participant_alpha"));

assert_eq!(
authorize_data_rights_request(&actor, &request()),
Err(AuthorizationError::CrossTenantDenied)
);
}

#[test]
fn data_rights_access_rejects_a_different_authenticated_participant() {
let actor = actor("tenant_alpha", Some("participant_beta"));

assert_eq!(
authorize_data_rights_request(&actor, &request()),
Err(AuthorizationError::OwnerMismatch)
);
}

#[test]
fn data_rights_access_requires_an_operational_participant_identity() {
let actor = actor("tenant_alpha", None);

assert_eq!(
authorize_data_rights_request(&actor, &request()),
Err(AuthorizationError::ParticipantIdentityRequired)
);
}
21 changes: 21 additions & 0 deletions tests/data_rights_authorization_documentation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const AUTHORIZATION_ARCHITECTURE: &str =
include_str!("../docs/architecture/DATA_RIGHTS_AUTHORIZATION.md");

#[test]
fn stored_data_rights_authorization_boundary_stays_documented() {
for required in [
"PARTIAL",
"TRD §11",
"DataRightsRequest",
"src/data_rights_authorization.rs::authorize_data_rights_request",
"ManageOwnDataRights",
"stored request",
"cross-tenant",
"no superseding ADR is required",
Comment thread
seonghobae marked this conversation as resolved.
Resolved
] {
assert!(
AUTHORIZATION_ARCHITECTURE.contains(required),
"data-rights authorization architecture must retain {required:?}"
);
}
}
Loading