Skip to content

Commit 7768ac2

Browse files
authored
Default to login url to keep backwards compatibility for OAuth JWT (#343)
* Default to login url to keep backwards compatibility * Remove extra instanceURL * Instance url is no longer required for JWT auth * Clean up * Update docs
1 parent 96cbd60 commit 7768ac2

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ This will publish a `config/forrest.php` file that can switch between authentica
5656

5757
After adding the config file, update your `.env` to include the following values (details for getting a consumer key and secret are outlined below):
5858

59-
```
59+
```txt
6060
SF_CONSUMER_KEY=123455
6161
SF_CONSUMER_SECRET=ABCDEF
6262
SF_CALLBACK_URI=https://test.app/callback
@@ -69,7 +69,6 @@ SF_PASSWORD=password123
6969
```
7070

7171
> For Lumen, you should copy the config file from `src/config/config.php` and add it to a `forrest.php` configuration file under a config directory in the root of your application.
72-
7372
> For Laravel 4, run `php artisan config:publish omniphx/forrest` which create `app/config/omniphx/forrest/config.php`
7473
7574
## Getting Started
@@ -125,7 +124,6 @@ Route::get('/authenticate', function()
125124
});
126125
```
127126

128-
129127
#### Client Credentials authentication flow
130128

131129
With the Client Credentials flow, you can directly authenticate with the `Forrest::authenticate()` method.
@@ -198,7 +196,7 @@ Next you need to pre-authorize a profile (As of now, can only do this step in Cl
198196
5. Go to Settings > Manage Users > Profiles and edit the profile of the associated user (i.e., Salesforce Administrator)
199197
6. Under 'Connected App Access' check the corresponding app name
200198

201-
The implementation is exactly the same as UserPassword
199+
The implementation is exactly the same as UserPassword (e.g., will need to explicitly specify a username and password)
202200

203201
```php
204202
Route::get('/authenticate', function()
@@ -208,6 +206,14 @@ Route::get('/authenticate', function()
208206
});
209207
```
210208

209+
For connecting to Lightning orgs you will need to configure an `instanceUrl` inside your `forrest.php` config:
210+
211+
```txt
212+
Lightning: https://<YOUR_ORG>.my.salesforce.com
213+
Lightning Sandbox: https://<YOUR_ORG>--<SANDBOX_NAME>.sandbox.my.salesforce.com
214+
Developer Org: https://<DEV_DOMAIN>.develop.my.salesforce.com
215+
```
216+
211217
#### Custom login urls
212218

213219
Sometimes users will need to connect to a sandbox or custom url. To do this, simply pass the url as an argument for the authenticatation method:

src/Omniphx/Forrest/Authentications/OAuthJWT.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ public static function getJWT($iss, $aud, $sub, $privateKey)
2121
return JWT::encode($payload, $privateKey, 'RS256');
2222
}
2323

24+
private function getDefaultInstanceURL()
25+
{
26+
if (isset($this->settings['instanceURL']) && !empty($this->settings['instanceURL'])) {
27+
return $this->settings['instanceURL'];
28+
} else {
29+
return $this->credentials['loginURL'];
30+
}
31+
}
32+
2433
public function authenticate($fullInstanceUrl = null)
2534
{
26-
$fullInstanceUrl = $fullInstanceUrl ?? $this->getInstanceURL() . '/services/oauth2/token';
35+
$fullInstanceUrl = $fullInstanceUrl ?? $this->getDefaultInstanceURL() . '/services/oauth2/token';
2736

2837
$consumerKey = $this->credentials['consumerKey'];
2938
$loginUrl = $this->credentials['loginURL'];

src/config/config.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@
7878
'version' => '',
7979

8080
/*
81-
* Optional (and not recommended) if you need to override the instance_url returned from Saleforce
81+
* Optional (and not recommended) if you need to override the instance_url returned from Salesforce
82+
*
83+
* This is useful for configuring lightning or lightning sandboxes with OAuthJWT:
84+
* Lightning: https://<YOUR_ORG>.my.salesforce.com
85+
* Lightning Sandbox: https://<YOUR_ORG>--<SANDBOX_NAME>.sandbox.my.salesforce.com
86+
* Developer Org: https://<DEV_DOMAIN>.develop.my.salesforce.com
8287
*/
8388
'instanceURL' => '',
8489

0 commit comments

Comments
 (0)