Short answer: Almost every failed token exchange is caused by a claim, key or URL that doesn't match what we registered for your client in that environment. The error message doesn't always point at the cause, so work through the checks below. If you're still stuck, send us the details listed at the end.
Check the request
- Send a POST to
https://auth.core-beta.blackpear.com/token(Beta) orhttps://auth.core.blackpear.com/token(Production), withContent-Type: application/x-www-form-urlencoded. -
grant_typeiscore-token-exchange,client_assertion_typeisurn:ietf:params:oauth:client-assertion-type:jwt-bearerandsubject_token_typeisurn:ietf:params:oauth:token-type:jwt. -
serviceis the meta endpoint URL and your HealthcareService ID joined by a pipe:https://<meta endpoint>|<service ID>. - The request comes from a UK IP address. Requests from outside the UK are blocked.
Check the client assertion
-
issandsubare both the client ID we gave you for this environment. Beta and Production client IDs are different. -
audis the Core Auth URL for the environment, for examplehttps://auth.core-beta.blackpear.com, sent as a single string, not an array. -
iat,expandjtiare present,expis in the future andjtiis new for every request. A client assertion can't be reused. - The header's
kidmatches a key in your client assertion JWKS. - If your JWKS key includes
alg, the header'salgis the same value.algis optional in the JWKS, but a mismatch (for example RS512 in the JWKS and RS256 in the token) is rejected. Supported values are RS256 and RS512.
Check the subject token
-
issis exactly the issuer you registered with us. The match is exact, so a trailing slash counts. - It's signed with a key in the subject token JWKS you registered, with a matching
kid. -
audis the Core Auth URL, as a single string. -
organizationishttps://fhir.nhs.uk/Id/ods-organization-code|<ODS code>, using an ODS code that's linked to your service in this environment. -
roleishttps://fhir.nhs.uk/Id/sds-role-code|<code>|<display>, for examplehttps://fhir.nhs.uk/Id/sds-role-code|R8000|Clinical Practitioner Access Role. -
subis a stable ID for the user that's the same on every request, andnameis the user's name. A newsubon each request creates a new user record each time. -
nbfandexpcover the current time.
Check your JWKS
- It's valid JSON. A single trailing comma makes the whole file unreadable, so none of your keys can be used.
- It's served over HTTPS as
application/json. Raw GitHub file links (raw.githubusercontent.com) are served as plain text and can't be read. - The public key matches the private key you sign with. If you built the JWKS by hand, check the
nvalue wasn't cut short when it was copied: a 4096-bit key gives a 512-byte signature and needs a 512-byte modulus.
Specific messages
| Message | Most likely cause |
|---|---|
invalid_client (client authentication failed) |
The client assertion: iss or sub, aud, kid or alg, or a signature that doesn't match your JWKS. |
signature verification failed |
The token was signed with a key that doesn't match the public key in your JWKS. |
invalid_client_metadata |
Check that your JWKS can be read (valid JSON, served as application/json) and that aud is a single string. |
| Organisation <ODS code> is not affiliated with the specified HealthcareService | The ODS code in organization isn't linked to your service in this environment. Codes used on Beta don't carry over to Production. Ask us to add the ODS code you need. |
| 400 Bad Request with no detail | Any of the checks above, most often kid or alg. |
After a successful exchange
The access token is valid for 600 seconds (expires_in). Reuse it for every API call until it expires, rather than exchanging tokens for each request. Each API call needs three headers: Authorization: Bearer <access token>, x-api-key and x-fhir-practitionerrole-id. If API calls return 401 or 403, see What do Core API error codes mean?
If it still fails, send us
- The
X-Amzn-Trace-Idorx-amzn-requestidfrom the response (how to find it) - The time of the request (UTC) and the environment
- Your client ID
- The decoded header and claims of both tokens. Never send your private key.
Full details of the token exchange are in API access with auto-enrolment.
0 Comments