p2-inc / keycloak-orgs

Single realm, multi-tenancy for SaaS apps
Other
367 stars 65 forks source link

400 Error thrown when fetching invitations for an organization #98

Closed egdelwonk closed 1 year ago

egdelwonk commented 1 year ago

Hi @xgp!

When fetching organization invitations, I'm receiving an HTTP 400 error from the endpoint.

It appears that if you add an invitation from the Admin UI. E.g. http://localhost:8080/auth/admin/master/console/#/myrealm/organizations/{orgId}/invitations that the response from the REST endpoint is an error.

I noticed this in the debug logs: 2023-06-15 18:49:26,472 DEBUG [org.keycloak.services.error.KeycloakErrorHandler] (executor-thread-3) Error response 400: com.fasterxml.jackson.databind.JsonMappingException: Cannot invoke "org.keycloak.models.UserModel.getId()" because the return value of "io.phasetwo.service.model.InvitationModel.getInviter()" is null

If I remove the pending invitation and fetch the endpoint again, it returns an empty array without an error as expected.

What's odd, is that inspecting the XHR request on the admin page to /invitations (http://localhost:8080/auth/realms/myrealm/orgs/{orgId}/invitations) the inviterId is present on the data returned.

e.g.

[
    {
        "id": "349c70f9-029d-444d-823e-91fcb0babed8",
        "email": "testinvite22@example.com",
        "createdAt": "2023-06-15T18:49Z",
        "inviterId": "f96b289d-03e1-4e66-8dcd-eff06cce32e0",
        "organizationId": "44e8eadc-4325-4fcd-86c1-a74d50ee0e18",
        "teams": [],
        "roles": []
    }
]

I think the issue could be that the user does not exist yet. (# Edit, just tested this, added the user before inviting and seeing the same error log.)

egdelwonk commented 1 year ago

Ok, sorry for the false report. It turns out that I was making the invitation from the admin user that was on my master realm (which works in the UI btw), but I needed to make the invitation from a user that had the appropriate access on my myrealm realm. I'm guessing that's why the UserModel wasn unable to resolve.