forked from puiterwijk/flask-oidc
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a user model to
flask.g
with convenience properties
Signed-off-by: Aurélien Bompard <[email protected]>
- Loading branch information
Showing
6 changed files
with
191 additions
and
14 deletions.
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
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
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,56 @@ | ||
# SPDX-FileCopyrightText: 2023 Aurélien Bompard <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
|
||
from flask import current_app, session | ||
|
||
|
||
class User: | ||
"""A representation of an OIDC-based user. | ||
Arguments: | ||
ext (OpenIDConnect): the extension instance | ||
""" | ||
|
||
def __init__(self, ext): | ||
self._ext = ext | ||
|
||
@property | ||
def logged_in(self): | ||
"""Return ``True`` if the user is logged in, ``False`` otherwise.""" | ||
return session.get("oidc_auth_token") is not None | ||
|
||
@property | ||
def access_token(self): | ||
"""The user's OIDC access token.""" | ||
return self._ext.get_access_token() | ||
|
||
@property | ||
def refresh_token(self): | ||
"""The user's OIDC refresh token.""" | ||
return self._ext.get_refresh_token() | ||
|
||
@property | ||
def profile(self): | ||
"""The user's OIDC profile, if any. | ||
Raises: | ||
RuntimeError: when ``OIDC_USER_INFO_ENABLED`` is ``False`` in the application's | ||
configuration. | ||
""" | ||
if not current_app.config["OIDC_USER_INFO_ENABLED"]: | ||
raise RuntimeError( | ||
"User info is disabled in configuration (OIDC_USER_INFO_ENABLED)" | ||
) | ||
return session.get("oidc_auth_profile", {}) | ||
|
||
@property | ||
def name(self): | ||
"""The user's nickname.""" | ||
return self.profile.get("nickname") | ||
|
||
@property | ||
def groups(self): | ||
"""The list of group names the user belongs to.""" | ||
return self.profile.get("groups", []) |
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
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,86 @@ | ||
# SPDX-FileCopyrightText: 2014-2015 Erica Ehrhardt | ||
# SPDX-FileCopyrightText: 2016-2022 Patrick Uiterwijk <[email protected]> | ||
# SPDX-FileCopyrightText: 2023 Aurélien Bompard <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
import flask | ||
import pytest | ||
|
||
from .utils import set_token | ||
|
||
|
||
def test_user_in_view(client, dummy_token): | ||
set_token(client, dummy_token) | ||
|
||
resp = client.get("/") | ||
assert resp.status_code == 200 | ||
|
||
assert hasattr(flask.g, "oidc_user") | ||
user = flask.g.oidc_user | ||
|
||
assert user.logged_in | ||
assert user.access_token == dummy_token["access_token"] | ||
assert user.refresh_token == dummy_token["refresh_token"] | ||
assert user.profile == {"nickname": "dummy"} | ||
assert user.name == "dummy" | ||
|
||
|
||
def test_user_with_groups(client, dummy_token): | ||
set_token(client, dummy_token, {"groups": ["dummy_group"]}) | ||
|
||
resp = client.get("/") | ||
assert resp.status_code == 200 | ||
|
||
assert hasattr(flask.g, "oidc_user") | ||
user = flask.g.oidc_user | ||
assert user.profile == {"nickname": "dummy", "groups": ["dummy_group"]} | ||
assert user.groups == ["dummy_group"] | ||
|
||
|
||
def test_user_no_user_info(client, test_app, dummy_token): | ||
set_token(client, dummy_token) | ||
test_app.config["OIDC_USER_INFO_ENABLED"] = False | ||
|
||
resp = client.get("/") | ||
assert resp.status_code == 200 | ||
|
||
assert hasattr(flask.g, "oidc_user") | ||
user = flask.g.oidc_user | ||
|
||
with pytest.raises(RuntimeError): | ||
user.profile | ||
|
||
with pytest.raises(RuntimeError): | ||
user.name | ||
|
||
with pytest.raises(RuntimeError): | ||
user.groups | ||
|
||
|
||
def test_user_no_user(make_test_app, dummy_token): | ||
test_app = make_test_app({"OIDC_USER_CLASS": None}) | ||
client = test_app.test_client() | ||
with client: | ||
set_token(client, dummy_token) | ||
resp = client.get("/") | ||
|
||
assert resp.status_code == 200 | ||
assert not hasattr(flask.g, "oidc_user") | ||
|
||
|
||
class OtherUser: | ||
def __init__(self, ext): | ||
pass | ||
|
||
|
||
def test_user_special_user_class(make_test_app, dummy_token): | ||
test_app = make_test_app({"OIDC_USER_CLASS": f"{__name__}.OtherUser"}) | ||
client = test_app.test_client() | ||
with client: | ||
set_token(client, dummy_token) | ||
resp = client.get("/") | ||
|
||
assert resp.status_code == 200 | ||
assert hasattr(flask.g, "oidc_user") | ||
assert isinstance(flask.g.oidc_user, OtherUser) |
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,11 @@ | ||
# SPDX-FileCopyrightText: 2023 Aurélien Bompard <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
|
||
def set_token(client, token, profile=None): | ||
_profile = {"nickname": "dummy"} | ||
_profile.update(profile or {}) | ||
with client.session_transaction() as session: | ||
session["oidc_auth_token"] = token | ||
session["oidc_auth_profile"] = _profile |