Skip to content

Commit c933a75

Browse files
committed
feature #16 add support for client name (azjezz)
This PR was merged into the 0.1-dev branch. Discussion ---------- add support for client name fixes: trikoder/oauth2-bundle#209 / trikoder/oauth2-bundle#145 `league/oauth2-server` uses both `name` and `identifier`, so it makes sense to do the same here. "name" doesn't have to be unique, it's the name of the oauth client which might be used at the user consent page so the user knows which application they are logging into. Commits ------- d3b8b1b add support for client name
2 parents 9f9edd0 + d3b8b1b commit c933a75

21 files changed

+69
-41
lines changed

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
33
totallyTyped="true"
4-
resolveFromConfigFile="true"
54
forbidEcho="true"
65
strictBinaryOperands="true"
76
phpVersion="7.1"

src/Command/CreateClientCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ protected function configure(): void
5757
'Sets allowed scope for client. Use this option multiple times to set multiple scopes.',
5858
[]
5959
)
60+
->addArgument(
61+
'name',
62+
InputArgument::REQUIRED,
63+
'The client name'
64+
)
6065
->addArgument(
6166
'identifier',
6267
InputArgument::OPTIONAL,
@@ -108,6 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
108113

109114
private function buildClientFromInput(InputInterface $input): Client
110115
{
116+
$name = $input->getArgument('name');
111117
/** @var string $identifier */
112118
$identifier = $input->getArgument('identifier') ?? hash('md5', random_bytes(16));
113119

@@ -120,7 +126,7 @@ private function buildClientFromInput(InputInterface $input): Client
120126
/** @var string $secret */
121127
$secret = $isPublic ? null : $input->getArgument('secret') ?? hash('sha512', random_bytes(32));
122128

123-
$client = new Client($identifier, $secret);
129+
$client = new Client($name, $identifier, $secret);
124130
$client->setActive(true);
125131
$client->setAllowPlainTextPkce($input->getOption('allow-plain-text-pkce'));
126132

src/League/Entity/Client.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ final class Client implements ClientEntityInterface
1818
*/
1919
private $allowPlainTextPkce = false;
2020

21-
/**
22-
* {@inheritdoc}
23-
*/
24-
public function getName(): string
21+
public function setName(string $name): void
2522
{
26-
return (string) $this->getIdentifier();
23+
$this->name = $name;
2724
}
2825

2926
/**

src/League/Repository/ClientRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getClientEntity($clientIdentifier)
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function validateClient($clientIdentifier, $clientSecret, $grantType)
41+
public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
4242
{
4343
$client = $this->clientManager->find($clientIdentifier);
4444

@@ -64,6 +64,7 @@ public function validateClient($clientIdentifier, $clientSecret, $grantType)
6464
private function buildClientEntity(ClientModel $client): ClientEntity
6565
{
6666
$clientEntity = new ClientEntity();
67+
$clientEntity->setName($client->getName());
6768
$clientEntity->setIdentifier($client->getIdentifier());
6869
$clientEntity->setRedirectUri(array_map('strval', $client->getRedirectUris()));
6970
$clientEntity->setConfidential($client->isConfidential());

src/Model/Client.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class Client
88
{
9+
/**
10+
* @var string
11+
*/
12+
private $name;
13+
914
/**
1015
* @var string
1116
*/
@@ -44,8 +49,9 @@ class Client
4449
/**
4550
* @psalm-mutation-free
4651
*/
47-
public function __construct(string $identifier, ?string $secret)
52+
public function __construct(string $name, string $identifier, ?string $secret)
4853
{
54+
$this->name = $name;
4955
$this->identifier = $identifier;
5056
$this->secret = $secret;
5157
}
@@ -58,6 +64,14 @@ public function __toString(): string
5864
return $this->getIdentifier();
5965
}
6066

67+
/**
68+
* @psalm-mutation-free
69+
*/
70+
public function getName(): string
71+
{
72+
return $this->name;
73+
}
74+
6175
/**
6276
* @psalm-mutation-free
6377
*/

src/Resources/config/doctrine/model/Client.orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
77
<entity name="League\Bundle\OAuth2ServerBundle\Model\Client" table="oauth2_client">
88
<id name="identifier" type="string" length="32" />
9+
<field name="name" type="string" length="128" />
910
<field name="secret" type="string" length="128" nullable="true" />
1011
<field name="redirectUris" type="oauth2_redirect_uri" nullable="true" />
1112
<field name="grants" type="oauth2_grant" nullable="true" />

tests/Acceptance/CreateClientCommandTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testCreateClient(): void
1616
$commandTester = new CommandTester($command);
1717
$commandTester->execute([
1818
'command' => $command->getName(),
19+
'name' => 'My Awesome OAuth Client',
1920
]);
2021

2122
$output = $commandTester->getDisplay();
@@ -28,6 +29,7 @@ public function testCreateClientWithIdentifier(): void
2829
$commandTester = new CommandTester($command);
2930
$commandTester->execute([
3031
'command' => $command->getName(),
32+
'name' => 'My Awesome OAuth Client',
3133
'identifier' => 'foobar',
3234
]);
3335

@@ -41,6 +43,7 @@ public function testCreateClientWithIdentifier(): void
4143
->get(ClientManagerInterface::class)
4244
->find('foobar');
4345
$this->assertInstanceOf(Client::class, $client);
46+
$this->assertSame('My Awesome OAuth Client', $client->getName());
4447
$this->assertTrue($client->isConfidential());
4548
$this->assertNotEmpty($client->getSecret());
4649
$this->assertFalse($client->isPlainTextPkceAllowed());
@@ -53,6 +56,7 @@ public function testCreatePublicClientWithIdentifier(): void
5356
$commandTester = new CommandTester($command);
5457
$commandTester->execute([
5558
'command' => $command->getName(),
59+
'name' => 'My Awesome OAuth Client',
5660
'identifier' => $clientIdentifier,
5761
'--public' => true,
5862
]);
@@ -81,6 +85,7 @@ public function testCannotCreatePublicClientWithSecret(): void
8185
$commandTester = new CommandTester($command);
8286
$commandTester->execute([
8387
'command' => $command->getName(),
88+
'name' => 'My Awesome OAuth Client',
8489
'identifier' => $clientIdentifier,
8590
'secret' => 'foo',
8691
'--public' => true,
@@ -105,6 +110,7 @@ public function testCreateClientWithSecret(): void
105110
$commandTester = new CommandTester($command);
106111
$commandTester->execute([
107112
'command' => $command->getName(),
113+
'name' => 'My Awesome OAuth Client',
108114
'identifier' => 'foobar',
109115
'secret' => 'quzbaz',
110116
]);
@@ -129,6 +135,7 @@ public function testCreateClientWhoIsAllowedToUsePlainPkceChallengeMethod(): voi
129135
$commandTester = new CommandTester($command);
130136
$commandTester->execute([
131137
'command' => $command->getName(),
138+
'name' => 'My Awesome OAuth Client',
132139
'identifier' => 'foobar-123',
133140
'--allow-plain-text-pkce' => true,
134141
]);
@@ -151,6 +158,7 @@ public function testCreateClientWithRedirectUris(): void
151158
$commandTester = new CommandTester($command);
152159
$commandTester->execute([
153160
'command' => $command->getName(),
161+
'name' => 'My Awesome OAuth Client',
154162
'identifier' => 'foobar',
155163
'--redirect-uri' => ['http://example.org', 'http://example.org'],
156164
]);
@@ -171,6 +179,7 @@ public function testCreateClientWithGrantTypes(): void
171179
$commandTester = new CommandTester($command);
172180
$commandTester->execute([
173181
'command' => $command->getName(),
182+
'name' => 'My Awesome OAuth Client',
174183
'identifier' => 'foobar',
175184
'--grant-type' => ['password', 'client_credentials'],
176185
]);
@@ -191,6 +200,7 @@ public function testCreateClientWithScopes(): void
191200
$commandTester = new CommandTester($command);
192201
$commandTester->execute([
193202
'command' => $command->getName(),
203+
'name' => 'My Awesome OAuth Client',
194204
'identifier' => 'foobar',
195205
'--scope' => ['foo', 'bar'],
196206
]);

tests/Acceptance/DeleteClientCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class DeleteClientCommandTest extends AbstractAcceptanceTest
1616
{
1717
public function testDeleteClient(): void
1818
{
19-
$client = $this->fakeAClient('foobar');
19+
$client = $this->fakeAClient('foo', 'foobar');
2020
$this->getClientManager()->save($client);
2121

2222
$command = $this->command();
@@ -54,9 +54,9 @@ private function findClient(string $identifier): ?Client
5454
;
5555
}
5656

57-
private function fakeAClient(string $identifier): Client
57+
private function fakeAClient(string $name, string $identifier): Client
5858
{
59-
return new Client($identifier, 'quzbaz');
59+
return new Client($name, $identifier, 'quzbaz');
6060
}
6161

6262
private function getClientManager(): ClientManagerInterface

tests/Acceptance/DoctrineAccessTokenManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testClearExpired(): void
2323

2424
$doctrineAccessTokenManager = new DoctrineAccessTokenManager($em);
2525

26-
$client = new Client('client', 'secret');
26+
$client = new Client('client', 'client', 'secret');
2727
$em->persist($client);
2828
$em->flush();
2929

@@ -80,7 +80,7 @@ public function testClearExpiredWithRefreshToken(): void
8080
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8181
$doctrineAccessTokenManager = new DoctrineAccessTokenManager($em);
8282

83-
$client = new Client('client', 'secret');
83+
$client = new Client('client', 'client', 'secret');
8484
$em->persist($client);
8585
$em->flush();
8686

tests/Acceptance/DoctrineAuthCodeManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testClearExpired(): void
2222

2323
$doctrineAuthCodeManager = new DoctrineAuthCodeManager($em);
2424

25-
$client = new Client('client', 'secret');
25+
$client = new Client('client', 'client', 'secret');
2626
$em->persist($client);
2727

2828
$testData = $this->buildClearExpiredTestData($client);

0 commit comments

Comments
 (0)