omniphx / forrest

A Laravel library for Salesforce
https://omniphx.github.io/forrest/
MIT License
259 stars 120 forks source link

Add lightning and sandbox capability to OAuthJWT #333

Closed patricknomad closed 8 months ago

patricknomad commented 1 year ago

Update the the OAuthJWT authentication type to work for Lightning, Lightning Sandboxes and the Developer Edition.

Lightning Example

config/forrest.php

return [
    // ...
    'credentials' => [
        'loginURL' => 'https://login.salesforce.com',
        'consumerKey' => '<YOUR_KEY_HERE>',
        'username' => '<EMAIL>',
        'privateKey' => '<PRIVATE_KEY>',
    ],

    'instanceURL' => 'https://<YOUR_ORG>.my.salesforce.com',
    // ...
];

Lightning Sandbox Example

config/forrest.php

return [
    // ...
    'credentials' => [
        'loginURL' => 'https://test.salesforce.com',
        'consumerKey' => '<YOUR_KEY_HERE>',
        'username' => '<EMAIL>.<SANDBOX_NAME>',
        'privateKey' => '<PRIVATE_KEY>',
    ],

    'instanceURL' => 'https://<YOUR_ORG>--<SANDBOX_NAME>.sandbox.my.salesforce.com',
    // ...
];

Salesforce Developer Edition Example

config/forrest.php return [

    // ...
    'credentials' => [
        'loginURL' => 'https://test.salesforce.com',
        'consumerKey' => '<YOUR_KEY_HERE>',
        'username' => '<EMAIL>',
        'privateKey' => '<PRIVATE_KEY>',
    ],

    'instanceURL' => 'https://<DEV_DOMAIN>.develop.my.salesforce.com',
    // ...
];
wch-karol commented 8 months ago

@omniphx & @patricknomad ⚠️ This PR make instanceURL setting required to have filled in case of OAuthJWT auth method. Otherwise it throws us an MissingTokenException in refresh/create token method. I suggest to add it somewhere to docs.

wch-karol commented 8 months ago

And maybe change comment here: https://github.com/omniphx/forrest/blob/96cbd606dcd496ce626da0353133c35ca6f49082/src/config/config.php#L80-L83 And maybe then create .env variable eg SF_INSTANCE_URL

omniphx commented 8 months ago

@wch-karol thanks for pointing this out. Just merged in a fix so that instanceURL is no longer required (v2.18.2). The method previously used in this PR would try to retrieve an instanceURL from the latest token (which if missing would throw the MissingTokenException). Obviously, that would always be the case when trying to authenticate for the first time.

Unfortunately, not the best approach but also not @patricknomad's fault. Just a misleading method name.

Also, added some documentation as you suggested.

Cheers!