Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allow to use custom authorization server response #1521

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Passport
//
];

/**
* The authorization server response formatter.
*
* @var \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface|null
*/
public static $authorizationServerResponseType = null;
yaroslawww marked this conversation as resolved.
Show resolved Hide resolved

/**
* The date when access tokens expire.
*
Expand Down
3 changes: 2 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ public function makeAuthorizationServer()
$this->app->make(Bridge\AccessTokenRepository::class),
$this->app->make(Bridge\ScopeRepository::class),
$this->makeCryptKey('private'),
app('encrypter')->getKey()
app('encrypter')->getKey(),
Passport::$authorizationServerResponseType
);
}

Expand Down
57 changes: 57 additions & 0 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Database\Factories\ClientFactory;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Passport;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Configuration;
Expand Down Expand Up @@ -270,9 +271,65 @@ public function testGettingAccessTokenWithPasswordGrantWithInvalidClientSecret()

$this->assertSame(0, Token::count());
}

public function testGettingCustomResponseType()
{
$this->withoutExceptionHandling();
Passport::$authorizationServerResponseType = new IdTokenResponse('foo_bar_open_id_token');

$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make('foobar123');
$user->save();

/** @var Client $client */
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->id]);

$response = $this->post(
'/oauth/token',
[
'grant_type' => 'client_credentials',
'client_id' => $client->id,
'client_secret' => $client->secret,
]
);

$response->assertOk();

$decodedResponse = $response->decodeResponseJson()->json();

$this->assertArrayHasKey('id_token', $decodedResponse);
$this->assertSame('foo_bar_open_id_token', $decodedResponse['id_token']);
}
}

class User extends \Illuminate\Foundation\Auth\User
{
use HasApiTokens;
}

class IdTokenResponse extends \League\OAuth2\Server\ResponseTypes\BearerTokenResponse
{
/**
* @var string Id token.
*/
protected $idToken;

/**
* @param string $idToken
*/
public function __construct($idToken)
{
$this->idToken = $idToken;
}

/**
* @inheritdoc
*/
protected function getExtraParams(\League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken)
{
return [
'id_token' => $this->idToken,
];
}
}