Skip to content

Commit e46ebc5

Browse files
committed
Implement personal access client config
1 parent 08bf389 commit e46ebc5

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

UPGRADE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ PR: https://github.com/laravel/passport/pull/1145
1818

1919
Client secrets may now be stored using a Bcrypt hash. However, before enabling this functionality, please consider the following. First, there is no way to reverse the hashing process once you have migrated your existing tokens. Secondly, when hashing client secrets, you will only have one opportunity to display the plain-text value to the user before it is hashed and stored in the database.
2020

21+
#### Personal Access Client
22+
23+
Before you continue, there's a special case for personal access clients. You should set your personal access client ID and unhashed secret in your `.env` file:
24+
25+
PASSPORT_PERSONAL_ACCESS_CLIENT_ID=
26+
PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=
27+
28+
After this, you should set register them with the `Passport` instance by playing the following calls within the `boot` method of your `AppServiceProvider`:
29+
30+
Passport::personalAccessClientId(config('passport.personal_access_token.id'));
31+
Passport::personalAccessClientSecret(config('passport.personal_access_token.secret'));
32+
33+
Make sure to do this before hashing your secrets using the step below, otherwise they'll be lost forever.
34+
35+
#### Hashing Existing Secrets
36+
2137
You may enable client secret hashing by calling the `Passport::hashClientSecrets()` method within the `boot` method of your `AppServiceProvider`. For convenience, we've included a new Artisan command which you can run to hash all existing client secrets:
2238

2339
php artisan passport:hash

config/passport.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@
3030

3131
'client_uuids' => false,
3232

33+
/*
34+
|--------------------------------------------------------------------------
35+
| Personal Access Client
36+
|--------------------------------------------------------------------------
37+
|
38+
| If you enable client hashing, you should set the personal access
39+
| client id and secret in your config file. This way they will be
40+
| used when you issue access tokens to your users.
41+
|
42+
*/
43+
44+
'personal_access_client' => [
45+
'id' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_ID'),
46+
'secret' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET'),
47+
],
48+
3349
];

src/Passport.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ class Passport
2222
/**
2323
* The personal access token client ID.
2424
*
25-
* @var int
25+
* @var int|string
2626
*/
2727
public static $personalAccessClientId;
2828

29+
/**
30+
* The personal access token client secret.
31+
*
32+
* @var string
33+
*/
34+
public static $personalAccessClientSecret;
35+
2936
/**
3037
* The default scope.
3138
*
@@ -192,7 +199,7 @@ public static function routes($callback = null, array $options = [])
192199
/**
193200
* Set the client ID that should be used to issue personal access tokens.
194201
*
195-
* @param int $clientId
202+
* @param int|string $clientId
196203
* @return static
197204
*/
198205
public static function personalAccessClientId($clientId)
@@ -202,6 +209,19 @@ public static function personalAccessClientId($clientId)
202209
return new static;
203210
}
204211

212+
/**
213+
* Set the client secret that should be used to issue personal access tokens.
214+
*
215+
* @param string $clientSecret
216+
* @return static
217+
*/
218+
public static function personalAccessClientSecret($clientSecret)
219+
{
220+
static::$personalAccessClientSecret = $clientSecret;
221+
222+
return new static;
223+
}
224+
205225
/**
206226
* Set the default scope(s). Multiple scopes may be an array or specified delimited by spaces.
207227
*

src/PersonalAccessTokenFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ public function make($userId, $name, array $scopes = [])
9393
*/
9494
protected function createRequest($client, $userId, array $scopes)
9595
{
96+
$secret = Passport::$hashesClientSecrets ? Passport::$personalAccessClientSecret : $client->secret;
97+
9698
return (new ServerRequest)->withParsedBody([
9799
'grant_type' => 'personal_access',
98100
'client_id' => $client->id,
99-
'client_secret' => $client->secret,
101+
'client_secret' => $secret,
100102
'user_id' => $userId,
101103
'scope' => implode(' ', $scopes),
102104
]);

0 commit comments

Comments
 (0)