-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User authentication samples #11343
Merged
Merged
User authentication samples #11343
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
recursive-include samples *.py | ||
recursive-include tests *.py | ||
include *.md | ||
include azure/__init__.py | ||
include azure/__init__.py |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- python | ||
products: | ||
- azure | ||
- azure-identity | ||
urlFragment: identity-samples | ||
--- | ||
|
||
# Azure Identity Client Library Python Samples | ||
|
||
* [user_authentication.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity/samples/user_authentication.py) - | ||
demonstrates user authentication API for applications | ||
|
||
* [control_interactive_prompts.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity/samples/control_interactive_prompts.py) - | ||
demonstrates controlling when interactive credentials such as | ||
`InteractiveBrowserCredential` prompt for user interaction |
38 changes: 38 additions & 0 deletions
38
sdk/identity/azure-identity/samples/control_interactive_prompts.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
"""Demonstrates controlling the timing of interactive authentication using InteractiveBrowserCredential. | ||
|
||
DeviceCodeCredential supports the same API. | ||
""" | ||
|
||
import os | ||
import sys | ||
from azure.identity import AuthenticationRequiredError, InteractiveBrowserCredential | ||
from azure.keyvault.secrets import SecretClient | ||
|
||
|
||
# This sample uses Key Vault only for demonstration. Any client accepting azure-identity credentials will work the same. | ||
VAULT_URL = os.environ.get("VAULT_URL") | ||
if not VAULT_URL: | ||
print("This sample expects environment variable 'VAULT_URL' to be set with the URL of a Key Vault.") | ||
sys.exit(1) | ||
|
||
|
||
# If it's important for your application to prompt for authentication only at certain times, | ||
# create the credential with disable_automatic_authentication=True. This configures the credential to raise | ||
# when interactive authentication is required, instead of immediately beginning that authentication. | ||
credential = InteractiveBrowserCredential(disable_automatic_authentication=True) | ||
client = SecretClient(VAULT_URL, credential) | ||
|
||
try: | ||
secret_names = [s.name for s in client.list_properties_of_secrets()] | ||
except AuthenticationRequiredError as ex: | ||
# Interactive authentication is necessary to authorize the client's request. The exception carries the | ||
# requested authentication scopes. If you pass these to 'authenticate', it will cache an access token | ||
# for those scopes. | ||
credential.authenticate(scopes=ex.scopes) | ||
|
||
# the client operation should now succeed | ||
secret_names = [s.name for s in client.list_properties_of_secrets()] |
43 changes: 43 additions & 0 deletions
43
sdk/identity/azure-identity/samples/user_authentication.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
"""Demonstrates user authentication using InteractiveBrowserCredential. DeviceCodeCredential supports the same API.""" | ||
|
||
import os | ||
import sys | ||
from azure.identity import AuthenticationRecord, InteractiveBrowserCredential | ||
from azure.keyvault.secrets import SecretClient | ||
|
||
|
||
# This sample uses Key Vault only for demonstration. Any client accepting azure-identity credentials will work the same. | ||
VAULT_URL = os.environ.get("VAULT_URL") | ||
if not VAULT_URL: | ||
print("This sample expects environment variable 'VAULT_URL' to be set with the URL of a Key Vault.") | ||
sys.exit(1) | ||
|
||
|
||
# Persistent caching is optional. By default, interactive credentials cache in memory only. | ||
credential = InteractiveBrowserCredential(enable_persistent_cache=True) | ||
|
||
# The 'authenticate' method begins interactive authentication. Call it whenever it's convenient | ||
# for your application to authenticate a user. It returns a record of the authentication. | ||
record = credential.authenticate() | ||
|
||
# The record contains no authentication secrets. You can serialize it to JSON for storage. | ||
record_json = record.serialize() | ||
|
||
# An authenticated credential is ready for use with a client. This request should succeed | ||
# without prompting for authentication again. | ||
client = SecretClient(VAULT_URL, credential) | ||
secret_names = [s.name for s in client.list_properties_of_secrets()] | ||
|
||
# With persistent caching enabled, an authentication record stored by your application enables | ||
# credentials to access data from past authentications. If the cache contains sufficient data, | ||
# this eliminates the need for your application to prompt for authentication every time it runs. | ||
deserialized_record = AuthenticationRecord.deserialize(record_json) | ||
new_credential = InteractiveBrowserCredential(enable_persistent_cache=True, authentication_record=deserialized_record) | ||
|
||
# This request should also succeed without prompting for authentication. | ||
client = SecretClient(VAULT_URL, new_credential) | ||
secret_names = [s.name for s in client.list_properties_of_secrets()] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like user needs to install keyvault to run these samples?
Maybe we should point it out in readme?