Skip to content

Commit 9637f5a

Browse files
gchq83514singingwolfboy
authored andcommitted
Updated Azure to allow defining authorization_url_params (#288)
Azure configuration: allow prompt, domain_hint, and login_hint
1 parent 1f42014 commit 9637f5a

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
to include the ``client_id`` in the OAuth token request.
88
* Removed Okta pre-set configuration, since it doesn't add any value over
99
using ``OAuth2ConsumerBlueprint`` directly.
10+
* Updated Azure to allow defining ``authorization_url_params``
1011

1112
`2.2.0`_ (2019-06-04)
1213
---------------------

flask_dance/contrib/azure.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def make_azure_blueprint(
2424
session_class=None,
2525
storage=None,
2626
tenant="common",
27+
prompt=None,
28+
domain_hint=None,
29+
login_hint=None,
2730
):
2831
"""
2932
Make a blueprint for authenticating with Azure AD using OAuth 2. This requires
@@ -55,12 +58,35 @@ def make_azure_blueprint(
5558
`See the Azure documentation for more information about this parameter.
5659
<https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpoints>`_
5760
Defaults to ``common``.
61+
prompt (str, optional): Indicate the type of user interaction that is required.
62+
Valid values are ``login``, ``select_account``, ``consent``, ``admin_consent``.
63+
Learn more about the options `here.
64+
<https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-protocols-oauth-code#request-an-authorization-code>`_
65+
Defaults to ``None``
66+
domain_hint (str, optional): Provides a hint about the tenant or domain that
67+
the user should use to sign in. The value of the domain_hint is a
68+
registered domain for the tenant. If the tenant is federated to an
69+
on-premises directory, AAD redirects to the specified tenant federation server.
70+
Defaults to ``None``
71+
login_hint (str, optional): Can be used to pre-fill the username/email
72+
address field of the sign-in page for the user, if you know their
73+
username ahead of time. Often apps use this parameter during re-authentication,
74+
having already extracted the username from a previous sign-in using the
75+
preferred_username claim.
76+
Defaults to ``None``
5877
5978
6079
:rtype: :class:`~flask_dance.consumer.OAuth2ConsumerBlueprint`
6180
:returns: A :ref:`blueprint <flask:blueprints>` to attach to your Flask app.
6281
"""
6382
scope = scope or ["openid", "email", "profile", "User.Read"]
83+
authorization_url_params = {}
84+
if login_hint:
85+
authorization_url_params["login_hint"] = login_hint
86+
if domain_hint:
87+
authorization_url_params["domain_hint"] = domain_hint
88+
if prompt:
89+
authorization_url_params["prompt"] = prompt
6490
azure_bp = OAuth2ConsumerBlueprint(
6591
"azure",
6692
__name__,
@@ -78,6 +104,7 @@ def make_azure_blueprint(
78104
redirect_to=redirect_to,
79105
login_url=login_url,
80106
authorized_url=authorized_url,
107+
authorization_url_params=authorization_url_params,
81108
session_class=session_class,
82109
storage=storage,
83110
)

tests/contrib/test_azure.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,78 @@ def test_blueprint_factory():
4343
)
4444

4545

46+
def test_blueprint_factory_with_domain_hint():
47+
azure_domain_bp = make_azure_blueprint(
48+
client_id="foo",
49+
client_secret="bar",
50+
scope="user.read",
51+
redirect_to="index",
52+
domain_hint="Sample Hint",
53+
)
54+
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint)
55+
assert azure_domain_bp.session.scope == "user.read"
56+
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com"
57+
assert azure_domain_bp.session.client_id == "foo"
58+
assert azure_domain_bp.client_secret == "bar"
59+
assert azure_domain_bp.authorization_url_params["domain_hint"] == "Sample Hint"
60+
assert (
61+
azure_domain_bp.authorization_url
62+
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
63+
)
64+
assert (
65+
azure_domain_bp.token_url
66+
== "https://login.microsoftonline.com/common/oauth2/v2.0/token"
67+
)
68+
69+
70+
def test_blueprint_factory_with_login_hint():
71+
azure_domain_bp = make_azure_blueprint(
72+
client_id="foo",
73+
client_secret="bar",
74+
scope="user.read",
75+
redirect_to="index",
76+
login_hint="Sample Login Hint",
77+
)
78+
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint)
79+
assert azure_domain_bp.session.scope == "user.read"
80+
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com"
81+
assert azure_domain_bp.session.client_id == "foo"
82+
assert azure_domain_bp.client_secret == "bar"
83+
assert azure_domain_bp.authorization_url_params["login_hint"] == "Sample Login Hint"
84+
assert (
85+
azure_domain_bp.authorization_url
86+
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
87+
)
88+
assert (
89+
azure_domain_bp.token_url
90+
== "https://login.microsoftonline.com/common/oauth2/v2.0/token"
91+
)
92+
93+
94+
def test_blueprint_factory_with_prompt():
95+
azure_domain_bp = make_azure_blueprint(
96+
client_id="foo",
97+
client_secret="bar",
98+
scope="user.read",
99+
redirect_to="index",
100+
prompt="select_account",
101+
)
102+
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint)
103+
assert azure_domain_bp.session.scope == "user.read"
104+
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com"
105+
assert azure_domain_bp.session.client_id == "foo"
106+
assert azure_domain_bp.client_secret == "bar"
107+
assert azure_domain_bp.authorization_url_params["prompt"] == "select_account"
108+
assert (
109+
azure_domain_bp.authorization_url
110+
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
111+
)
112+
assert (
113+
azure_domain_bp.token_url
114+
== "https://login.microsoftonline.com/common/oauth2/v2.0/token"
115+
)
116+
117+
46118
def test_blueprint_factory_with_organization_tenant():
47119
azure_orgs_bp = make_azure_blueprint(
48120
client_id="foo",

0 commit comments

Comments
 (0)