88use splitbrain \phpcli \Options ;
99use splitbrain \phpcli \TableFormatter ;
1010
11- class Module2 extends CLI
11+ class Module3 extends CLI
1212{
1313 /**
1414 * @var GuzzleHttp\Client
1515 */
1616 private $ client ;
17+ private $ token ;
1718
1819 private $ user ;
1920
@@ -33,6 +34,7 @@ protected function setup(Options $options)
3334
3435 $ options ->registerCommand ('register ' , 'Create a user account. ' );
3536 $ options ->registerCommand ('changePassword ' , 'Change your user password ' );
37+ $ options ->registerCommand ('login ' , 'Create a persistent authentication session ' );
3638
3739 $ options ->registerArgument ('note ' , 'Actual note to store. ' , true , 'create ' );
3840 $ options ->registerArgument ('noteId ' , 'Identify the note to retrieve. ' , true , 'get ' );
@@ -47,12 +49,13 @@ protected function setup(Options $options)
4749 $ options ->registerOption ('new-password ' , 'New login password ' , 'p ' , true , 'changePassword ' );
4850 $ options ->registerOption ('repeat-password ' , 'New login password (again) ' , 'r ' , true , 'changePassword ' );
4951
50- foreach (['create ' , 'get ' , 'delete ' , 'all ' ] as $ command ) {
52+ foreach (['create ' , 'get ' , 'delete ' , 'all ' , ' login ' ] as $ command ) {
5153 $ options ->registerOption ('email ' , 'Email address ' , 'e ' , true , $ command );
5254 $ options ->registerOption ('password ' , 'Login password ' , 'p ' , true , $ command );
5355 }
5456
5557 $ this ->client = new GuzzleHttp \Client (['base_uri ' => 'http://localhost:8888 ' ]);
58+ $ this ->token = file_get_contents ('.session ' );
5659 }
5760
5861 protected function createNote ($ contents )
@@ -66,9 +69,11 @@ protected function createNote($contents)
6669 'note ' => $ contents
6770 ];
6871
69- $ response = $ this ->client ->request ('POST ' , 'notes ' , [
72+ $ response = $ this ->client ->request (
73+ 'POST ' ,
74+ 'notes ' , [
7075 'body ' => json_encode ($ jsonBody ),
71- 'auth ' => [$ this ->user , $ this ->pass ]
76+ 'auth ' => [$ this ->user , $ this ->pass ] // @TODO Replace basic auth with Bearer auth
7277 ]);
7378
7479 if ($ response ->getStatusCode () === 200 ) {
@@ -116,7 +121,11 @@ private function printNote(array $note, TableFormatter $tf)
116121
117122 protected function getNote ($ noteId )
118123 {
119- $ response = $ this ->client ->request ('GET ' , sprintf ('notes/%s ' , $ noteId ), ['auth ' => [$ this ->user , $ this ->pass ]]);
124+ $ response = $ this ->client ->request (
125+ 'GET ' ,
126+ sprintf ('notes/%s ' , $ noteId ),
127+ ['auth ' => [$ this ->user , $ this ->pass ]] // @TODO Replace basic auth with Bearer auth
128+ );
120129
121130 if ($ response ->getStatusCode () === 200 ) {
122131 $ return = json_decode ($ response ->getBody (), true );
@@ -134,7 +143,11 @@ protected function getNote($noteId)
134143 protected function deleteNote ($ noteId )
135144 {
136145 try {
137- $ this ->client ->request ('DELETE ' , sprintf ('notes/%s ' , $ noteId ), ['auth ' => [$ this ->user , $ this ->pass ]]);
146+ $ this ->client ->request (
147+ 'DELETE ' ,
148+ sprintf ('notes/%s ' , $ noteId ),
149+ ['auth ' => [$ this ->user , $ this ->pass ]] // @TODO Replace basic auth with Bearer auth
150+ );
138151 $ this ->info (sprintf ('Note ID %s has been deleted. ' , $ noteId ));
139152 } catch (\GuzzleHttp \Exception \BadResponseException $ e ) {
140153 $ this ->error (sprintf ('Unable to delete note %s. It might not exist! ' , $ noteId ));
@@ -143,7 +156,11 @@ protected function deleteNote($noteId)
143156
144157 protected function getAllNotes ()
145158 {
146- $ response = $ this ->client ->request ('GET ' , 'notes ' , ['auth ' => [$ this ->user , $ this ->pass ]]);
159+ $ response = $ this ->client ->request (
160+ 'GET ' ,
161+ 'notes ' ,
162+ ['auth ' => [$ this ->user , $ this ->pass ]] // @TODO Replace basic auth with Bearer auth
163+ );
147164
148165 if ($ response ->getStatusCode () === 200 ) {
149166 $ notes = json_decode ($ response ->getBody (), true );
@@ -247,6 +264,35 @@ protected function changePassword(Options $options)
247264 }
248265 }
249266
267+ protected function login (Options $ options )
268+ {
269+ try {
270+ $ response = $ this ->client ->request ('GET ' , 'login ' , ['auth ' => [$ this ->user , $ this ->pass ]]);
271+
272+ if ($ response ->getStatusCode () === 200 ) {
273+ $ auth = json_decode ($ response ->getBody (), true );
274+
275+ if (empty ($ auth )) {
276+ $ this ->error ('Could not establish a session! ' );
277+ } else {
278+ $ token = $ auth ['token ' ];
279+
280+ try {
281+ $ fp = fopen ('.session ' , 'w ' );
282+ fwrite ($ fp , $ token );
283+ fclose ($ fp );
284+
285+ $ this ->info ('Established a persistent session. Commence note taking! ' );
286+ } catch (\Exception $ e ) {
287+ $ this ->error ('Could not establish a session! ' );
288+ }
289+ }
290+ }
291+ } catch (\GuzzleHttp \Exception \BadResponseException $ e ) {
292+ $ this ->error ('Could not establish a session! ' );
293+ }
294+ }
295+
250296 protected function main (Options $ options )
251297 {
252298 $ args = $ options ->getArgs ();
@@ -272,6 +318,9 @@ protected function main(Options $options)
272318 case 'changePassword ' :
273319 $ this ->changePassword ($ options );
274320 break ;
321+ case 'login ' :
322+ $ this ->login ($ options );
323+ break ;
275324 default :
276325 $ this ->error ('No known command was called, we show the default help instead: ' );
277326 echo $ options ->help ();
@@ -280,5 +329,5 @@ protected function main(Options $options)
280329 }
281330}
282331
283- $ cli = new Module2 ();
332+ $ cli = new Module3 ();
284333$ cli ->run ();
0 commit comments