Skip to content

API Reference parrot.services.identity_mapping

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.services.identity_mapping

View source

IdentityMappingService — CRUD for navigator-auth user_identities.

Links navigator-auth internal user IDs with external provider identities (Telegram numeric IDs, Teams AAD object ids, Jira account IDs, etc.).

Schema facts that shape this module (auth.user_identities, navigator-auth >= 0.28.8):

  • PK is identity_id (uuid, gen_random_uuid()); there is no uid.
  • user_id is bigint with a deferred FK to auth.users.
  • The only unique index usable as an upsert arbiter is the PARTIAL index UNIQUE (user_id, auth_provider, provider_user_id) WHERE provider_user_id IS NOT NULL. (user_id, auth_provider) is NOT unique (one user may link several accounts of a provider), so the old ON CONFLICT (user_id, auth_provider) failed with 42P10 on every call.

Two write paths:

  • channel mappings (msteams/slack/telegram) carry no credential and are written with raw SQL below;
  • token-bearing providers (jira) go through navigator-auth's :class:IdentityStore, which ciphers the credential.

Class ClaimResult

Outcome of :meth:IdentityMappingService.claim_channel_identity (never carries the other owner).

Fields:

Field Type Default Description
status Literal['claimed', 'already_owned', 'conflict']

Class IdentityMappingService

CRUD service for auth.user_identities records.

Parameter Type Default Description
db_pool Any An asyncpg-compatible connection pool (typically the navigator-auth authdb pool obtained from app.get("authdb")).
identity_store Optional['IdentityStore'] None

Method IdentityMappingService.claim_channel_identity

Exclusively claim (auth_provider, provider_user_id) for nav_user_id (FEAT-777 M2).

One transaction: advisory lock on the identity key, read every matching row (disabled included), insert only when nobody owns it. Same enabled owner is idempotent. Anything else is a conflict, no write.

claim_channel_identity(*, nav_user_id: str, auth_provider: str, provider_user_id: str, auth_data: Dict[str, Any], display_name: Optional[str] = None, email: Optional[str] = None) -> ClaimResult

Parameter Type Default Description
nav_user_id str
auth_provider str
provider_user_id str
auth_data Dict[str, Any]
display_name Optional[str] None
email Optional[str] None

Returns: ClaimResult

Raises:

  • ValueError — non-numeric nav_user_id, empty provider_user_id or secrets in auth_data.
  • Exception — database failures propagate.

Method IdentityMappingService.upsert_identity

Write one identity row.

provider_user_id is REQUIRED: the only usable unique index is partial on it being non-NULL, so a NULL re-introduces the 42P10. When token is given the write goes through IdentityStore so the credential is ciphered; otherwise it is a metadata-only channel mapping and goes through the SQL above.

upsert_identity(*, nav_user_id: str, auth_provider: str, provider_user_id: str, auth_data: Dict[str, Any], display_name: Optional[str] = None, email: Optional[str] = None, token: Optional['TokenResponse'] = None) -> None

Parameter Type Default Description
nav_user_id str navigator-auth user_id (PK of auth.users).
auth_provider str Provider key (e.g., "telegram", "jira").
provider_user_id str The account id on the provider side.
auth_data Dict[str, Any] Non-secret provider payload (serialized to JSONB).
display_name Optional[str] None Optional display name override.
email Optional[str] None Optional email override.
token Optional['TokenResponse'] None Credential for token-bearing providers.

Raises:

  • ValueError — If nav_user_id is not the numeric navigator-auth user id, provider_user_id is empty, or auth_data carries a secret.
  • Exception — Any database/cipher failure propagates — callers decide how to surface it.

Method IdentityMappingService.get_identity

Fetch a single identity record by (user_id, provider).

get_identity(nav_user_id: str, auth_provider: str) -> Optional[Dict[str, Any]]

Parameter Type Default Description
nav_user_id str navigator-auth user_id.
auth_provider str Provider key.

Returns: Optional[Dict[str, Any]] — A dict with auth_data, display_name, email, identity_id, created_at, or None if no record exists.

Method IdentityMappingService.get_all_identities

List all identity records for a user.

get_all_identities(nav_user_id: str) -> List[Dict[str, Any]]

Parameter Type Default Description
nav_user_id str navigator-auth user_id.

Returns: List[Dict[str, Any]] — List of identity dicts (empty if no identities are linked).

Method IdentityMappingService.delete_identity

Remove the identity record for (user_id, provider).

No-op if no matching row exists.

delete_identity(nav_user_id: str, auth_provider: str) -> None

Parameter Type Default Description
nav_user_id str
auth_provider str

Method IdentityMappingService.link_channel_identity

Make nav_user_id the sole owner of a channel account, atomically.

One transaction: advisory lock, delete rows owned by other users, upsert.

link_channel_identity(*, nav_user_id: str, auth_provider: str, provider_user_id: str, auth_data: Dict[str, Any], display_name: Optional[str] = None, email: Optional[str] = None) -> None

Parameter Type Default Description
nav_user_id str
auth_provider str
provider_user_id str
auth_data Dict[str, Any]
display_name Optional[str] None
email Optional[str] None

Raises:

  • ValueError — invalid input (same rules as upsert_identity) or secrets in auth_data.
  • RuntimeError — the upsert returned no row (the transaction is rolled back).

Method IdentityMappingService.unlink_channel_identity

Delete the rows of a channel account under the shared lock.

unlink_channel_identity(auth_provider: str, provider_user_id: str, expected_user_id: Optional[str] = None) -> int

Parameter Type Default Description
auth_provider str Channel provider name (e.g. telegram).
provider_user_id str The channel account id.
expected_user_id Optional[str] None When given, delete only a row owned by this Navigator user, so a stale session of a previous owner cannot remove a link that has since moved to someone else.

Returns: int — The number of deleted rows (0 when absent).

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally