Skip to content

Commit f426cba

Browse files
committed
Add client command
1 parent 248fac7 commit f426cba

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

src/Console/ClientCommand.php

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace DesignMyNight\Mongodb\Console;
4+
5+
use DesignMyNight\Mongodb\Passport\Client;
6+
use Laravel\Passport\ClientRepository;
7+
use Laravel\Passport\Console\ClientCommand as PassportClientCommand;
8+
9+
class ClientCommand extends PassportClientCommand
10+
{
11+
/**
12+
* Create a new personal access client.
13+
*
14+
* @param \Laravel\Passport\ClientRepository $clients
15+
* @return void
16+
*/
17+
protected function createPersonalClient(ClientRepository $clients)
18+
{
19+
$name = $this->option('name') ?: $this->ask(
20+
'What should we name the personal access client?',
21+
config('app.name').' Personal Access Client'
22+
);
23+
24+
$client = $clients->createPersonalAccessClient(
25+
null, $name, 'http://localhost'
26+
);
27+
28+
$this->info('Personal access client created successfully.');
29+
30+
$this->patchedOutputClientDetails($client);
31+
}
32+
33+
/**
34+
* Create a new password grant client.
35+
*
36+
* @param \Laravel\Passport\ClientRepository $clients
37+
* @return void
38+
*/
39+
protected function createPasswordClient(ClientRepository $clients)
40+
{
41+
$name = $this->option('name') ?: $this->ask(
42+
'What should we name the password grant client?',
43+
config('app.name').' Password Grant Client'
44+
);
45+
46+
$client = $clients->createPasswordGrantClient(
47+
null, $name, 'http://localhost'
48+
);
49+
50+
$this->info('Password grant client created successfully.');
51+
52+
$this->patchedOutputClientDetails($client);
53+
}
54+
55+
/**
56+
* Create a client credentials grant client.
57+
*
58+
* @param \Laravel\Passport\ClientRepository $clients
59+
* @return void
60+
*/
61+
protected function createClientCredentialsClient(ClientRepository $clients)
62+
{
63+
$name = $this->option('name') ?: $this->ask(
64+
'What should we name the client?',
65+
config('app.name').' ClientCredentials Grant Client'
66+
);
67+
68+
$client = $clients->create(
69+
null, $name, ''
70+
);
71+
72+
$this->info('New client created successfully.');
73+
74+
$this->patchedOutputClientDetails($client);
75+
}
76+
77+
/**
78+
* Create a authorization code client.
79+
*
80+
* @param \Laravel\Passport\ClientRepository $clients
81+
* @return void
82+
*/
83+
protected function createAuthCodeClient(ClientRepository $clients)
84+
{
85+
$userId = $this->option('user_id') ?: $this->ask(
86+
'Which user ID should the client be assigned to?'
87+
);
88+
89+
$name = $this->option('name') ?: $this->ask(
90+
'What should we name the client?'
91+
);
92+
93+
$redirect = $this->option('redirect_uri') ?: $this->ask(
94+
'Where should we redirect the request after authorization?',
95+
url('/auth/callback')
96+
);
97+
98+
$client = $clients->create(
99+
$userId, $name, $redirect, false, false, ! $this->option('public')
100+
);
101+
102+
$this->info('New client created successfully.');
103+
104+
$this->patchedOutputClientDetails($client);
105+
}
106+
107+
/**
108+
* Output the client's ID and secret key.
109+
*
110+
* @param \Laravel\Passport\Client $client
111+
* @return void
112+
*/
113+
protected function patchedOutputClientDetails(Client $client)
114+
{
115+
$this->line('<comment>Client ID:</comment> '.$client->id);
116+
$this->line('<comment>Client secret:</comment> '.$client->secret);
117+
}
118+
}

src/MongodbPassportServiceProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace DesignMyNight\Mongodb;
44

5+
use DesignMyNight\Mongodb\Console\ClientCommand;
6+
use Illuminate\Foundation\Application;
57
use Illuminate\Support\ServiceProvider;
68
use DesignMyNight\Mongodb\Passport\AuthCode;
79
use DesignMyNight\Mongodb\Passport\Bridge\RefreshTokenRepository;
810
use DesignMyNight\Mongodb\Passport\Client;
911
use DesignMyNight\Mongodb\Passport\PersonalAccessClient;
10-
use DesignMyNight\Mongodb\Passport\RefreshToken;
1112
use DesignMyNight\Mongodb\Passport\Token;
1213
use Laravel\Passport\Bridge\RefreshTokenRepository as PassportRefreshTokenRepository;
14+
use Laravel\Passport\Console\ClientCommand as OriginalClientCommand;
1315
use Laravel\Passport\Passport;
1416

1517
class MongodbPassportServiceProvider extends ServiceProvider
@@ -27,5 +29,9 @@ public function register()
2729
$this->app->bind(PassportRefreshTokenRepository::class, function () {
2830
return $this->app->make(RefreshTokenRepository::class);
2931
});
32+
33+
$this->app->extend(OriginalClientCommand::class, function (Application $app) {
34+
return new ClientCommand();
35+
});
3036
}
3137
}

0 commit comments

Comments
 (0)