Skip to content

Commit

Permalink
Fix membership retrieval (#95)
Browse files Browse the repository at this point in the history
The aud claim may be a string or an array.
  • Loading branch information
michaelwheeler authored Mar 25, 2024
1 parent a1b39d0 commit 15829b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lti_tool/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ def roles_claim(self):
def membership(self) -> LtiMembership:
"""The LTI membership associated with the launch."""
issuer = self.get_claim("iss")
client_id = self.get_claim("aud")
aud = self.get_claim("aud")
client_id = aud[0] if isinstance(aud, list) else aud
sub = self.get_claim("sub")
context_id = self.context_claim["id"] if self.context_claim is not None else ""
return LtiMembership.objects.get(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,25 @@ def test_membership_with_duplicate_context_ids(self, monkeypatch):
monkeypatch.setattr(models.LtiLaunch, "deployment", new_deployment)
lti_launch = models.LtiLaunch(None)
assert lti_launch.membership == new_membership

def test_membership_with_aud_array(self, monkeypatch):
membership = factories.LtiMembershipFactory()
deployment = membership.context.deployment
registration = deployment.registration
launch_data = {
"iss": registration.issuer,
"aud": [registration.client_id],
"sub": membership.user.sub,
"https://purl.imsglobal.org/spec/lti/claim/deployment_id": (
deployment.deployment_id
),
"https://purl.imsglobal.org/spec/lti/claim/context": {
"id": membership.context.id_on_platform,
},
}
monkeypatch.setattr(
models.LtiLaunch, "get_launch_data", lambda self: launch_data
)
monkeypatch.setattr(models.LtiLaunch, "deployment", deployment)
lti_launch = models.LtiLaunch(None)
assert lti_launch.membership == membership

0 comments on commit 15829b9

Please sign in to comment.