@@ -23,7 +23,7 @@ class ClientCommand extends Command
2323 {--name= : The name of the client}
2424 {--provider= : The name of the user provider}
2525 {--redirect_uri= : The URI to redirect to after authorization }
26- {--public : Create a public client (Auth code grant type only ) } ' ;
26+ {--public : Create a public client (without secret ) } ' ;
2727
2828 /**
2929 * The console command description.
@@ -34,158 +34,107 @@ class ClientCommand extends Command
3434
3535 /**
3636 * Execute the console command.
37- *
38- * @param \Laravel\Passport\ClientRepository $clients
39- * @return void
4037 */
41- public function handle (ClientRepository $ clients )
38+ public function handle (ClientRepository $ clients ): void
4239 {
43- if ($ this ->option ('personal ' )) {
44- $ this ->createPersonalAccessClient ($ clients );
45- } elseif ($ this ->option ('password ' )) {
46- $ this ->createPasswordClient ($ clients );
47- } elseif ($ this ->option ('client ' )) {
48- $ this ->createClientCredentialsClient ($ clients );
49- } elseif ($ this ->option ('implicit ' )) {
50- $ this ->createImplicitClient ($ clients );
51- } else {
52- $ this ->createAuthCodeClient ($ clients );
40+ if (! $ this ->hasOption ('name ' )) {
41+ $ this ->input ->setOption ('name ' , $ this ->ask (
42+ 'What should we name the client? ' ,
43+ config ('app.name ' )
44+ ));
45+ }
46+
47+ $ client = match (true ) {
48+ $ this ->option ('personal ' ) => $ this ->createPersonalAccessClient ($ clients ),
49+ $ this ->option ('password ' ) => $ this ->createPasswordClient ($ clients ),
50+ $ this ->option ('client ' ) => $ this ->createClientCredentialsClient ($ clients ),
51+ $ this ->option ('implicit ' ) => $ this ->createImplicitClient ($ clients ),
52+ default => $ this ->createAuthCodeClient ($ clients )
53+ };
54+
55+ $ this ->components ->info ('New client created successfully. ' );
56+
57+ if ($ client ) {
58+ $ this ->components ->twoColumnDetail ('Client ID ' , $ client ->getKey ());
59+
60+ if ($ client ->confidential ()) {
61+ $ this ->components ->twoColumnDetail ('Client Secret ' , $ client ->plainSecret );
62+ $ this ->components ->warn ('The client secret will not be shown again, so don \'t lose it! ' );
63+ }
5364 }
5465 }
5566
5667 /**
5768 * Create a new personal access client.
58- *
59- * @param \Laravel\Passport\ClientRepository $clients
60- * @return void
6169 */
62- protected function createPersonalAccessClient (ClientRepository $ clients )
70+ protected function createPersonalAccessClient (ClientRepository $ clients ): ? Client
6371 {
64- $ name = $ this ->option ('name ' ) ?: $ this ->ask (
65- 'What should we name the client? ' ,
66- config ('app.name ' ).' Personal Access Grant Client '
67- );
68-
6972 $ provider = $ this ->option ('provider ' ) ?: $ this ->choice (
7073 'Which user provider should this client use to retrieve users? ' ,
7174 collect (config ('auth.guards ' ))->where ('driver ' , 'passport ' )->pluck ('provider ' )->all (),
7275 config ('auth.guards.api.provider ' )
7376 );
7477
75- $ clients ->createPersonalAccessGrantClient ($ name , $ provider );
78+ $ clients ->createPersonalAccessGrantClient ($ this -> option ( ' name ' ) , $ provider );
7679
77- $ this -> components -> info ( ' Personal access client created successfully. ' ) ;
80+ return null ;
7881 }
7982
8083 /**
8184 * Create a new password grant client.
82- *
83- * @param \Laravel\Passport\ClientRepository $clients
84- * @return void
8585 */
86- protected function createPasswordClient (ClientRepository $ clients )
86+ protected function createPasswordClient (ClientRepository $ clients ): Client
8787 {
88- $ name = $ this ->option ('name ' ) ?: $ this ->ask (
89- 'What should we name the client? ' ,
90- config ('app.name ' ).' Password Grant Client '
91- );
92-
9388 $ provider = $ this ->option ('provider ' ) ?: $ this ->choice (
9489 'Which user provider should this client use to retrieve users? ' ,
9590 collect (config ('auth.guards ' ))->where ('driver ' , 'passport ' )->pluck ('provider ' )->all (),
9691 config ('auth.guards.api.provider ' )
9792 );
9893
99- $ client = $ clients ->createPasswordGrantClient ($ name , $ provider );
94+ $ confidential = $ this ->hasOption ('public ' )
95+ ? ! $ this ->option ('public ' )
96+ : $ this ->confirm ('Would you like to make this client confidential? ' );
10097
101- $ this ->components ->info ('Password grant client created successfully. ' );
102-
103- $ this ->outputClientDetails ($ client );
98+ return $ clients ->createPasswordGrantClient ($ this ->option ('name ' ), $ provider , $ confidential );
10499 }
105100
106101 /**
107102 * Create a client credentials grant client.
108- *
109- * @param \Laravel\Passport\ClientRepository $clients
110- * @return void
111103 */
112- protected function createClientCredentialsClient (ClientRepository $ clients )
104+ protected function createClientCredentialsClient (ClientRepository $ clients ): Client
113105 {
114- $ name = $ this ->option ('name ' ) ?: $ this ->ask (
115- 'What should we name the client? ' ,
116- config ('app.name ' ).' Client Credentials Grant Client '
117- );
118-
119- $ client = $ clients ->createClientCredentialsGrantClient ($ name );
120-
121- $ this ->components ->info ('New client created successfully. ' );
122-
123- $ this ->outputClientDetails ($ client );
106+ return $ clients ->createClientCredentialsGrantClient ($ this ->option ('name ' ));
124107 }
125108
126109 /**
127110 * Create an implicit grant client.
128- *
129- * @param \Laravel\Passport\ClientRepository $clients
130- * @return void
131111 */
132- protected function createImplicitClient (ClientRepository $ clients )
112+ protected function createImplicitClient (ClientRepository $ clients ): Client
133113 {
134- $ name = $ this ->option ('name ' ) ?: $ this ->ask (
135- 'What should we name the client? ' ,
136- config ('app.name ' ).' Implicit Grant Client '
137- );
138-
139114 $ redirect = $ this ->option ('redirect_uri ' ) ?: $ this ->ask (
140115 'Where should we redirect the request after authorization? ' ,
141116 url ('/auth/callback ' )
142117 );
143118
144- $ client = $ clients ->createImplicitGrantClient ($ name , explode (', ' , $ redirect ));
145-
146- $ this ->components ->info ('New client created successfully. ' );
147-
148- $ this ->outputClientDetails ($ client );
119+ return $ clients ->createImplicitGrantClient ($ this ->option ('name ' ), explode (', ' , $ redirect ));
149120 }
150121
151122 /**
152- * Create a authorization code client.
153- *
154- * @param \Laravel\Passport\ClientRepository $clients
155- * @return void
123+ * Create an authorization code client.
156124 */
157- protected function createAuthCodeClient (ClientRepository $ clients )
125+ protected function createAuthCodeClient (ClientRepository $ clients ): Client
158126 {
159- $ name = $ this ->option ('name ' ) ?: $ this ->ask (
160- 'What should we name the client? ' ,
161- config ('app.name ' )
162- );
163-
164127 $ redirect = $ this ->option ('redirect_uri ' ) ?: $ this ->ask (
165128 'Where should we redirect the request after authorization? ' ,
166129 url ('/auth/callback ' )
167130 );
168131
169- $ client = $ clients ->createAuthorizationCodeGrantClient (
170- $ name , explode (', ' , $ redirect ), ! $ this ->option ('public ' ),
171- );
172-
173- $ this ->components ->info ('New client created successfully. ' );
174-
175- $ this ->outputClientDetails ($ client );
176- }
177-
178- /**
179- * Output the client's ID and secret key.
180- *
181- * @param \Laravel\Passport\Client $client
182- * @return void
183- */
184- protected function outputClientDetails (Client $ client )
185- {
186- $ this ->components ->warn ('Here is your new client secret. This is the only time it will be shown so don \'t lose it! ' );
132+ $ confidential = $ this ->hasOption ('public ' )
133+ ? ! $ this ->option ('public ' )
134+ : $ this ->confirm ('Would you like to make this client confidential? ' , true );
187135
188- $ this ->components ->twoColumnDetail ('Client ID ' , $ client ->getKey ());
189- $ this ->components ->twoColumnDetail ('Client Secret ' , $ client ->plainSecret );
136+ return $ clients ->createAuthorizationCodeGrantClient (
137+ $ this ->option ('name ' ), explode (', ' , $ redirect ), $ confidential ,
138+ );
190139 }
191140}
0 commit comments