88
99defined ('_JEXEC ' ) or die ( 'Restricted access ' );
1010
11+ require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php ' ;
12+
13+ use Firebase \JWT \JWT ;
14+
1115jimport ('joomla.plugin.plugin ' );
1216jimport ('joomla.html.html ' );
1317jimport ('joomla.application.component.controller ' );
@@ -39,7 +43,7 @@ public function keygen()
3943 //init variable
4044 $ obj = new stdclass ;
4145 $ umodel = new JUser ;
42- $ user = $ umodel ->getInstance ();
46+ $ user = $ umodel ->getInstance ();
4347
4448 $ app = JFactory::getApplication ();
4549 $ username = $ app ->input ->get ('username ' , 0 , 'STRING ' );
@@ -50,7 +54,7 @@ public function keygen()
5054 if ($ id == null )
5155 {
5256 $ model = FD ::model ('Users ' );
53- $ id = $ model ->getUserId ('email ' , $ username );
57+ $ id = $ model ->getUserId ('email ' , $ username );
5458 }
5559
5660 $ kmodel = new ApiModelKey ;
@@ -83,31 +87,52 @@ public function keygen()
8387
8488 $ result = $ kmodel ->save ($ data );
8589 $ key = $ result ->hash ;
86-
90+
8791 //add new key in easysocial table
8892 $ easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php ' ;
8993 if (JFile::exists ($ easyblog ) && JComponentHelper::isEnabled ('com_easysocial ' , true ))
9094 {
9195 $ this ->updateEauth ( $ user , $ key );
9296 }
9397 }
94-
98+
9599 if ( !empty ($ key ) )
96100 {
97101 $ obj ->auth = $ key ;
98102 $ obj ->code = '200 ' ;
99103 //$obj->id = $user->id;
100104 $ obj ->id = $ id ;
105+
106+ // Generate claim for jwt
107+ $ data = [
108+ "id " => trim ($ id ),
109+ /*"iat" => '',
110+ "exp" => '',
111+ "aud" => '',
112+ "sub" => ''"*/
113+ ];
114+
115+ // Using HS256 algo to generate JWT
116+ $ jwt = JWT ::encode ($ data , trim ($ key ), 'HS256 ' );
117+
118+ if (isset ($ jwt ) && $ jwt != '' )
119+ {
120+ $ obj ->jwt = $ jwt ;
121+ }
122+ else
123+ {
124+ $ obj ->jwt = false ;
125+ }
101126 }
102127 else
103128 {
104129 $ obj ->code = 403 ;
105130 $ obj ->message = JText::_ ('PLG_API_USERS_BAD_REQUEST_MESSAGE ' );
106131 }
107132 return ( $ obj );
108-
133+
109134 }
110-
135+
111136 /*
112137 * function to update Easyblog auth keys
113138 */
0 commit comments