External IdP with Clerk.com
# support
l
Hi there, I'm attempting to stand up the example ( https://github.com/medplum/medplum-client-external-idp-demo/tree/main ) external IdP flow with my hosted medplum project but I'm getting the following back from https://api.medplum.com/auth/external
Copy code
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "text": "Failed to verify code - check your identity provider configuration"
      }
    }
  ],
  "extension": [
    {
      "url": "https://medplum.com/fhir/StructureDefinition/tracing",
      "extension": [
        {
          "url": "requestId",
          "valueId": "06149170-c63e-414c-81ac-3aba919be269"
        },
        {
          "url": "traceId",
          "valueId": "1-6924bbdb-54b107a33232693458766d13"
        }
      ]
    }
  ]
}
any ideas? I've configured everything as per the documentation but it seems like the medplum auth/external endpoint doesn't like the config somewhere. The url that my IdP sends to has a
code
and a
state
parameter on it and the state has URL encoded JSON with the
projectId
,
clientId
and
redirectUri
inside.
@reshma or anyone? Is there some way I can debug what's happening on the medplum side?
f
Hi @lazybaer , I am happy to help! Can you confirm that your IDP is configured with your self hosted API's version of this redirect URI:
https://api.medplum.com/auth/external
l
so I'm running the example application on my
localhost
and it's configured with
Copy code
const MEDPLUM_BASE_URL = 'https://api.medplum.com/';
const EXTERNAL_REDIRECT_URI = MEDPLUM_BASE_URL + 'auth/external';
so yes it should be hitting that URL
f
Ok, gotcha. If you are self hosting, that
MEDPLUM_BASE_URL
will need to be your hosted API URL
l
I am not self hosting. I'm using the medplum hosted version
f
Ahh, ok I misunderstood from your initial question. Do you have the web app
redirectUri
set on the
ClientApplication
?
l
on the medplum side? yes I do
f
Ok, by default I believe that example app runs at
http://localhost:8000/
l
yup. I updated to port 3000
f
can you try without the
/signin
?
l
the app runs, displays the sign in button, pushes me to clerk, I auth and approve the Oauth application
then I end up at the medplum url with the error
so set the redirect to be without
/signin
?
f
No, actually that should be fine
Which medplum url to you end up at?
that's where I get
Copy code
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "text": "Failed to verify code - check your identity provider configuration"
      }
    }
  ],
  "extension": [
    {
      "url": "https://medplum.com/fhir/StructureDefinition/tracing",
      "extension": [
        {
          "url": "requestId",
          "valueId": "8bbd2448-58c0-4a24-8faa-a309c33c0735"
        },
        {
          "url": "traceId",
          "valueId": "1-6924c5bb-67d437a0333313dd305957ed"
        }
      ]
    }
  ]
}
which I would assume was off on my client config on the medplum side but I don't see anything missing or off there
f
Can you confirm that the
MEDPLUM_CLIENT_ID
you set in the example app is the ClientApplication that you sent screenshot of from above?
f
Ok nice! Now can you confirm that these are consistent with your Clerk setup?
Copy code
/**
 * External OAuth2 "authorize" endpoint URL.
 * For example, this would be an Auth0, AWS Cognito, or Okta URL.
 * This value is specific to your external auth provider.
 */
const EXTERNAL_AUTHORIZE_URL = '';

/**
 * External OAuth2 client ID.
 * This value is specific to your external auth provider.
 */
const EXTERNAL_CLIENT_ID = '';
the value set in medplum is the same as in my code that I redacted there for the
EXTERNAL_CLIENT_ID
f
Ok, gotcha. And did you set
ClientApplication.identityProvider
?
And those all match?
l
in the code like so?
Copy code
const handleClick = useCallback(() => {
    medplum
      .signInWithExternalAuth(
        EXTERNAL_AUTHORIZE_URL,
        EXTERNAL_CLIENT_ID,
        EXTERNAL_REDIRECT_URI,
        {
          projectId: MEDPLUM_PROJECT_ID,
          clientId: MEDPLUM_CLIENT_ID,
          redirectUri: WEB_APP_REDIRECT_URI,
        },
        false,
      )
      .catch(console.error);
  }, [medplum]);
f
Yes, but also on the ClientApplication:
Copy code
{
  "resourceType": "ClientApplication",
  "redirectUri": "http://localhost:3000/signin",
  "identityProvider": {
    "authorizeUrl": "https://YOUR_CLERK_DOMAIN/oauth/authorize",
    "tokenUrl": "https://YOUR_CLERK_DOMAIN/oauth/token",
    "clientId": "YOUR_CLERK_CLIENT_ID",
    "clientSecret": "YOUR_CLERK_CLIENT_SECRET"
  }
}
l
in the medplum
ClientApplication
record those are all set, yes
f
l
oh no. So it's on the medplum service side?
f
Does the Clerk dashboard give you any logs for error messages? We don't store store that info in our logs unfortunately
No, the error is being thrown by Clerk. That is where Medplum server makes request to Clerk token url
l
Clerk doesn't see to have anything unfortunately
ahhhh
lemme double check clerk but I don't think there's anything
f
Maybe try to hit your Clerk token url directly with postman or curl?
l
so got through the flow, hit the error, and then take the
code
from the URL and try and make this call? https://github.com/medplum/medplum/blob/ec580462ffb705f4de5e6dc6d8748c181e56503e/packages/server/src/auth/external.ts#L189
sorry. which request should I be emulating ?
f
Yea, exactly
l
gotcha. ok. I can give that a try. thanks @finnbergquist
I'll let you know how it turns out
@finnbergquist thanks for your time and patience. I ended up recreating my OAuth app and setting new client credentials.... and it worked. I can only guess it was a copy pasta error on my behalf 🤦🏼‍♂️ https://cdn.discordapp.com/attachments/1442610329690575049/1442666475675713647/image.png?ex=69414a1b&is=693ff89b&hm=f87c5d792b548966700a8503be5f6bc1d795bb025f2104a8c946bfc911d039e4&
f
Glad you got it working!
l
@finnbergquist one more quick question for you. Can the Medplum client config have more than one redirect URI ?
i
@lazybaer , picking this back up - within ClientApplication you can set an array of
redirectUris
- https://www.medplum.com/docs/api/fhir/medplum/clientapplication
l
Thank you!
the Medplum app itself doesn't seem to support that
redirectUris
field. Do I need to set that programatically perhaps?
r
@lazybaer can you look again?
l
🤦🏼‍♂️ I see it now. I'm not sure how I could have possibly missed that. thanks a ton
45 Views