Skip to content

Commit f032ba9

Browse files
authored
User authentication samples (#11343)
1 parent 97dc978 commit f032ba9

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
recursive-include samples *.py
12
recursive-include tests *.py
23
include *.md
3-
include azure/__init__.py
4+
include azure/__init__.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
- azure-identity
8+
urlFragment: identity-samples
9+
---
10+
11+
# Azure Identity Library Python Samples
12+
13+
## Prerequisites
14+
15+
You must have an [Azure subscription](https://azure.microsoft.com/free) and an
16+
[Azure Key Vault](https://azure.microsoft.com/en-us/services/key-vault/) to run
17+
these samples. You can create a Key Vault in the
18+
[Azure Portal](https://portal.azure.com/#create/Microsoft.KeyVault) or with the
19+
[Azure CLI](https://docs.microsoft.com/en-us/azure/key-vault/secrets/quick-create-cli).
20+
21+
Azure Key Vault is used only to demonstrate authentication. Azure Identity has
22+
the same API for all compatible client libraries.
23+
24+
## Setup
25+
26+
To run these samples, first install the Azure Identity and Key Vault Secrets
27+
client libraries:
28+
29+
```commandline
30+
pip install azure-identity azure-keyvault-secrets
31+
```
32+
33+
## Contents
34+
| File | Description |
35+
|-------------|-------------|
36+
| control_interactive_prompts.py | demonstrates controlling when interactive credentials prompt for user interaction |
37+
| user_authentication.py | demonstrates user authentication API for applications |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
"""Demonstrates controlling the timing of interactive authentication using InteractiveBrowserCredential.
6+
7+
DeviceCodeCredential supports the same API.
8+
"""
9+
10+
import os
11+
import sys
12+
from azure.identity import AuthenticationRequiredError, InteractiveBrowserCredential
13+
from azure.keyvault.secrets import SecretClient
14+
15+
16+
# This sample uses Key Vault only for demonstration. Any client accepting azure-identity credentials will work the same.
17+
VAULT_URL = os.environ.get("VAULT_URL")
18+
if not VAULT_URL:
19+
print("This sample expects environment variable 'VAULT_URL' to be set with the URL of a Key Vault.")
20+
sys.exit(1)
21+
22+
23+
# If it's important for your application to prompt for authentication only at certain times,
24+
# create the credential with disable_automatic_authentication=True. This configures the credential to raise
25+
# when interactive authentication is required, instead of immediately beginning that authentication.
26+
credential = InteractiveBrowserCredential(disable_automatic_authentication=True)
27+
client = SecretClient(VAULT_URL, credential)
28+
29+
try:
30+
secret_names = [s.name for s in client.list_properties_of_secrets()]
31+
except AuthenticationRequiredError as ex:
32+
# Interactive authentication is necessary to authorize the client's request. The exception carries the
33+
# requested authentication scopes. If you pass these to 'authenticate', it will cache an access token
34+
# for those scopes.
35+
credential.authenticate(scopes=ex.scopes)
36+
37+
# the client operation should now succeed
38+
secret_names = [s.name for s in client.list_properties_of_secrets()]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
"""Demonstrates user authentication using InteractiveBrowserCredential. DeviceCodeCredential supports the same API."""
6+
7+
import os
8+
import sys
9+
from azure.identity import AuthenticationRecord, InteractiveBrowserCredential
10+
from azure.keyvault.secrets import SecretClient
11+
12+
13+
# This sample uses Key Vault only for demonstration. Any client accepting azure-identity credentials will work the same.
14+
VAULT_URL = os.environ.get("VAULT_URL")
15+
if not VAULT_URL:
16+
print("This sample expects environment variable 'VAULT_URL' to be set with the URL of a Key Vault.")
17+
sys.exit(1)
18+
19+
20+
# Persistent caching is optional. By default, interactive credentials cache in memory only.
21+
credential = InteractiveBrowserCredential(enable_persistent_cache=True)
22+
23+
# The 'authenticate' method begins interactive authentication. Call it whenever it's convenient
24+
# for your application to authenticate a user. It returns a record of the authentication.
25+
record = credential.authenticate()
26+
27+
# The record contains no authentication secrets. You can serialize it to JSON for storage.
28+
record_json = record.serialize()
29+
30+
# An authenticated credential is ready for use with a client. This request should succeed
31+
# without prompting for authentication again.
32+
client = SecretClient(VAULT_URL, credential)
33+
secret_names = [s.name for s in client.list_properties_of_secrets()]
34+
35+
# With persistent caching enabled, an authentication record stored by your application enables
36+
# credentials to access data from past authentications. If the cache contains sufficient data,
37+
# this eliminates the need for your application to prompt for authentication every time it runs.
38+
deserialized_record = AuthenticationRecord.deserialize(record_json)
39+
new_credential = InteractiveBrowserCredential(enable_persistent_cache=True, authentication_record=deserialized_record)
40+
41+
# This request should also succeed without prompting for authentication.
42+
client = SecretClient(VAULT_URL, new_credential)
43+
secret_names = [s.name for s in client.list_properties_of_secrets()]

0 commit comments

Comments
 (0)