Skip to content
18 changes: 18 additions & 0 deletions .claude/skills/dataverse-sdk-use/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ with DataverseClient("https://yourorg.crm.dynamics.com", credential) as client:
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

### Acquiring Tokens for Other Microsoft Resources

`client.auth.acquire_token(resource_url)` returns an OAuth2 access token from the **same credential** for any Microsoft Entra ID protected resource — for example a linked Dynamics 365 Finance & Operations environment. Use it instead of building a second credential. The `/.default` scope is appended automatically.

```python
# Sync client
fno_token = client.auth.acquire_token("https://myenv.operations.dynamics.com")

# Use the token to call the ERP OData / Custom Service endpoints directly
headers = {"Authorization": f"Bearer {fno_token}"}

# Async client
async with AsyncDataverseClient("https://yourorg.crm.dynamics.com", credential) as aclient:
fno_token = await aclient.auth.acquire_token("https://myenv.operations.dynamics.com")
```

Pass the bare resource URL — trailing slashes and surrounding whitespace are trimmed, and a blank value raises `ValueError`. The app registration must already hold the required permission on the target resource with admin consent granted. For Finance & Operations the standard delegated permissions are `OData.FullAccess` and `CustomService.FullAccess` on the **Microsoft Dynamics ERP** API (`00000015-0000-0000-c000-000000000000`).

### CRUD Operations

#### Create Records
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- **Cross-resource token acquisition** — `client.auth.acquire_token(resource_url)` (and `await client.auth.acquire_token(resource_url)` on `AsyncDataverseClient`) returns an OAuth2 access token for any Microsoft Entra ID protected resource using the same credential the Dataverse client was constructed with — for example a linked Dynamics 365 Finance & Operations environment. The `/.default` scope is appended automatically; token caching and refresh remain the credential's responsibility. The internal Dataverse request path now routes through the same method, removing the duplicated inline scope construction in `_ODataClient._headers()` and `_AsyncODataClient._headers()`.
## [1.0.1] - 2026-09-08

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ with DataverseClient("https://yourorg.crm.dynamics.com", credential) as client:

