Skip to content

Commit 656c2ec

Browse files
committed
Added documentation and examples
1 parent 0ccbab7 commit 656c2ec

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ from redis_entraid.cred_provider import *
5050

5151
### Step 2 - Create the credential provider via the factory method
5252

53+
Following factory methods are offered depends on authentication type you need:
54+
55+
`create_from_managed_identity` - Creates a credential provider based on a managed identity.
56+
Managed identities allow Azure services to authenticate without needing explicit credentials, as they are automatically assigned by Azure.
57+
58+
`create_from_service_principal` - Creates a credential provider using a service principal.
59+
A service principal is typically used when you want to authenticate as an application, rather than as a user, with Azure Active Directory.
60+
61+
`create_from_default_azure_credential` - Creates a credential provider from a Default Azure Credential.
62+
This method allows automatic selection of the appropriate credential mechanism based on the environment
63+
(e.g., environment variables, managed identities, service principal, interactive browser etc.).
64+
65+
#### Examples ####
66+
67+
**Managed Identity**
68+
69+
```python
70+
credential_provider = create_from_managed_identity(
71+
identity_type=ManagedIdentityType.SYSTEM_ASSIGNED,
72+
resource="https://redis.azure.com/"
73+
)
74+
```
75+
76+
**Service principal**
77+
5378
```python
5479
credential_provider = create_from_service_principal(
5580
CLIENT_ID,
@@ -58,6 +83,14 @@ credential_provider = create_from_service_principal(
5883
)
5984
```
6085

86+
**Default Azure Credential**
87+
88+
```python
89+
credential_provider = create_from_default_azure_credential(
90+
("https://redis.azure.com/.default",),
91+
)
92+
```
93+
6194
### Step 3 - Provide optional token renewal configuration
6295

6396
The default configuration would be applied, but you're able to customise it.

examples/__init__.py

Whitespace-only changes.

examples/interactive_browser_login.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Before run this example you need to configure your EntraID application the following way:
2+
#
3+
# 1. Enable "Allow public client flows" option, under "Authentication" section.
4+
# 2. Add the Redirect URL of the web server that DefaultAzureCredential runs
5+
# By default, uses port 8400, so the default Redirect URL looks like "http://localhost:8400".
6+
7+
import os
8+
9+
from redis_entraid.cred_provider import create_from_default_azure_credential
10+
11+
def main():
12+
13+
# By default, interactive browser login is excluded so you need to enable it.
14+
credential_provider = create_from_default_azure_credential(
15+
("user.read",),
16+
app_kwargs={
17+
"exclude_interactive_browser_credential": False,
18+
"interactive_browser_client_id": os.getenv("AZURE_CLIENT_ID"),
19+
"interactive_browser_tenant_id": os.getenv("AZURE_TENANT_ID"),
20+
}
21+
)
22+
23+
# Opens a browser tab. After you'll enter your username/password it would return you a credentials needed for auth.
24+
print(credential_provider.get_credentials())
25+
26+
27+
if __name__ == "__main__":
28+
main()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ dependencies = [
2020

2121
[tool.setuptools.packages.find]
2222
include = ["redis_entraid"]
23-
exclude = ["tests", ".github"]
23+
exclude = ["tests", "examples", ".github"]

redis_entraid/cred_provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def create_from_service_principal(
164164

165165
def create_from_default_azure_credential(
166166
scopes: Tuple[str],
167-
additional_tenant_id: Optional[str] = None,
167+
tenant_id: Optional[str] = None,
168168
authority: Optional[str] = None,
169169
token_kwargs: Optional[dict] = {},
170170
app_kwargs: Optional[dict] = {},
@@ -183,7 +183,7 @@ def create_from_default_azure_credential(
183183
https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
184184
185185
:param scopes: Service principal scopes. Fallback to default scopes if None.
186-
:param additional_tenant_id: Optional tenant to include in the token request.
186+
:param tenant_id: Optional tenant to include in the token request.
187187
:param authority: Custom authority, by default used 'login.microsoftonline.com'
188188
:param token_kwargs: Optional token arguments applied when retrieving tokens.
189189
:param app_kwargs: Optional keyword arguments to pass when instantiating application.
@@ -192,7 +192,7 @@ def create_from_default_azure_credential(
192192
default_azure_credential_config = DefaultAzureCredentialIdentityProviderConfig(
193193
scopes=scopes,
194194
authority=authority,
195-
additional_tenant_id=additional_tenant_id,
195+
additional_tenant_id=tenant_id,
196196
token_kwargs=token_kwargs,
197197
app_kwargs=app_kwargs,
198198
)

0 commit comments

Comments
 (0)