Sub topic 10
Creating a key pair and certificate for JWT signing
The JWT bearer flow proves who the caller is with a digital signature instead of a secret sent over the network. This chapter creates the key pair behind that signature. It assumes you know what public and private keys are, can run openssl from a terminal, and have a connected app in a Salesforce org (covered in an earlier chapter).
How the pieces fit
The integration server keeps the private key and uses it to sign each JWT. Salesforce holds the matching certificate, which contains the public key, and uses it to check the signature. If the signature verifies, Salesforce knows the JWT came from the holder of the private key.
Generate the key and certificate
One openssl command creates both files:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 \
-keyout server.key -out server.crt -subj "/CN=sf-jwt-integration"Each option does the following:
| Option | Meaning |
|---|---|
| `-x509` | Produces a self-signed certificate instead of a certificate request. |
| `-sha256` | Signs the certificate with SHA-256. |
| `-nodes` | Leaves the private key unencrypted, so a server can read it without a passphrase prompt. |
| `-days 365` | Sets the certificate to expire in one year. |
| `-newkey rsa:2048` | Creates a new 2048-bit RSA key pair. |
| `-keyout`, `-out` | Name the private key file and the certificate file. |
| `-subj` | Sets the certificate subject, so OpenSSL does not ask questions. |
The result is two files:
server.keyis the private key. It stays on the integration server.server.crtis the certificate. You upload it to the connected app.
Upload the certificate
- In Setup, open the connected app and edit it.
- Turn on Enable OAuth Settings if it is not already on.
- Select Use digital signatures.
- Choose
server.crtand save.
The JWT bearer flow also needs a scope such as api on the app, and users must be pre-authorized. Pre-authorization is the next chapter.
Common errors
- Certificate expired. Salesforce rejects signatures from an expired certificate. Note the expiry date and rotate before it arrives. To rotate, generate a new pair, upload the new certificate, and deploy the new key.
- Wrong key or certificate pairing. If you regenerate
server.keybut keep the old certificate in Salesforce, signature checks fail, and the token request returnsinvalid_grant. - Key committed to source control. Anyone who has
server.keycan sign JWTs as your integration user. Add it to.gitignoreand store it in a secrets manager or a file readable only by the service account.
Check your files
Confirm that the key and certificate belong together by comparing their public key hashes. The two lines of output must be identical.
openssl x509 -in server.crt -noout -pubkey | openssl sha256
openssl pkey -in server.key -pubout | openssl sha256You can also read the expiry date:
openssl x509 -in server.crt -noout -enddateExercise
A teammate regenerated server.key on the integration server but did not upload a new certificate. What happens when the server requests a token, and how do you fix it?
Show answer
The signature does not match the public key in the uploaded certificate, so Salesforce rejects the assertion (typically with invalid_grant). Generate a certificate from the new key, or reuse the original key pair, and upload the matching server.crt to the connected app.
Exercise
Which file do you upload to the connected app, and which file must never leave the integration server?
Show answer
Upload server.crt (the certificate). Keep server.key (the private key) on the integration server only.