Skip to content

[10.x] Client Credentials Feature tests #1341

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions database/factories/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ public function asPasswordClient()
'password_client' => true,
]);
}

/**
* Use as Client Credentials.
*
* @return $this
*/
public function asClientCredentials()
{
return $this->state([
'personal_access_client' => false,
'password_client' => false,
]);
}
}
89 changes: 89 additions & 0 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,95 @@ protected function getUserClass()
return User::class;
}

public function testGettingAccessTokenWithClientCredentialsGrant()
{
$this->withoutExceptionHandling();

$user = new User();
$user->email = 'foo@gmail.com';
$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();

$response->assertHeader('pragma', 'no-cache');
$response->assertHeader('cache-control', 'no-store, private');
$response->assertHeader('content-type', 'application/json; charset=UTF-8');

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

$this->assertArrayHasKey('token_type', $decodedResponse);
$this->assertArrayHasKey('expires_in', $decodedResponse);
$this->assertArrayHasKey('access_token', $decodedResponse);
$this->assertSame('Bearer', $decodedResponse['token_type']);
$expiresInSeconds = 31536000;
$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));

$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti'));
$this->assertInstanceOf(Token::class, $token);
$this->assertTrue($token->client->is($client));
$this->assertFalse($token->revoked);
$this->assertNull($token->name);
$this->assertNull($token->user_id);
$this->assertLessThanOrEqual(5, CarbonImmutable::now()->addSeconds($expiresInSeconds)->diffInSeconds($token->expires_at));
}

public function testGettingAccessTokenWithClientCredentialsGrantInvalidClientSecret()
{
$user = new User();
$user->email = 'foo@gmail.com';
$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.'foo',
]
);

$response->assertStatus(401);

$response->assertHeader('cache-control', 'no-cache, private');
$response->assertHeader('content-type', 'application/json');

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

$this->assertArrayNotHasKey('token_type', $decodedResponse);
$this->assertArrayNotHasKey('expires_in', $decodedResponse);
$this->assertArrayNotHasKey('access_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());
}

public function testGettingAccessTokenWithPasswordGrant()
{
$this->withoutExceptionHandling();
Expand Down