Why is our token exchange failing?

John Wade -

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) or https://auth.core.blackpear.com/token (Production), with Content-Type: application/x-www-form-urlencoded.
  • grant_type is core-token-exchange, client_assertion_type is urn:ietf:params:oauth:client-assertion-type:jwt-bearer and subject_token_type is urn:ietf:params:oauth:token-type:jwt.
  • service is 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

  • iss and sub are both the client ID we gave you for this environment. Beta and Production client IDs are different.
  • aud is the Core Auth URL for the environment, for example https://auth.core-beta.blackpear.com, sent as a single string, not an array.
  • iat, exp and jti are present, exp is in the future and jti is new for every request. A client assertion can't be reused.
  • The header's kid matches a key in your client assertion JWKS.
  • If your JWKS key includes alg, the header's alg is the same value. alg is 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

  • iss is 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.
  • aud is the Core Auth URL, as a single string.
  • organization is https://fhir.nhs.uk/Id/ods-organization-code|<ODS code>, using an ODS code that's linked to your service in this environment.
  • role is https://fhir.nhs.uk/Id/sds-role-code|<code>|<display>, for example https://fhir.nhs.uk/Id/sds-role-code|R8000|Clinical Practitioner Access Role.
  • sub is a stable ID for the user that's the same on every request, and name is the user's name. A new sub on each request creates a new user record each time.
  • nbf and exp cover 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 n value 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-Id or x-amzn-requestid from 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.

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk