Setup Salesforce Auto-Deployments (JWT OAuth Flow)

1. Create a Public/Private Key Pair

To authenticate using JWT, you'll need an RSA key pair.

πŸ“ Generate the Keys (using OpenSSL)

# Generate private key
openssl genrsa -out server.key 2048

# Generate public key from private key
openssl rsa -in server.key -pubout -out server.pub
  • server.key = Private key (used to sign JWT)

  • server.pub = Public key (uploaded to Salesforce)

  1. Generate a Self-Signed X.509 Certificate

Use your existing private key to generate the certificate:

openssl req -new -x509 -key olympus-grid-alpha-1.key -out olympus-grid-alpha-1.crt -days 3650 -subj "//CN=AutomatedDeployment"
  • server.key: your private key

  • server.crt: your self-signed certificate (this is what you upload to Salesforce)

  • The -subj flag avoids prompts


2. Create the Connected App in Salesforce

  1. Login to Salesforce Setup

    • Go to Setup β†’ App Manager β†’ New Connected App

      • Note - We used the "Create a Connected App" in this example

  2. Basic Information

    • Connected App Name: e.g., Eos Automated Deployment JWT

    • API Name: auto-filled

    • Contact Email: your email

  3. Enable OAuth Settings

    • β˜‘οΈ Enable OAuth Settings

    • Callback URL: http://localhost/callback (dummy for JWT)

  4. Enable JWT Flow

    • β˜‘οΈ Use digital signatures

    • Upload the certificate (i.e., server.crt)

  5. Selected OAuth Scopes:

    • Full Access (full)

    • Perform requests on your behalf at any time (refresh_token, offline_access)

    • Add others as needed (e.g., Access and manage your data (api))

  6. Save the App

    • After saving, click Continue

    • Save the Consumer Key for use in the JWT assertion


3. Configure the Connected App Policy

  • Set the OAuth Policies to "Admin approved users are pre-authorized"

3. Configure the Salesforce User

Ensure the user (usually a deployment service account) meets these criteria:

  • Has the correct Profile or Permission Set with API access to the connected app

  • Belongs to the same org where the Connected App was created

  • Username (e.g., [email protected]) must match the sub (subject) claim in the JWT


4. Use the sf cli to validate the jwt

5. (Optional) Build the JWT Assertion Manually

A JWT assertion has the following structure:

πŸ“ Header (Base64-encoded)

πŸ“ Payload (Base64-encoded)

πŸ“ Sign the JWT

Use the private key (server.key) to sign the JWT using RS256.


5. Test JWT Login with Postman

  1. Set up a POST request

    URL:

    Headers:

    Body (x-www-form-urlencoded):

  2. Send the request

    If successful, Salesforce will respond with:

    βœ… Success! You can now use the access_token to make authenticated API requests.


βœ… Tips

  • Use libraries like jsonwebtoken (Node.js) or pyjwt (Python) to automate JWT creation and signing.

  • You can add multiple keys later under Connected App β†’ Manage β†’ Edit Policies β†’ Use Digital Signatures.

Last updated