create-prisma-php-app 1.11.16 → 1.11.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,13 +6,18 @@ use Firebase\JWT\JWT;
6
6
  use Firebase\JWT\Key;
7
7
  use DateInterval;
8
8
  use DateTime;
9
+ use Lib\Validator;
10
+ use GuzzleHttp\Client;
11
+ use GuzzleHttp\Exception\RequestException;
12
+ use Lib\Prisma\Classes\Prisma;
9
13
 
10
14
  class Auth
11
15
  {
12
16
  public const PAYLOAD_NAME = 'role';
13
17
  public const ROLE_NAME = '';
14
18
  public const PAYLOAD = 'payload';
15
- public const COOKIE_NAME = 'auth_token';
19
+ public const COOKIE_NAME = 'pphp_aut_token';
20
+ private const PPHPAUTH = 'pphpauth';
16
21
 
17
22
  private $secretKey;
18
23
  private $defaultTokenValidity = '1h'; // Default to 1 hour
@@ -182,4 +187,257 @@ class Auth
182
187
 
183
188
  return null;
184
189
  }
190
+
191
+ private function exchangeCode($data, $apiUrl)
192
+ {
193
+ try {
194
+ $client = new Client();
195
+ $response = $client->post($apiUrl, [
196
+ 'headers' => [
197
+ 'Accept' => 'application/json',
198
+ ],
199
+ 'form_params' => $data,
200
+ ]);
201
+
202
+ if ($response->getStatusCode() === 200) {
203
+ return json_decode($response->getBody()->getContents());
204
+ }
205
+
206
+ return false;
207
+ } catch (RequestException) {
208
+ return false;
209
+ }
210
+ }
211
+
212
+ public function authProviders(GithubProvider | null $githubProvider = null, GoogleProvider | null $googleProvider = null)
213
+ {
214
+ global $isGet, $dynamicRouteParams;
215
+
216
+ if ($isGet && in_array('signin', $dynamicRouteParams[self::PPHPAUTH]) && in_array('github', $dynamicRouteParams[self::PPHPAUTH]) && $githubProvider) {
217
+ $githubAuthUrl = "https://github.com/login/oauth/authorize?scope=user:email%20read:user&client_id=$githubProvider->clientId";
218
+ redirect($githubAuthUrl);
219
+ } elseif ($isGet && in_array('signin', $dynamicRouteParams[self::PPHPAUTH]) && in_array('google', $dynamicRouteParams[self::PPHPAUTH]) && $googleProvider) {
220
+ $googleAuthUrl = "https://accounts.google.com/o/oauth2/v2/auth?"
221
+ . "scope=" . urlencode('email profile') . "&"
222
+ . "response_type=code&"
223
+ . "client_id=" . urlencode($googleProvider->clientId) . "&"
224
+ . "redirect_uri=" . urlencode($googleProvider->redirectUri);
225
+
226
+ redirect($googleAuthUrl);
227
+ }
228
+
229
+ $authCode = Validator::validateString($_GET['code'] ?? '');
230
+
231
+ if (
232
+ $isGet && in_array('callback', $dynamicRouteParams[self::PPHPAUTH]) &&
233
+ in_array('github', $dynamicRouteParams[self::PPHPAUTH]) && isset($authCode)
234
+ ) {
235
+ return $this->githubProvider($githubProvider, $authCode);
236
+ } elseif (
237
+ $isGet && in_array('callback', $dynamicRouteParams[self::PPHPAUTH]) &&
238
+ in_array('google', $dynamicRouteParams[self::PPHPAUTH]) && isset($authCode)
239
+ ) {
240
+ return $this->googleProvider($googleProvider, $authCode);
241
+ } else {
242
+ exit("Error occurred. Please try again.");
243
+ }
244
+ }
245
+
246
+ private function githubProvider(GithubProvider $githubProvider, string $authCode)
247
+ {
248
+ $gitToken = [
249
+ 'client_id' => $githubProvider->clientId,
250
+ 'client_secret' => $githubProvider->clientSecret,
251
+ 'code' => $authCode,
252
+ ];
253
+
254
+ $apiUrl = 'https://github.com/login/oauth/access_token';
255
+ $tokenData = (object)$this->exchangeCode($gitToken, $apiUrl);
256
+
257
+ if (!$tokenData) {
258
+ exit("Error occurred. Please try again.");
259
+ }
260
+
261
+ if (isset($tokenData->error)) {
262
+ exit("Error occurred. Please try again.");
263
+ }
264
+
265
+ if (isset($tokenData->access_token)) {
266
+ $client = new Client();
267
+ $emailResponse = $client->get('https://api.github.com/user/emails', [
268
+ 'headers' => [
269
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
270
+ 'Accept' => 'application/json',
271
+ ],
272
+ ]);
273
+
274
+ $emails = json_decode($emailResponse->getBody()->getContents(), true);
275
+
276
+ $primaryEmail = array_reduce($emails, function ($carry, $item) {
277
+ return ($item['primary'] && $item['verified']) ? $item['email'] : $carry;
278
+ }, null);
279
+
280
+ $response = $client->get('https://api.github.com/user', [
281
+ 'headers' => [
282
+ 'Accept' => 'application/json',
283
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
284
+ ],
285
+ ]);
286
+
287
+ if ($response->getStatusCode() == 200) {
288
+ $responseInfo = json_decode($response->getBody()->getContents());
289
+
290
+ $accountData = [
291
+ 'provider' => 'github',
292
+ 'type' => 'oauth',
293
+ 'providerAccountId' => "$responseInfo->id",
294
+ 'access_token' => $tokenData->access_token,
295
+ 'expires_at' => $tokenData->expires_at ?? null,
296
+ 'token_type' => $tokenData->token_type,
297
+ 'scope' => $tokenData->scope,
298
+ ];
299
+
300
+ $prisma = new Prisma();
301
+ $foundUser = $prisma->user->findUnique([
302
+ 'where' => [
303
+ 'email' => $primaryEmail,
304
+ ],
305
+ ]);
306
+
307
+ if (!$foundUser) {
308
+ $userData = [
309
+ 'name' => $responseInfo->login,
310
+ 'email' => $primaryEmail,
311
+ 'image' => $responseInfo->avatar_url,
312
+ 'emailVerified' => $primaryEmail ? date("Y-m-d H:i:s") : null,
313
+ 'Account' => [
314
+ 'create' => $accountData,
315
+ ]
316
+ ];
317
+
318
+ $createUser = $prisma->user->create([
319
+ 'data' => $userData,
320
+ ]);
321
+
322
+ if (!$createUser) {
323
+ exit("Error occurred. Please try again.");
324
+ }
325
+ }
326
+
327
+ $userToAuthenticate = [
328
+ 'name' => $responseInfo->login,
329
+ 'email' => $primaryEmail,
330
+ 'image' => $responseInfo->avatar_url,
331
+ 'Account' => (object)$accountData
332
+ ];
333
+ $userToAuthenticate = (object)$userToAuthenticate;
334
+
335
+ $this->authenticate($userToAuthenticate, $githubProvider->maxAge);
336
+ }
337
+ }
338
+ }
339
+
340
+ private function googleProvider(GoogleProvider $googleProvider, string $authCode)
341
+ {
342
+ $googleToken = [
343
+ 'client_id' => $googleProvider->clientId,
344
+ 'client_secret' => $googleProvider->clientSecret,
345
+ 'code' => $authCode,
346
+ 'grant_type' => 'authorization_code',
347
+ 'redirect_uri' => $googleProvider->redirectUri
348
+ ];
349
+
350
+ $apiUrl = 'https://oauth2.googleapis.com/token';
351
+ $tokenData = (object)$this->exchangeCode($googleToken, $apiUrl);
352
+
353
+ if (!$tokenData) {
354
+ exit("Error occurred. Please try again.");
355
+ }
356
+
357
+ if (isset($tokenData->error)) {
358
+ exit("Error occurred. Please try again.");
359
+ }
360
+
361
+ if (isset($tokenData->access_token)) {
362
+ $client = new Client();
363
+ $response = $client->get('https://www.googleapis.com/oauth2/v1/userinfo', [
364
+ 'headers' => [
365
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
366
+ 'Accept' => 'application/json',
367
+ ],
368
+ ]);
369
+
370
+ if ($response->getStatusCode() == 200) {
371
+ $responseInfo = json_decode($response->getBody()->getContents());
372
+
373
+ $accountData = [
374
+ 'provider' => 'google',
375
+ 'type' => 'oauth',
376
+ 'providerAccountId' => "$responseInfo->id",
377
+ 'access_token' => $tokenData->access_token,
378
+ 'expires_at' => $tokenData->expires_at ?? null,
379
+ 'token_type' => $tokenData->token_type,
380
+ 'scope' => $tokenData->scope,
381
+ ];
382
+
383
+ $prisma = new Prisma();
384
+ $foundUser = $prisma->user->findUnique([
385
+ 'where' => [
386
+ 'email' => $responseInfo->email,
387
+ ],
388
+ ]);
389
+
390
+ if (!$foundUser) {
391
+ $userData = [
392
+ 'name' => $responseInfo->name,
393
+ 'email' => $responseInfo->email,
394
+ 'image' => $responseInfo->picture,
395
+ 'emailVerified' => $responseInfo->email ? date("Y-m-d H:i:s") : null,
396
+ 'Account' => [
397
+ 'create' => $accountData,
398
+ ]
399
+ ];
400
+
401
+ $createUser = $prisma->user->create([
402
+ 'data' => $userData,
403
+ ]);
404
+
405
+ if (!$createUser) {
406
+ exit("Error occurred. Please try again.");
407
+ }
408
+ }
409
+
410
+ $userToAuthenticate = [
411
+ 'name' => $responseInfo->name,
412
+ 'email' => $responseInfo->email,
413
+ 'image' => $responseInfo->picture,
414
+ 'Account' => (object)$accountData
415
+ ];
416
+ $userToAuthenticate = (object)$userToAuthenticate;
417
+
418
+ $this->authenticate($userToAuthenticate, $googleProvider->maxAge);
419
+ }
420
+ }
421
+ }
422
+ }
423
+
424
+ class GoogleProvider
425
+ {
426
+ public function __construct(
427
+ public string $clientId,
428
+ public string $clientSecret,
429
+ public string $redirectUri,
430
+ public string $maxAge = '30d'
431
+ ) {
432
+ }
433
+ }
434
+
435
+ class GithubProvider
436
+ {
437
+ public function __construct(
438
+ public string $clientId,
439
+ public string $clientSecret,
440
+ public string $maxAge = '30d'
441
+ ) {
442
+ }
185
443
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "1.11.16",
3
+ "version": "1.11.17",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",