forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98456cb
commit d5f7cdc
Showing
5 changed files
with
247 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
namespace Laravel\Passport\Tests\Feature; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Illuminate\Contracts\Hashing\Hasher; | ||
use Illuminate\Database\Eloquent\Factory; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Laravel\Passport\Client; | ||
use Laravel\Passport\ClientRepository; | ||
use Laravel\Passport\HasApiTokens; | ||
use Laravel\Passport\Token; | ||
use Laravel\Passport\TokenRepository; | ||
use Lcobucci\JWT\Parser; | ||
|
||
class AccessTokenControllerTest extends PassportTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
$table->string('password'); | ||
$table->dateTime('created_at'); | ||
$table->dateTime('updated_at'); | ||
}); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Schema::dropIfExists('users'); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
protected function getUserClass(): ?string | ||
{ | ||
return User::class; | ||
} | ||
|
||
public function testGettingAccessTokenWithPasswordGrant() | ||
{ | ||
$this->withoutExceptionHandling(); | ||
|
||
$password = 'foobar123'; | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->password = $this->app->make(Hasher::class)->make($password); | ||
$user->save(); | ||
|
||
/** @var Client $client */ | ||
$client = $this->app->make(Factory::class)->of(Client::class)->state('password_client')->create(['user_id' => $user->id]); | ||
|
||
$response = $this->post( | ||
'/oauth/token', | ||
[ | ||
'grant_type' => 'password', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret, | ||
'username' => $user->email, | ||
'password' => $password, | ||
] | ||
); | ||
|
||
$response->assertOk(); | ||
|
||
$response->assertHeader('pragma', 'no-cache'); | ||
$response->assertHeader('cache-control', 'no-store, private'); | ||
$response->assertHeader('content-type', 'application/json; charset=UTF-8'); | ||
|
||
$decodedResponse = $response->decodeResponseJson(); | ||
|
||
$this->assertArrayHasKey('token_type', $decodedResponse); | ||
$this->assertArrayHasKey('expires_in', $decodedResponse); | ||
$this->assertArrayHasKey('access_token', $decodedResponse); | ||
$this->assertArrayHasKey('refresh_token', $decodedResponse); | ||
$this->assertSame('Bearer', $decodedResponse['token_type']); | ||
$expiresInSeconds = 31622400; | ||
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5); | ||
|
||
$jwtAccessToken = (new Parser())->parse($decodedResponse['access_token']); | ||
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->getClaim('aud'))->is($client)); | ||
$this->assertTrue($this->app->make('auth')->createUserProvider()->retrieveById($jwtAccessToken->getClaim('sub'))->is($user)); | ||
|
||
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti')); | ||
$this->assertInstanceOf(Token::class, $token); | ||
$this->assertFalse($token->revoked); | ||
$this->assertTrue($token->user->is($user)); | ||
$this->assertTrue($token->client->is($client)); | ||
$this->assertNull($token->name); | ||
$this->assertLessThanOrEqual(5, CarbonImmutable::now()->addSeconds($expiresInSeconds)->diffInSeconds($token->expires_at)); | ||
} | ||
|
||
public function testGettingAccessTokenWithPasswordGrantWithInvalidPassword() | ||
{ | ||
$password = 'foobar123'; | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->password = $this->app->make(Hasher::class)->make($password); | ||
$user->save(); | ||
|
||
/** @var Client $client */ | ||
$client = $this->app->make(Factory::class)->of(Client::class)->state('password_client')->create(['user_id' => $user->id]); | ||
|
||
$response = $this->post( | ||
'/oauth/token', | ||
[ | ||
'grant_type' => 'password', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret, | ||
'username' => $user->email, | ||
'password' => $password.'foo', | ||
] | ||
); | ||
|
||
$response->assertStatus(400); | ||
|
||
$response->assertHeader('cache-control', 'no-cache, private'); | ||
$response->assertHeader('content-type', 'application/json'); | ||
|
||
$decodedResponse = $response->decodeResponseJson(); | ||
|
||
$this->assertArrayNotHasKey('token_type', $decodedResponse); | ||
$this->assertArrayNotHasKey('expires_in', $decodedResponse); | ||
$this->assertArrayNotHasKey('access_token', $decodedResponse); | ||
$this->assertArrayNotHasKey('refresh_token', $decodedResponse); | ||
|
||
$this->assertArrayHasKey('error', $decodedResponse); | ||
$this->assertSame('invalid_grant', $decodedResponse['error']); | ||
$this->assertArrayHasKey('error_description', $decodedResponse); | ||
$this->assertArrayHasKey('hint', $decodedResponse); | ||
$this->assertArrayHasKey('message', $decodedResponse); | ||
|
||
$this->assertSame(0, Token::count()); | ||
} | ||
|
||
public function testGettingAccessTokenWithPasswordGrantWithInvalidClientSecret() | ||
{ | ||
$password = 'foobar123'; | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->password = $this->app->make(Hasher::class)->make($password); | ||
$user->save(); | ||
|
||
/** @var Client $client */ | ||
$client = $this->app->make(Factory::class)->of(Client::class)->state('password_client')->create(['user_id' => $user->id]); | ||
|
||
$response = $this->post( | ||
'/oauth/token', | ||
[ | ||
'grant_type' => 'password', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret.'foo', | ||
'username' => $user->email, | ||
'password' => $password, | ||
] | ||
); | ||
|
||
$response->assertStatus(401); | ||
|
||
$response->assertHeader('cache-control', 'no-cache, private'); | ||
$response->assertHeader('content-type', 'application/json'); | ||
|
||
$decodedResponse = $response->decodeResponseJson(); | ||
|
||
$this->assertArrayNotHasKey('token_type', $decodedResponse); | ||
$this->assertArrayNotHasKey('expires_in', $decodedResponse); | ||
$this->assertArrayNotHasKey('access_token', $decodedResponse); | ||
$this->assertArrayNotHasKey('refresh_token', $decodedResponse); | ||
|
||
$this->assertArrayHasKey('error', $decodedResponse); | ||
$this->assertSame('invalid_client', $decodedResponse['error']); | ||
$this->assertArrayHasKey('error_description', $decodedResponse); | ||
$this->assertSame('Client authentication failed', $decodedResponse['error_description']); | ||
$this->assertArrayNotHasKey('hint', $decodedResponse); | ||
$this->assertArrayHasKey('message', $decodedResponse); | ||
$this->assertSame('Client authentication failed', $decodedResponse['message']); | ||
|
||
$this->assertSame(0, Token::count()); | ||
} | ||
} | ||
|
||
class User extends \Illuminate\Foundation\Auth\User | ||
{ | ||
use HasApiTokens; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters