Skip to content

Commit ba1996c

Browse files
yaroslawwwdriesvintstaylorotwell
authored
[10.x] Allow to use custom authorization server response (#1521)
* Allow to use custom authorization server response * Update src/Passport.php Remove direct initialisation as "null" Co-authored-by: Dries Vints <[email protected]> * Update Passport.php Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent b4a829e commit ba1996c

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/Passport.php

+7
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ class Passport
182182
*/
183183
public static $withInheritedScopes = false;
184184

185+
/**
186+
* The authorization server response type.
187+
*
188+
* @var \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface|null
189+
*/
190+
public static $authorizationServerResponseType;
191+
185192
/**
186193
* Enable the implicit grant type.
187194
*

src/PassportServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ public function makeAuthorizationServer()
212212
$this->app->make(Bridge\AccessTokenRepository::class),
213213
$this->app->make(Bridge\ScopeRepository::class),
214214
$this->makeCryptKey('private'),
215-
app('encrypter')->getKey()
215+
app('encrypter')->getKey(),
216+
Passport::$authorizationServerResponseType
216217
);
217218
}
218219

tests/Feature/AccessTokenControllerTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Laravel\Passport\ClientRepository;
1111
use Laravel\Passport\Database\Factories\ClientFactory;
1212
use Laravel\Passport\HasApiTokens;
13+
use Laravel\Passport\Passport;
1314
use Laravel\Passport\Token;
1415
use Laravel\Passport\TokenRepository;
1516
use Lcobucci\JWT\Configuration;
@@ -270,9 +271,65 @@ public function testGettingAccessTokenWithPasswordGrantWithInvalidClientSecret()
270271

271272
$this->assertSame(0, Token::count());
272273
}
274+
275+
public function testGettingCustomResponseType()
276+
{
277+
$this->withoutExceptionHandling();
278+
Passport::$authorizationServerResponseType = new IdTokenResponse('foo_bar_open_id_token');
279+
280+
$user = new User();
281+
$user->email = '[email protected]';
282+
$user->password = $this->app->make(Hasher::class)->make('foobar123');
283+
$user->save();
284+
285+
/** @var Client $client */
286+
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->id]);
287+
288+
$response = $this->post(
289+
'/oauth/token',
290+
[
291+
'grant_type' => 'client_credentials',
292+
'client_id' => $client->id,
293+
'client_secret' => $client->secret,
294+
]
295+
);
296+
297+
$response->assertOk();
298+
299+
$decodedResponse = $response->decodeResponseJson()->json();
300+
301+
$this->assertArrayHasKey('id_token', $decodedResponse);
302+
$this->assertSame('foo_bar_open_id_token', $decodedResponse['id_token']);
303+
}
273304
}
274305

275306
class User extends \Illuminate\Foundation\Auth\User
276307
{
277308
use HasApiTokens;
278309
}
310+
311+
class IdTokenResponse extends \League\OAuth2\Server\ResponseTypes\BearerTokenResponse
312+
{
313+
/**
314+
* @var string Id token.
315+
*/
316+
protected $idToken;
317+
318+
/**
319+
* @param string $idToken
320+
*/
321+
public function __construct($idToken)
322+
{
323+
$this->idToken = $idToken;
324+
}
325+
326+
/**
327+
* @inheritdoc
328+
*/
329+
protected function getExtraParams(\League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken)
330+
{
331+
return [
332+
'id_token' => $this->idToken,
333+
];
334+
}
335+
}

0 commit comments

Comments
 (0)