@@ -14,8 +14,8 @@ class Authentication
1414 * This function is for user-friendly interface purpose only.
1515 * You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API.
1616 *
17- * @param string $email the user email
18- * @param string $password the user password
17+ * @param string $email the user email
18+ * @param string $password the user password
1919 *
2020 * @return null|array the API keys
2121 *
@@ -130,6 +130,36 @@ public static function getPublishableKeys(Payplug $payplug = null)
130130 }
131131 }
132132
133+ /**
134+ * Generate a token JWT.
135+ *
136+ * @param Payplug $payplug the client configuration
137+ *
138+ * @return array the token JWT
139+ *
140+ * @throws Exception
141+ */
142+ public static function generateJWT ($ client_id = '' , Payplug $ payplug = null )
143+ {
144+ if ($ client_id == '' ) {
145+ return array ();
146+ }
147+
148+ if ($ payplug === null ) {
149+ $ payplug = Payplug::getDefaultConfiguration ();
150+ }
151+
152+ $ httpClient = new Core \HttpClient ($ payplug );
153+ try {
154+ return $ httpClient ->post (
155+ Core \APIRoutes::getRoute (Core \APIRoutes::$ HYDRA_RESOURCE ),
156+ array ('client_id ' => $ client_id , 'grant_type ' => 'client_credentials ' )
157+ );
158+ } catch (Exception $ e ) {
159+ return array ();
160+ }
161+ }
162+
133163 /**
134164 * Validates the Payplug token
135165 *
@@ -144,4 +174,98 @@ private static function validateToken(Payplug $payplug)
144174 throw new ConfigurationException ('The Payplug configuration requires a valid token. ' );
145175 }
146176 }
177+
178+ /**
179+ * Retrieve client datas from the user manager resource.
180+ *
181+ * @param Payplug $payplug the client configuration
182+ *
183+ * @return array the client id and client_secret_mask
184+ *
185+ * @throws Exception
186+ */
187+ public static function getClientData ($ session = null , Payplug $ payplug = null )
188+ {
189+ if ($ payplug === null ) {
190+ $ payplug = Payplug::getDefaultConfiguration ();
191+ }
192+ $ kratosSession = self ::setKratosSession ($ session );
193+
194+ $ httpClient = new Core \HttpClient ($ payplug );
195+ $ response = $ httpClient ->get (Core \APIRoutes::$ USER_MANAGER_RESOURCE , null , $ kratosSession );
196+ $ result = array ();
197+ foreach ($ response ['httpResponse ' ] as $ client ) {
198+ $ result [] = array (
199+ 'client_id ' => $ client ['client_id ' ],
200+ 'client_secret_mask ' => $ client ['client_secret_mask ' ],
201+ 'client_name ' => $ client ['client_name ' ],
202+ 'client_type ' => $ client ['client_type ' ],
203+ 'mode ' => $ client ['mode ' ],
204+
205+ );
206+ }
207+
208+ return $ result ;
209+ }
210+
211+ /**
212+ * Create a client ID and secret for a given mode
213+ *
214+ * @param $company_id
215+ * @param $client_name
216+ * @param $mode
217+ * @param $session
218+ * @param Payplug|null $payplug
219+ * @return array
220+ * @throws ConfigurationException
221+ * @throws Exception\ConfigurationNotSetException
222+ * @throws Exception\ConnectionException
223+ * @throws Exception\HttpException
224+ * @throws Exception\UnexpectedAPIResponseException
225+ */
226+ public static function createClientIdAndSecret ($ company_id ='' , $ client_name ='' , $ mode ='' , $ session = null , Payplug $ payplug = null )
227+ {
228+
229+ if ($ payplug === null ) {
230+ $ payplug = Payplug::getDefaultConfiguration ();
231+ }
232+ $ kratosSession = self ::setKratosSession ($ session );
233+
234+ $ httpClient = new Core \HttpClient ($ payplug );
235+ $ result = array ();
236+
237+ $ response = $ httpClient ->post (Core \APIRoutes::$ USER_MANAGER_RESOURCE , array (
238+ 'company_id ' => $ company_id ,
239+ 'client_name ' => $ client_name ,
240+ 'client_type ' =>'oauth2 ' ,
241+ 'mode ' => $ mode ,
242+ ), $ kratosSession );
243+ foreach ($ response ['httpResponse ' ] as $ client ) {
244+ $ result [] = array (
245+ 'client_id ' => $ client ['client_id ' ],
246+ 'client_secret ' => $ client ['client_secret ' ],
247+ );
248+ }
249+
250+ return $ result ;
251+ }
252+
253+
254+
255+ /**
256+ * Set the Kratos session cookie.
257+ *
258+ * @param string $session The session value to be set in the cookie.
259+ *
260+ * @return string The formatted Kratos session cookie string.
261+ * @throws ConfigurationException
262+ */
263+ public static function setKratosSession ($ session )
264+ {
265+ if (empty ($ session )) {
266+ throw new ConfigurationException ('The session value must be set. ' );
267+ }
268+ return 'ory_kratos_session= ' . $ session ;
269+ }
270+
147271}
0 commit comments