Add acquire_token() for cross-resource auth (sync + async) - supersedes #182 - #192
Open
rishi1212 wants to merge 1 commit into
Open
Add acquire_token() for cross-resource auth (sync + async) - supersedes #182#192rishi1212 wants to merge 1 commit into
rishi1212 wants to merge 1 commit into
Conversation
Adds a public, resource-agnostic token helper so callers can reuse the credential the Dataverse client was constructed with to reach other Microsoft Entra ID protected resources -- most commonly a linked Dynamics 365 Finance & Operations environment sitting alongside the same Dataverse org -- without building and consenting a second credential. - `_AuthManager.acquire_token(resource_url) -> str` (sync) - `_AsyncAuthManager.acquire_token(resource_url) -> str` (async, awaitable) Both append the `/.default` scope suffix via a single shared `_build_default_scope()` helper, which trims surrounding whitespace and trailing slashes and raises `ValueError` on blank input so malformed scopes fail locally instead of at the token endpoint. `_ODataClient._headers()` and `_AsyncODataClient._headers()` now route through the same public method, removing the duplicated inline scope construction. Constructor docstrings for both OData clients were updated to document the new `acquire_token(resource_url)` auth contract, and the shared/local auth test doubles were updated to match. Supersedes microsoft#182: rebased onto current main (1.0.1), adds the async client parity requested in review, plus whitespace-trim validation, `_headers()` regression tests for both clients, and `OData.FullAccess` casing in docs. Co-authored-by: Copilot <[email protected]> Copilot-Session: 4b965378-e5d9-4e92-b7ab-ddbe52636253
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a public, resource-agnostic token acquisition helper on both sync and async auth managers so callers can reuse the Dataverse client’s credential to obtain tokens for other Microsoft Entra ID–protected resources (e.g., Dynamics 365 Finance & Operations). Internal OData header construction is refactored to route through this shared entry point, and tests/docs are updated accordingly.
Changes:
- Introduces
_AuthManager.acquire_token(resource_url)and_AsyncAuthManager.acquire_token(resource_url)backed by a shared_build_default_scope()helper (whitespace/trailing-slash normalization + blank validation). - Refactors
_ODataClient._headers()and_AsyncODataClient._headers()to useauth.acquire_token(base_url)instead of inline scope construction. - Updates unit tests and documentation (README + SKILL.md + changelog) and adds regression tests to lock the new
_headers()auth wiring.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/PowerPlatform/Dataverse/core/_auth.py |
Adds _build_default_scope() and sync acquire_token() for cross-resource tokens. |
src/PowerPlatform/Dataverse/aio/core/_async_auth.py |
Adds async acquire_token() and reuses shared scope builder. |
src/PowerPlatform/Dataverse/data/_odata.py |
_headers() now delegates token creation to auth.acquire_token(base_url). |
src/PowerPlatform/Dataverse/aio/data/_async_odata.py |
Async _headers() now awaits auth.acquire_token(base_url). |
tests/unit/core/test_auth.py |
Adds coverage for _build_default_scope() and sync acquire_token(). |
tests/unit/aio/core/test_async_auth.py |
Adds parity coverage for async acquire_token(). |
tests/unit/data/test_odata_internal.py |
Adds sync _headers() regression tests + updates auth mocks. |
tests/unit/aio/data/test_async_odata_internal.py |
Adds async _headers() regression tests + updates auth mocks. |
tests/conftest.py |
Updates shared dummy_auth fixture to include acquire_token(). |
tests/unit/test_operation_context.py |
Updates auth mock to provide acquire_token() return value. |
tests/unit/data/test_upload.py |
Updates auth mock to provide acquire_token() return value. |
tests/unit/data/test_batch_edge_cases.py |
Updates auth mock to provide acquire_token() return value. |
tests/unit/data/test_sql_parse.py |
Extends local DummyAuth with acquire_token() to match new call site. |
tests/unit/data/test_sql_guardrails.py |
Extends local DummyAuth with acquire_token() to match new call site. |
tests/unit/data/test_logical_crud.py |
Extends local DummyAuth with acquire_token() to match new call site. |
tests/unit/data/test_enum_optionset_payload.py |
Extends local DummyAuth with acquire_token() to match new call site. |
tests/unit/core/test_http_errors.py |
Extends local DummyAuth with acquire_token() to match new call site. |
tests/unit/aio/data/test_async_upload.py |
Updates async auth mock to provide awaitable acquire_token(). |
tests/unit/aio/data/test_async_relationships.py |
Updates async auth mock to provide awaitable acquire_token(). |
src/PowerPlatform/Dataverse/claude_skill/dataverse-sdk-use/SKILL.md |
Documents new cross-resource acquire_token() usage. |
.claude/skills/dataverse-sdk-use/SKILL.md |
Keeps the duplicated skill doc in sync with new guidance. |
README.md |
Adds end-user docs for acquiring tokens for other resources (sync + async). |
CHANGELOG.md |
Adds an Unreleased entry describing the new cross-resource token helper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
18
to
+33
| @@ -24,6 +29,9 @@ class Token: | |||
|
|
|||
| return Token() | |||
|
|
|||
| def acquire_token(self, resource_url): | |||
| return self._acquire_token(f"{(resource_url or '').strip().rstrip('/')}/.default").access_token | |||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a public, resource-agnostic token helper so callers can reuse the credential the Dataverse client was constructed with to reach other Microsoft Entra ID protected resources -- most commonly a linked Dynamics 365 Finance & Operations environment sitting alongside the same Dataverse org -- without building and consenting a second credential.
_AuthManager.acquire_token(resource_url) -> str(sync)_AsyncAuthManager.acquire_token(resource_url) -> str(async, awaitable)Both append the
/.defaultscope suffix via a single shared_build_default_scope()helper, which trims surrounding whitespace and trailing slashes and raisesValueErroron blank input so malformed scopes fail locally instead of at the token endpoint._ODataClient._headers()and_AsyncODataClient._headers()now route through the same public method, removing the duplicated inline scope construction. Constructor docstrings for both OData clients were updated to document the newacquire_token(resource_url)auth contract, and the shared/local auth test doubles were updated to match.Supersedes #182: rebased onto current main (1.0.1), adds the async client parity requested in review, plus whitespace-trim validation,
_headers()regression tests for both clients, andOData.FullAccesscasing in docs.