|
| 1 | +[](https://dev.azure.com/azure-sdk/public/_build/latest?definitionId=46?branchName=master) |
| 2 | + |
| 3 | +# Azure Mixed Reality Authentication Package client library for Python |
| 4 | + |
| 5 | +Mixed Reality services, like Azure Spatial Anchors, Azure Remote Rendering, and others, use the Mixed Reality security |
| 6 | +token service (STS) for authentication. This package supports exchanging Mixed Reality account credentials for an access |
| 7 | +token from the STS that can be used to access Mixed Reality services. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# Getting started |
| 12 | + |
| 13 | +## Currently supported environments |
| 14 | + |
| 15 | +This package has been tested with Python 2.7, 3.5, 3.6, 3.7, 3.8, and 3.9. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- An [Azure subscription][azure_sub]. |
| 20 | +- You must have an account with an [Azure Mixed Reality service](https://azure.microsoft.com/topic/mixed-reality/): |
| 21 | + - [Azure Remote Rendering](https://docs.microsoft.com/azure/remote-rendering/) |
| 22 | + - [Azure Spatial Anchors](https://docs.microsoft.com/azure/spatial-anchors/) |
| 23 | +- Familiarity with the authentication and credential concepts from the [Azure Identity library][azure_identity]. |
| 24 | +- Python 2.7, or 3.5 or later is required to use this package. |
| 25 | + |
| 26 | +## Install the package |
| 27 | + |
| 28 | +Install the Azure Mixed Reality Authentication SDK. |
| 29 | + |
| 30 | +```bash |
| 31 | +pip install --pre azure-mixedreality-authentication |
| 32 | +``` |
| 33 | + |
| 34 | +## Create and authenticate a `MixedRealityStsClient` |
| 35 | + |
| 36 | +To create a client object to request an access token for a Mixed Reality service, you will need the `account identifier` |
| 37 | +and `account domain` of your Mixed Reality service resource and a `credential`. |
| 38 | + |
| 39 | +Mixed Reality services support a few different forms of authentication: |
| 40 | + |
| 41 | +- Account Key authentication |
| 42 | + - Account keys enable you to get started quickly with using Mixed Reality services. But before you deploy your application |
| 43 | + to production, we recommend that you update your app to use Azure AD authentication. |
| 44 | +- Azure Active Directory (AD) token authentication |
| 45 | + - If you're building an enterprise application and your company is using Azure AD as its identity system, you can use |
| 46 | + user-based Azure AD authentication in your app. You then grant access to your Mixed Reality accounts by using your |
| 47 | + existing Azure AD security groups. You can also grant access directly to users in your organization. |
| 48 | + - Otherwise, we recommend that you obtain Azure AD tokens from a web service that supports your app. We recommend this |
| 49 | + method for production applications because it allows you to avoid embedding the credentials for access to a Mixed |
| 50 | + Reality service in your client application. |
| 51 | + |
| 52 | +See [here][register_aad_app] for detailed instructions and information. |
| 53 | + |
| 54 | +### Using account key authentication |
| 55 | + |
| 56 | +Use the [Azure Portal][azure_portal] to browse to your Mixed Reality service resource and retrieve an `account key`. |
| 57 | + |
| 58 | +Once you have an account key, you can use the `AzureKeyCredential` class to authenticate the client as follows: |
| 59 | + |
| 60 | +```python |
| 61 | +from azure.core.credentials import AzureKeyCredential |
| 62 | +from azure.mixedreality.authentication import MixedRealityStsClient |
| 63 | + |
| 64 | +account_id = "<ACCOUNTD ID>" |
| 65 | +account_domain = "<ACCOUNT_DOMAIN>" |
| 66 | +account_key = "<ACCOUNT_KEY>" |
| 67 | +key_credential = AzureKeyCredential(account_key) |
| 68 | + |
| 69 | +client = MixedRealityStsClient(account_id, account_domain, key_credential) |
| 70 | +``` |
| 71 | + |
| 72 | +> Note: Account key authentication is **not recommended** for production applications. |
| 73 | +
|
| 74 | +### Using an Azure Active Directory Credential |
| 75 | + |
| 76 | +Account key authentication is used in most of the examples, but you can also authenticate with Azure Active Directory |
| 77 | +using the [Azure Identity library][azure_identity]. This is the recommended method for production applications. To use |
| 78 | +the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with |
| 79 | +the Azure SDK, please install the `@azure/identity` package: |
| 80 | + |
| 81 | +You will also need to [register a new AAD application][register_aad_app] and grant access to your Mixed Reality resource |
| 82 | +by assigning the appropriate role for your Mixed Reality service to your service principal. |
| 83 | + |
| 84 | +```python |
| 85 | +from azure.identity import DefaultAzureCredential |
| 86 | +from azure.mixedreality.authentication import MixedRealityStsClient |
| 87 | + |
| 88 | +account_id = "<ACCOUNTD ID>" |
| 89 | +account_domain = "<ACCOUNT_DOMAIN>" |
| 90 | +default_credential = DefaultAzureCredential() |
| 91 | + |
| 92 | +client = MixedRealityStsClient(account_id, account_domain, default_credential) |
| 93 | +``` |
| 94 | + |
| 95 | +# Key concepts |
| 96 | + |
| 97 | +## MixedRealityStsClient |
| 98 | + |
| 99 | +The `MixedRealityStsClient` is the client library used to access the Mixed Reality STS to get an access token. An access |
| 100 | +token can be retrieved by calling `get_token()` on an `MixedRealityStsClient` instance. |
| 101 | + |
| 102 | +Tokens obtained from the Mixed Reality STS have a lifetime of **24 hours**. |
| 103 | + |
| 104 | +### Token result value |
| 105 | + |
| 106 | +The return value for a successful call to `get_token` is an `azure.core.credentials.AccessToken`. |
| 107 | + |
| 108 | +See the authentication examples [above](#authenticate-the-client) or [Azure Identity][azure_identity] for more complex |
| 109 | +authentication scenarios. |
| 110 | + |
| 111 | +## Retrieve an access token synchronously |
| 112 | + |
| 113 | +```python |
| 114 | +from azure.core.credentials import AzureKeyCredential |
| 115 | +from azure.mixedreality.authentication import MixedRealityStsClient |
| 116 | + |
| 117 | +account_id = "<ACCOUNTD ID>" |
| 118 | +account_domain = "<ACCOUNT_DOMAIN>" |
| 119 | +account_key = "<ACCOUNT_KEY>" |
| 120 | +key_credential = AzureKeyCredential(account_key) |
| 121 | + |
| 122 | +client = MixedRealityStsClient(account_id, account_domain, key_credential) |
| 123 | + |
| 124 | +token = client.get_token() |
| 125 | +``` |
| 126 | + |
| 127 | +## Retrieve an access token asynchronously |
| 128 | + |
| 129 | +```python |
| 130 | +from azure.core.credentials import AzureKeyCredential |
| 131 | +from azure.mixedreality.authentication.aio import MixedRealityStsClient |
| 132 | + |
| 133 | +account_id = "<ACCOUNTD ID>" |
| 134 | +account_domain = "<ACCOUNT_DOMAIN>" |
| 135 | +account_key = "<ACCOUNT_KEY>" |
| 136 | +key_credential = AzureKeyCredential(account_key) |
| 137 | + |
| 138 | +client = MixedRealityStsClient(account_id, account_domain, key_credential) |
| 139 | + |
| 140 | +token = await client.get_token() |
| 141 | +``` |
| 142 | + |
| 143 | +# Examples |
| 144 | + |
| 145 | +These are code samples that show common scenario operations with the Azure Mixed Reality Authentication client library. |
| 146 | +The async versions of the samples (the python sample files appended with `_async`) show asynchronous operations, |
| 147 | +and require Python 3.5 or later. |
| 148 | +Before running the sample code, refer to Prerequisites |
| 149 | +<!-- [Prerequisites](#Prerequisites) --> |
| 150 | +to create a resource, then set some Environment Variables |
| 151 | + |
| 152 | +```bash |
| 153 | +set MIXEDREALITY_ACCOUNT_DOMAIN="<the Mixed Reality account domain>" |
| 154 | +set MIXEDREALITY_ACCOUNT_ID="<the Mixed Reality account identifier>" |
| 155 | +set MIXEDREALITY_ACCOUNT_KEY="<the Mixed Reality account primary or secondary key>" |
| 156 | + |
| 157 | +pip install azure-mixedreality-authentication |
| 158 | + |
| 159 | +python samples\client_sample.py |
| 160 | +python samples\client_sample_async.py |
| 161 | +``` |
| 162 | + |
| 163 | +# Troubleshooting |
| 164 | + |
| 165 | +The [troubleshooting](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity#troubleshooting) |
| 166 | +section for Azure Identity can be helpful when troubleshooting authentication issues. |
| 167 | + |
| 168 | +# Next steps |
| 169 | + |
| 170 | +## Mixed Reality client libraries |
| 171 | + |
| 172 | +- Coming soon |
| 173 | + |
| 174 | +## Contributing |
| 175 | + |
| 176 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a |
| 177 | +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us |
| 178 | +the rights to use your contribution. For details, visit https://cla.microsoft.com. |
| 179 | + |
| 180 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide |
| 181 | +a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions |
| 182 | +provided by the bot. You will only need to do this once across all repos using our CLA. |
| 183 | + |
| 184 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 185 | +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or |
| 186 | +contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 187 | + |
| 188 | +If you'd like to contribute to this library, please read the |
| 189 | +[contributing guide](https://github.com/Azure/azure-sdk-for-python/blob/master/CONTRIBUTING.md) to learn more about how to |
| 190 | +build and test the code. |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +[azure_cli]: https://docs.microsoft.com/cli/azure |
| 195 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 196 | +[azure_portal]: https://portal.azure.com |
| 197 | +[azure_identity]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity |
| 198 | +[register_aad_app]: https://docs.microsoft.com/azure/spatial-anchors/concepts/authentication |
| 199 | +[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity#defaultazurecredential |
0 commit comments