Skip to content

Commit 8fedbbd

Browse files
committed
Merge branch '1.5'
2 parents 8979144 + fa16b79 commit 8fedbbd

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

Command/CreateClientCommand.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSOAuthServerBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\OAuthServerBundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Style\SymfonyStyle;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
19+
20+
class CreateClientCommand extends ContainerAwareCommand
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
protected function configure()
26+
{
27+
$this
28+
->setName('fos:oauth-server:create-client')
29+
->setDescription('Creates a new client')
30+
->addOption(
31+
'redirect-uri',
32+
null,
33+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
34+
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
35+
null
36+
)
37+
->addOption(
38+
'grant-type',
39+
null,
40+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
41+
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types..',
42+
null
43+
)
44+
->setHelp(<<<EOT
45+
The <info>%command.name%</info> command creates a new client.
46+
47+
<info>php %command.full_name% [--redirect-uri=...] [--grant-type=...]</info>
48+
49+
EOT
50+
);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function execute(InputInterface $input, OutputInterface $output)
57+
{
58+
$io = new SymfonyStyle($input, $output);
59+
60+
$io->title('Client Credentials');
61+
62+
// Get the client manager
63+
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
64+
65+
// Create a new client
66+
$client = $clientManager->createClient();
67+
68+
$client->setRedirectUris($input->getOption('redirect-uri'));
69+
$client->setAllowedGrantTypes($input->getOption('grant-type'));
70+
71+
// Save the client
72+
$clientManager->updateClient($client);
73+
74+
// Give the credentials back to the user
75+
$headers = ['Client ID', 'Client Secret'];
76+
$rows = [
77+
[$client->getPublicId(), $client->getSecret()],
78+
];
79+
80+
$io->table($headers, $rows);
81+
82+
return 0;
83+
}
84+
}

Resources/doc/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ fos_oauth_server:
566566

567567
## Creating A Client
568568

569+
### Console Command
570+
571+
The most convenient way to create a client is to use the console command.
572+
573+
$ php app/console fos:oauth-server:create-client --redirect-uri="..." --grant-type="..."
574+
575+
Note: you can use `--redirect-uri` and `--grant-type` multiple times to add additional values.
576+
577+
### Programatically
578+
569579
Before you can generate tokens, you need to create a Client using the ClientManager.
570580

571581
``` php

Security/Firewall/OAuthListener.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function __construct($securityContext, AuthenticationManagerInterface $au
5454
if (!$securityContext instanceof TokenStorageInterface && !$securityContext instanceof SecurityContextInterface) {
5555
throw new \InvalidArgumentException('Wrong type for OAuthListener, it has to implement TokenStorageInterface or SecurityContextInterface');
5656
}
57+
if ($securityContext instanceof SecurityContextInterface) {
58+
@trigger_error(sprintf('Injecting SecurityContextInterface into %1$s::__construct is deprecated since 1.6 and will be removed in 2.0.', __CLASS__), E_USER_DEPRECATED);
59+
}
5760
$this->securityContext = $securityContext;
5861
$this->authenticationManager = $authenticationManager;
5962
$this->serverService = $serverService;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSOAuthServerBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\OAuthServerBundle\Tests\Command;
13+
14+
use FOS\OAuthServerBundle\Command\CreateClientCommand;
15+
use FOS\OAuthServerBundle\Tests\TestCase;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\DependencyInjection\Container;
19+
20+
class CreateClientCommandTest extends TestCase
21+
{
22+
/**
23+
* @var
24+
*/
25+
private $command;
26+
27+
/**
28+
* @var Container
29+
*/
30+
private $container;
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function setUp()
36+
{
37+
$command = new CreateClientCommand();
38+
39+
$application = new Application();
40+
$application->add($command);
41+
42+
$this->container = new Container();
43+
44+
$this->command = $application->find($command->getName());
45+
$this->command->setContainer($this->container);
46+
}
47+
48+
/**
49+
* @dataProvider classProvider
50+
*
51+
* @param string $class A fully qualified class name.
52+
*/
53+
public function testItShouldCreateClient($clientManager, $client)
54+
{
55+
$clientManager = $this
56+
->getMockBuilder($clientManager)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
60+
$clientManager
61+
->expects($this->any())
62+
->method('createClient')
63+
->will($this->returnValue(new $client));
64+
65+
$this->container->set('fos_oauth_server.client_manager.default', $clientManager);
66+
67+
$commandTester = new CommandTester($this->command);
68+
69+
$commandTester->execute([
70+
'command' => $this->command->getName(),
71+
'--redirect-uri' => ['https://www.example.com/oauth2/callback'],
72+
'--grant-type' => [
73+
'authorization_code',
74+
'password',
75+
'refresh_token',
76+
'token',
77+
'client_credentials',
78+
],
79+
]);
80+
81+
$this->assertEquals(0, $commandTester->getStatusCode());
82+
83+
$output = $commandTester->getDisplay();
84+
85+
$this->assertContains('Client ID', $output);
86+
$this->assertContains('Client Secret', $output);
87+
}
88+
89+
/**
90+
* @return array
91+
*/
92+
public function classProvider()
93+
{
94+
return [
95+
['FOS\OAuthServerBundle\Document\ClientManager', 'FOS\OAuthServerBundle\Document\Client'],
96+
['FOS\OAuthServerBundle\Entity\ClientManager', 'FOS\OAuthServerBundle\Entity\Client'],
97+
['FOS\OAuthServerBundle\Model\ClientManager', 'FOS\OAuthServerBundle\Model\Client'],
98+
['FOS\OAuthServerBundle\Propel\ClientManager', 'FOS\OAuthServerBundle\Propel\Client'],
99+
];
100+
}
101+
}

0 commit comments

Comments
 (0)