Skip to content
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

Make username_claim callable in CILogon #718

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion oauthenticator/cilogon.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ def _user_info_to_unprocessed_username(self, user_info):
username_derivation = self.allowed_idps[user_idp]["username_derivation"]
username_claim = username_derivation["username_claim"]

username = user_info.get(username_claim)
if callable(username_claim):
username = username_claim(user_info)
else:
username = user_info.get(self.username_claim, None)

if not username:
message = f"Configured username_claim {username_claim} for {user_idp} was not found in the response {user_info.keys()}"
self.log.error(message)
Expand Down
4 changes: 2 additions & 2 deletions oauthenticator/schemas/cilogon-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ properties:
required:
- username_claim
properties:
username_claim:
type: string
# FIXME: This needs to take a string or a callable
username_claim: {}
action:
type: string
enum:
Expand Down
23 changes: 23 additions & 0 deletions oauthenticator/tests/test_cilogon.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ async def test_cilogon(
assert auth_model == None


async def test_username_claim_callable(
cilogon_client,
):
c = Config()
c.CILogonOAuthenticator = Config()

c.CILogonOAuthenticator.allowed_idps = {
"https://some-idp.com/login/oauth/authorize": {
"username_derivation": {
"username_claim": lambda user_info: f"prefixed-{user_info['username']}",
},
},
}

authenticator = CILogonOAuthenticator(config=c)

handled_user_model = user_model("user1", "username")
handler = cilogon_client.handler_for_user(handled_user_model)
auth_model = await authenticator.get_authenticated_user(handler, None)

assert auth_model["name"] == f"prefixed-user1"


@mark.parametrize(
"test_variation_id,idp_config,class_config,test_user_name,expect_allowed,expect_admin",
[
Expand Down
Loading