More on reading and writing records: [Work with Dataverse data](https://learn.microsoft.com/power-apps/developer/data-platform/sdk-python/work-data) and the [walkthrough.py](https://github.com/microsoft/PowerPlatform-DataverseClient-Python/blob/main/examples/advanced/walkthrough.py) sample.

### Acquire tokens for other resources

Reuse the client's credential for another Microsoft Entra ID protected resource, such as Finance & Operations. Pass the resource URL without a scope suffix; `/.default` is appended automatically. The app must already have the target resource's required permissions and consent.

```python
fno_token = client.auth.acquire_token("https://myenv.operations.dynamics.com")
# With AsyncDataverseClient: await client.auth.acquire_token(resource_url)
```

### Query records

`client.query.builder()` builds type-safe OData for you — filters use `col()` with standard Python operators, and it escapes values automatically. Results are iterable and can be handed straight to pandas with `.to_dataframe()`.
Expand Down
49 changes: 45 additions & 4 deletions src/PowerPlatform/Dataverse/aio/core/_async_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@

This module provides :class:`~PowerPlatform.Dataverse.aio.core._async_auth._AsyncAuthManager`,
a thin wrapper over any Azure Identity ``AsyncTokenCredential`` for acquiring OAuth2 access
tokens asynchronously, and reuses :class:`~PowerPlatform.Dataverse.core._auth._TokenPair` for
storing the acquired token alongside its scope.
tokens asynchronously for Microsoft Entra ID protected resources -- Dataverse by default, and
any other resource (for example a linked Dynamics 365 Finance & Operations environment) when a
different resource URL is supplied -- and reuses
:class:`~PowerPlatform.Dataverse.core._auth._TokenPair` for storing the acquired token alongside
its scope.
"""

from __future__ import annotations

from azure.core.credentials_async import AsyncTokenCredential

from ...core._auth import _TokenPair
from ...core._auth import _TokenPair, _build_default_scope


class _AsyncAuthManager:
"""
Azure Identity-based async authentication manager for Dataverse.
Azure Identity-based async authentication manager.

Async counterpart to :class:`~PowerPlatform.Dataverse.core._auth._AuthManager` with the same
resource-agnostic surface: the resource URL passed to :meth:`acquire_token` selects the target
resource. The async Dataverse client supplies its own organization URL on every internal
request, and the same method can be awaited by application code (through
``await client.auth.acquire_token(...)``) to obtain tokens for other Microsoft Entra ID
protected resources -- for example a linked Dynamics 365 Finance & Operations environment.

:param credential: Azure Identity async credential implementation.
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
Expand All @@ -43,3 +53,34 @@ async def _acquire_token(self, scope: str) -> _TokenPair:
"""
token = await self.credential.get_token(scope)
return _TokenPair(resource=scope, access_token=token.token)

async def acquire_token(self, resource_url: str) -> str:
"""
Acquire an OAuth2 access token asynchronously for a Microsoft Entra ID protected resource.

Async counterpart of :meth:`~PowerPlatform.Dataverse.core._auth._AuthManager.acquire_token`.
Resource-agnostic helper: pass the resource URL (the Dataverse environment URL for
Dataverse, the Finance & Operations environment URL for ERP, and so on) and the
``/.default`` scope suffix is appended automatically before delegating to the underlying
credential. Token caching, refresh, and silent reauthentication remain the credential's
responsibility; Azure Identity credentials cache in memory by default, so repeated calls
are cheap.

:param resource_url: Resource URL for the target Microsoft service (for example
``"https://myenv.operations.dynamics.com"``). Surrounding whitespace and trailing
slashes are removed before scope construction.
:type resource_url: :class:`str`
:return: OAuth2 access token string suitable for an ``Authorization: Bearer <token>`` header.
:rtype: :class:`str`
:raises ValueError: If ``resource_url`` is empty after trimming whitespace and trailing slashes.
:raises ~azure.core.exceptions.ClientAuthenticationError: If token acquisition fails.

Example:
Acquire a token for a linked Finance & Operations environment using the same credential
the async Dataverse client was built with::

async with AsyncDataverseClient(dataverse_url, credential) as client:
fno_token = await client.auth.acquire_token("https://myenv.operations.dynamics.com")
"""
pair = await self._acquire_token(_build_default_scope(resource_url))
return pair.access_token
5 changes: 2 additions & 3 deletions src/PowerPlatform/Dataverse/aio/data/_async_odata.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(

Sets up authentication, base URL, configuration, and internal caches.

:param auth: Async authentication manager providing ``_acquire_token(scope)`` that returns an object with ``access_token``.
:param auth: Async authentication manager exposing awaitable ``acquire_token(resource_url)`` that returns the OAuth2 access token string for the given resource. ``_headers()`` awaits ``auth.acquire_token(self.base_url)`` so the token is scoped to this OData client's Dataverse organization.
:type auth: ~PowerPlatform.Dataverse.aio.core._async_auth._AsyncAuthManager
:param base_url: Organization base URL (e.g. ``"https://<org>.crm.dynamics.com"``).
:type base_url: ``str``
Expand Down Expand Up @@ -96,8 +96,7 @@ async def close(self) -> None:

async def _headers(self) -> Dict[str, str]:
"""Build standard OData headers with bearer auth."""
scope = f"{self.base_url}/.default"
token = (await self.auth._acquire_token(scope)).access_token
token = await self.auth.acquire_token(self.base_url)
ua = f"{_USER_AGENT} ({self._operation_context})" if self._operation_context else _USER_AGENT
return {
"Authorization": f"Bearer {token}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ with DataverseClient("https://yourorg.crm.dynamics.com", credential) as client:
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

### Acquiring Tokens for Other Microsoft Resources

`client.auth.acquire_token(resource_url)` returns an OAuth2 access token from the **same credential** for any Microsoft Entra ID protected resource — for example a linked Dynamics 365 Finance & Operations environment. Use it instead of building a second credential. The `/.default` scope is appended automatically.

```python
# Sync client
fno_token = client.auth.acquire_token("https://myenv.operations.dynamics.com")

# Use the token to call the ERP OData / Custom Service endpoints directly
headers = {"Authorization": f"Bearer {fno_token}"}

# Async client
async with AsyncDataverseClient("https://yourorg.crm.dynamics.com", credential) as aclient:
fno_token = await aclient.auth.acquire_token("https://myenv.operations.dynamics.com")
```

Pass the bare resource URL — trailing slashes and surrounding whitespace are trimmed, and a blank value raises `ValueError`. The app registration must already hold the required permission on the target resource with admin consent granted. For Finance & Operations the standard delegated permissions are `OData.FullAccess` and `CustomService.FullAccess` on the **Microsoft Dynamics ERP** API (`00000015-0000-0000-c000-000000000000`).

### CRUD Operations

#### Create Records
Expand Down
62 changes: 59 additions & 3 deletions src/PowerPlatform/Dataverse/core/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
Authentication helpers for Dataverse.

This module provides :class:`~PowerPlatform.Dataverse.core._auth._AuthManager`, a thin wrapper over any Azure Identity
``TokenCredential`` for acquiring OAuth2 access tokens, and :class:`~PowerPlatform.Dataverse.core._auth._TokenPair` for
storing the acquired token alongside its scope.
``TokenCredential`` for acquiring OAuth2 access tokens for Microsoft Entra ID protected resources -- Dataverse by
default, and any other resource (for example a linked Dynamics 365 Finance & Operations environment) when a different
resource URL is supplied -- and :class:`~PowerPlatform.Dataverse.core._auth._TokenPair` for storing the acquired token
alongside its scope.
"""

from __future__ import annotations
Expand All @@ -15,6 +17,28 @@

from azure.core.credentials import TokenCredential

#: Scope suffix appended to a resource URL to request the resource's default (statically consented) permission set.
_DEFAULT_SCOPE_SUFFIX = "/.default"


def _build_default_scope(resource_url: str) -> str:
"""
Build the ``<resource>/.default`` OAuth2 scope for a resource URL.

Shared by the sync and async auth managers so scope construction lives in exactly one place.

:param resource_url: Resource URL for the target Microsoft service (e.g. ``"https://<org>.crm.dynamics.com"``).
Surrounding whitespace and trailing slashes are removed before the suffix is appended.
:type resource_url: :class:`str`
:return: The ``/.default`` scope string for ``resource_url``.
:rtype: :class:`str`
:raises ValueError: If ``resource_url`` is empty, whitespace-only, or only slashes.
"""
target = (resource_url or "").strip().rstrip("/")
if not target:
raise ValueError("resource_url must not be empty.")
return f"{target}{_DEFAULT_SCOPE_SUFFIX}"


@dataclass(repr=False)
class _TokenPair:
Expand Down Expand Up @@ -44,7 +68,12 @@ def __repr__(self) -> str:

class _AuthManager:
"""
Azure Identity-based authentication manager for Dataverse.
Azure Identity-based authentication manager.

Resource-agnostic: the resource URL passed to :meth:`acquire_token` selects the target resource. The Dataverse
client supplies its own organization URL on every internal request, and the same method can be called by
application code (through ``client.auth.acquire_token(...)``) to obtain tokens for other Microsoft Entra ID
protected resources -- for example a linked Dynamics 365 Finance & Operations environment.

:param credential: Azure Identity credential implementation.
:type credential: ~azure.core.credentials.TokenCredential
Expand All @@ -68,3 +97,30 @@ def _acquire_token(self, scope: str) -> _TokenPair:
"""
token = self.credential.get_token(scope)
return _TokenPair(resource=scope, access_token=token.token)

def acquire_token(self, resource_url: str) -> str:
"""
Acquire an OAuth2 access token for a Microsoft Entra ID protected resource.

Resource-agnostic helper: pass the resource URL (the Dataverse environment URL for Dataverse, the Finance &
Operations environment URL for ERP, and so on) and the ``/.default`` scope suffix is appended automatically
before delegating to the underlying credential. Token caching, refresh, and silent reauthentication remain the
credential's responsibility; Azure Identity credentials cache in memory by default, so repeated calls are cheap.

:param resource_url: Resource URL for the target Microsoft service (for example
``"https://myenv.operations.dynamics.com"``). Surrounding whitespace and trailing slashes are removed
before scope construction.
:type resource_url: :class:`str`
:return: OAuth2 access token string suitable for an ``Authorization: Bearer <token>`` header.
:rtype: :class:`str`
:raises ValueError: If ``resource_url`` is empty after trimming whitespace and trailing slashes.
:raises ~azure.core.exceptions.ClientAuthenticationError: If token acquisition fails.

Example:
Acquire a token for a linked Finance & Operations environment using the same credential the Dataverse
client was built with::

client = DataverseClient(dataverse_url, credential)
fno_token = client.auth.acquire_token("https://myenv.operations.dynamics.com")
"""
return self._acquire_token(_build_default_scope(resource_url)).access_token
5 changes: 2 additions & 3 deletions src/PowerPlatform/Dataverse/data/_odata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(

Sets up authentication, base URL, configuration, and internal caches.

:param auth: Authentication manager providing ``_acquire_token(scope)`` that returns an object with ``access_token``.
:param auth: Authentication manager exposing ``acquire_token(resource_url)`` that returns the OAuth2 access token string for the given resource. ``_headers()`` calls ``auth.acquire_token(self.base_url)`` so the token is scoped to this OData client's Dataverse organization.
:type auth: ~PowerPlatform.Dataverse.core._auth._AuthManager
:param base_url: Organization base URL (e.g. ``"https://<org>.crm.dynamics.com"``).
:type base_url: ``str``
Expand Down Expand Up @@ -94,8 +94,7 @@ def close(self) -> None:

def _headers(self) -> Dict[str, str]:
"""Build standard OData headers with bearer auth."""
scope = f"{self.base_url}/.default"
token = self.auth._acquire_token(scope).access_token
token = self.auth.acquire_token(self.base_url)
ua = _USER_AGENT
if self._operation_context:
ua = f"{_USER_AGENT} ({self._operation_context})"
Expand Down
13 changes: 12 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@

import pytest
from unittest.mock import Mock
from PowerPlatform.Dataverse.core._auth import _build_default_scope
from PowerPlatform.Dataverse.core.config import DataverseConfig


@pytest.fixture
def dummy_auth():
"""Mock authentication object for testing."""
"""Mock authentication object for testing.

Mirrors the real ``_AuthManager`` surface: both the internal
``_acquire_token(scope)`` and the public ``acquire_token(resource_url)``
used by ``_ODataClient._headers()``. Scope construction reuses the
production ``_build_default_scope`` helper so the double preserves its
normalization and ``ValueError`` validation instead of drifting from it.
"""

class DummyAuth:
def _acquire_token(self, scope):
Expand All @@ -24,6 +32,9 @@ class Token:

return Token()

def acquire_token(self, resource_url):
return self._acquire_token(_build_default_scope(resource_url)).access_token

return DummyAuth()


Expand Down
61 changes: 61 additions & 0 deletions tests/unit/aio/core/test_async_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,64 @@ async def test_acquire_token_different_scope(self):
await manager._acquire_token("https://example.crm10.dynamics.com/.default")

mock_cred.get_token.assert_called_once_with("https://example.crm10.dynamics.com/.default")


class TestAsyncAuthManagerAcquireToken:
"""Tests for the public, resource-agnostic ``_AsyncAuthManager.acquire_token``.

Mirrors ``tests/unit/core/test_auth.py::TestAuthManagerAcquireToken`` so the async
client keeps parity with the sync client for cross-resource token acquisition.
"""

async def test_appends_default_scope_and_returns_token_string(self):
"""acquire_token appends /.default to the resource URL and returns the access token string."""
mock_cred = MagicMock(spec=AsyncTokenCredential)
mock_cred.get_token = AsyncMock(return_value=MagicMock(token="dv-token"))

manager = _AsyncAuthManager(mock_cred)
result = await manager.acquire_token("https://org.crm.dynamics.com")

mock_cred.get_token.assert_called_once_with("https://org.crm.dynamics.com/.default")
assert result == "dv-token"

async def test_strips_trailing_slash(self):
"""acquire_token strips trailing slashes before constructing the scope."""
mock_cred = MagicMock(spec=AsyncTokenCredential)
mock_cred.get_token = AsyncMock(return_value=MagicMock(token="t"))

manager = _AsyncAuthManager(mock_cred)
await manager.acquire_token("https://myenv.operations.dynamics.com/")

mock_cred.get_token.assert_called_once_with("https://myenv.operations.dynamics.com/.default")

async def test_strips_surrounding_whitespace(self):
"""acquire_token trims whitespace so a padded URL still yields a well-formed scope."""
mock_cred = MagicMock(spec=AsyncTokenCredential)
mock_cred.get_token = AsyncMock(return_value=MagicMock(token="t"))

manager = _AsyncAuthManager(mock_cred)
await manager.acquire_token(" https://myenv.operations.dynamics.com/ ")

mock_cred.get_token.assert_called_once_with("https://myenv.operations.dynamics.com/.default")

async def test_supports_alternate_resource(self):
"""acquire_token works for any resource URL (for example a linked Finance & Operations env)."""
mock_cred = MagicMock(spec=AsyncTokenCredential)
mock_cred.get_token = AsyncMock(return_value=MagicMock(token="fno-token"))

manager = _AsyncAuthManager(mock_cred)
result = await manager.acquire_token("https://myenv.operations.dynamics.com")

mock_cred.get_token.assert_called_once_with("https://myenv.operations.dynamics.com/.default")
assert result == "fno-token"

@pytest.mark.parametrize("bad", ["", " ", "/", " // ", None])
async def test_blank_url_raises_without_calling_credential(self, bad):
"""Blank input fails locally with ValueError instead of requesting a malformed scope."""
mock_cred = MagicMock(spec=AsyncTokenCredential)
mock_cred.get_token = AsyncMock()
manager = _AsyncAuthManager(mock_cred)

with pytest.raises(ValueError):
await manager.acquire_token(bad)
mock_cred.get_token.assert_not_called()
Loading
Loading