Skip to content

Commit 533f72f

Browse files
committed
Separate module for the token membership/admin functions
1 parent a366176 commit 533f72f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

mlflow_oidc_auth/token_utils.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import fnmatch
2+
3+
from mlflow_oidc_auth.config import config
4+
5+
6+
def token_get_user_groups(token: dict) -> list[str]:
7+
"""Retrieve the list of groups this user (based on the provided token) is a member of
8+
9+
Args:
10+
token: dictionary holding the oidc token information
11+
12+
Returns:
13+
list of all the groups this user is a member of
14+
"""
15+
user_groups = []
16+
17+
if config.OIDC_GROUP_DETECTION_PLUGIN:
18+
import importlib
19+
20+
user_groups = importlib.import_module(config.OIDC_GROUP_DETECTION_PLUGIN).get_user_groups(token["access_token"])
21+
else:
22+
user_groups = token["userinfo"][config.OIDC_GROUPS_ATTRIBUTE]
23+
24+
# Now filter the user groups to keep only those matching the pattern or the ADMIN group
25+
user_groups = sorted(
26+
set(
27+
[
28+
g
29+
for g in user_groups
30+
if (g == config.OIDC_ADMIN_GROUP_NAME) or any(fnmatch.fnmatch(g, p) for p in config.OIDC_GROUP_FILTER_PATTERNS)
31+
]
32+
)
33+
)
34+
35+
return user_groups
36+
37+
38+
def token_get_user_is_admin(user_groups: list[str]):
39+
"""Check if the admin group is included in the user_groups. In that case
40+
it means that the user is an admin user
41+
42+
Args:
43+
user_groups (list[str]): list of the groups the current user belongs to
44+
45+
Returns:
46+
True if the admin group is in the list of the groups of the current user, False otherwise
47+
48+
"""
49+
is_admin = False
50+
51+
if config.OIDC_ADMIN_GROUP_NAME in user_groups:
52+
is_admin = True
53+
54+
return is_admin

0 commit comments

Comments
 (0)