Skip to content

Commit

Permalink
Add preferred_username to user_context
Browse files Browse the repository at this point in the history
  • Loading branch information
eray-inuits committed Feb 7, 2024
1 parent 3cce2e0 commit 7861572
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
STATIC_ISSUER="inuits-policy-based-auth"
ALLOWED_ISSUERS="inuits-policy-based-auth"
STATIC_PRIVATE_KEY=""
STATIC_PUBLIC_KEY=""
TEST_API_CONFIGURATION=src/tests/integration/test_api/configuration.json
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __instantiate_authentication_policy(policy_module_name, policy, logger: Logg
token_schema,
os.getenv("STATIC_ISSUER"),
os.getenv("STATIC_PUBLIC_KEY"),
None,
os.getenv("ALLOWED_ISSUERS"),
allow_anonymous_users,
)
if policy_module_name == "token_based_policies.tenant_token_roles_policy":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ def authenticate(self, user_context, _):
user_context.auth_objects.add_key_value_pair("token", token)
flattened_token = user_context.flatten_auth_object(token)

user_context.id = flattened_token.get(self._token_schema["id"], "")
user_context.id = flattened_token[self._token_schema["id"]]
user_context.email = flattened_token.get(
self._token_schema["email"], ""
self._token_schema.get("email", ""), ""
).lower()
user_context.preferred_username = flattened_token.get(
self._token_schema.get("preferred_username", ""), ""
).lower()
return user_context
except InvalidTokenError as error:
Expand Down
13 changes: 13 additions & 0 deletions src/inuits_policy_based_auth/contexts/user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class UserContext:
The id of the authenticated user.
email : str
The email of the authenticated user.
preferred_username : str
The preferred_username of the authenticated user.
x_tenant : Tenant
The user tenant that is requested from the X-Tenant-Id http header.
tenants : list[Tenant]
Expand All @@ -38,6 +40,7 @@ def __init__(self):
self._auth_objects = ImmutableDict({})
self._id = ""
self._email = ""
self._preferred_username = ""
self._x_tenant = Tenant()
self._tenants: list[Tenant] = []
self._bag = {}
Expand Down Expand Up @@ -72,6 +75,16 @@ def email(self):
def email(self, email: str):
self._email = email

@property
def preferred_username(self):
"""The preferred_username of the authenticated user."""

return self._preferred_username

@preferred_username.setter
def preferred_username(self, preferred_username: str):
self._preferred_username = preferred_username

@property
def x_tenant(self):
"""The user tenant that is requested from the X-Tenant-Id http header."""
Expand Down
10 changes: 6 additions & 4 deletions src/tests/integration/test_api/policy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ def __instantiate_authentication_policy(policy_module_name, policy, logger: Logg
token_schema,
os.getenv("STATIC_ISSUER"),
os.getenv("STATIC_PUBLIC_KEY"),
None,
os.getenv("ALLOWED_ISSUERS"),
allow_anonymous_users,
)
if policy_module_name == "token_based_policies.tenant_token_roles_policy":
return policy(
token_schema,
os.getenv("ROLE_SCOPE_MAPPING", os.getenv("TEST_API_SCOPES")),
True
if os.getenv("ALLOW_ANONYMOUS_USERS", "false").lower() == "true"
else False,
(
True
if os.getenv("ALLOW_ANONYMOUS_USERS", "false").lower() == "true"
else False
),
)

return policy()
Expand Down
1 change: 1 addition & 0 deletions src/tests/integration/test_api/token_schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"id": "sub",
"email": "email",
"preferred_username": "preferred_username",
"roles": "resource_access.inuits-policy-based-auth.roles"
}

0 comments on commit 7861572

Please sign in to comment.