create-prisma-php-app 1.11.16 → 1.11.18

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,17 @@ 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;
9
12
 
10
13
  class Auth
11
14
  {
12
15
  public const PAYLOAD_NAME = 'role';
13
16
  public const ROLE_NAME = '';
14
17
  public const PAYLOAD = 'payload';
15
- public const COOKIE_NAME = 'auth_token';
18
+ public const COOKIE_NAME = 'pphp_aut_token';
19
+ private const PPHPAUTH = 'pphpauth';
16
20
 
17
21
  private $secretKey;
18
22
  private $defaultTokenValidity = '1h'; // Default to 1 hour
@@ -182,4 +186,212 @@ class Auth
182
186
 
183
187
  return null;
184
188
  }
189
+
190
+ private function exchangeCode($data, $apiUrl)
191
+ {
192
+ try {
193
+ $client = new Client();
194
+ $response = $client->post($apiUrl, [
195
+ 'headers' => [
196
+ 'Accept' => 'application/json',
197
+ ],
198
+ 'form_params' => $data,
199
+ ]);
200
+
201
+ if ($response->getStatusCode() === 200) {
202
+ return json_decode($response->getBody()->getContents());
203
+ }
204
+
205
+ return false;
206
+ } catch (RequestException) {
207
+ return false;
208
+ }
209
+ }
210
+
211
+ private function saveAuthInfo($responseInfo, $accountData)
212
+ {
213
+ // Save user data to the database
214
+ }
215
+
216
+ public function authProviders(GithubProvider | null $githubProvider = null, GoogleProvider | null $googleProvider = null)
217
+ {
218
+ global $isGet, $dynamicRouteParams;
219
+
220
+ if ($isGet && in_array('signin', $dynamicRouteParams[self::PPHPAUTH]) && in_array('github', $dynamicRouteParams[self::PPHPAUTH]) && $githubProvider) {
221
+ $githubAuthUrl = "https://github.com/login/oauth/authorize?scope=user:email%20read:user&client_id=$githubProvider->clientId";
222
+ redirect($githubAuthUrl);
223
+ } elseif ($isGet && in_array('signin', $dynamicRouteParams[self::PPHPAUTH]) && in_array('google', $dynamicRouteParams[self::PPHPAUTH]) && $googleProvider) {
224
+ $googleAuthUrl = "https://accounts.google.com/o/oauth2/v2/auth?"
225
+ . "scope=" . urlencode('email profile') . "&"
226
+ . "response_type=code&"
227
+ . "client_id=" . urlencode($googleProvider->clientId) . "&"
228
+ . "redirect_uri=" . urlencode($googleProvider->redirectUri);
229
+
230
+ redirect($googleAuthUrl);
231
+ }
232
+
233
+ $authCode = Validator::validateString($_GET['code'] ?? '');
234
+
235
+ if (
236
+ $isGet && in_array('callback', $dynamicRouteParams[self::PPHPAUTH]) &&
237
+ in_array('github', $dynamicRouteParams[self::PPHPAUTH]) && isset($authCode)
238
+ ) {
239
+ return $this->githubProvider($githubProvider, $authCode);
240
+ } elseif (
241
+ $isGet && in_array('callback', $dynamicRouteParams[self::PPHPAUTH]) &&
242
+ in_array('google', $dynamicRouteParams[self::PPHPAUTH]) && isset($authCode)
243
+ ) {
244
+ return $this->googleProvider($googleProvider, $authCode);
245
+ } else {
246
+ exit("Error occurred. Please try again.");
247
+ }
248
+ }
249
+
250
+ private function githubProvider(GithubProvider $githubProvider, string $authCode)
251
+ {
252
+ $gitToken = [
253
+ 'client_id' => $githubProvider->clientId,
254
+ 'client_secret' => $githubProvider->clientSecret,
255
+ 'code' => $authCode,
256
+ ];
257
+
258
+ $apiUrl = 'https://github.com/login/oauth/access_token';
259
+ $tokenData = (object)$this->exchangeCode($gitToken, $apiUrl);
260
+
261
+ if (!$tokenData) {
262
+ exit("Error occurred. Please try again.");
263
+ }
264
+
265
+ if (isset($tokenData->error)) {
266
+ exit("Error occurred. Please try again.");
267
+ }
268
+
269
+ if (isset($tokenData->access_token)) {
270
+ $client = new Client();
271
+ $emailResponse = $client->get('https://api.github.com/user/emails', [
272
+ 'headers' => [
273
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
274
+ 'Accept' => 'application/json',
275
+ ],
276
+ ]);
277
+
278
+ $emails = json_decode($emailResponse->getBody()->getContents(), true);
279
+
280
+ $primaryEmail = array_reduce($emails, function ($carry, $item) {
281
+ return ($item['primary'] && $item['verified']) ? $item['email'] : $carry;
282
+ }, null);
283
+
284
+ $response = $client->get('https://api.github.com/user', [
285
+ 'headers' => [
286
+ 'Accept' => 'application/json',
287
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
288
+ ],
289
+ ]);
290
+
291
+ if ($response->getStatusCode() == 200) {
292
+ $responseInfo = json_decode($response->getBody()->getContents());
293
+
294
+ $accountData = [
295
+ 'provider' => 'github',
296
+ 'type' => 'oauth',
297
+ 'providerAccountId' => "$responseInfo->id",
298
+ 'access_token' => $tokenData->access_token,
299
+ 'expires_at' => $tokenData->expires_at ?? null,
300
+ 'token_type' => $tokenData->token_type,
301
+ 'scope' => $tokenData->scope,
302
+ ];
303
+
304
+ $this->saveAuthInfo($responseInfo, $accountData);
305
+
306
+ $userToAuthenticate = [
307
+ 'name' => $responseInfo->login,
308
+ 'email' => $primaryEmail,
309
+ 'image' => $responseInfo->avatar_url,
310
+ 'Account' => (object)$accountData
311
+ ];
312
+ $userToAuthenticate = (object)$userToAuthenticate;
313
+
314
+ $this->authenticate($userToAuthenticate, $githubProvider->maxAge);
315
+ }
316
+ }
317
+ }
318
+
319
+ private function googleProvider(GoogleProvider $googleProvider, string $authCode)
320
+ {
321
+ $googleToken = [
322
+ 'client_id' => $googleProvider->clientId,
323
+ 'client_secret' => $googleProvider->clientSecret,
324
+ 'code' => $authCode,
325
+ 'grant_type' => 'authorization_code',
326
+ 'redirect_uri' => $googleProvider->redirectUri
327
+ ];
328
+
329
+ $apiUrl = 'https://oauth2.googleapis.com/token';
330
+ $tokenData = (object)$this->exchangeCode($googleToken, $apiUrl);
331
+
332
+ if (!$tokenData) {
333
+ exit("Error occurred. Please try again.");
334
+ }
335
+
336
+ if (isset($tokenData->error)) {
337
+ exit("Error occurred. Please try again.");
338
+ }
339
+
340
+ if (isset($tokenData->access_token)) {
341
+ $client = new Client();
342
+ $response = $client->get('https://www.googleapis.com/oauth2/v1/userinfo', [
343
+ 'headers' => [
344
+ 'Authorization' => 'Bearer ' . $tokenData->access_token,
345
+ 'Accept' => 'application/json',
346
+ ],
347
+ ]);
348
+
349
+ if ($response->getStatusCode() == 200) {
350
+ $responseInfo = json_decode($response->getBody()->getContents());
351
+
352
+ $accountData = [
353
+ 'provider' => 'google',
354
+ 'type' => 'oauth',
355
+ 'providerAccountId' => "$responseInfo->id",
356
+ 'access_token' => $tokenData->access_token,
357
+ 'expires_at' => $tokenData->expires_at ?? null,
358
+ 'token_type' => $tokenData->token_type,
359
+ 'scope' => $tokenData->scope,
360
+ ];
361
+
362
+ $this->saveAuthInfo($responseInfo, $accountData);
363
+
364
+ $userToAuthenticate = [
365
+ 'name' => $responseInfo->name,
366
+ 'email' => $responseInfo->email,
367
+ 'image' => $responseInfo->picture,
368
+ 'Account' => (object)$accountData
369
+ ];
370
+ $userToAuthenticate = (object)$userToAuthenticate;
371
+
372
+ $this->authenticate($userToAuthenticate, $googleProvider->maxAge);
373
+ }
374
+ }
375
+ }
376
+ }
377
+
378
+ class GoogleProvider
379
+ {
380
+ public function __construct(
381
+ public string $clientId,
382
+ public string $clientSecret,
383
+ public string $redirectUri,
384
+ public string $maxAge = '30d'
385
+ ) {
386
+ }
387
+ }
388
+
389
+ class GithubProvider
390
+ {
391
+ public function __construct(
392
+ public string $clientId,
393
+ public string $clientSecret,
394
+ public string $maxAge = '30d'
395
+ ) {
396
+ }
185
397
  }
@@ -29,7 +29,7 @@
29
29
  <p class="mx-auto max-w-[700px] text-gray-500 md:text-xl dark:text-gray-400">
30
30
  The Next Generation ORM for PHP
31
31
  </p>
32
- <a class="inline-flex h-10 items-center justify-center rounded-md bg-gray-900 px-8 text-sm font-medium text-gray-50 shadow transition-colors hover:bg-gray-900/90 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950 disabled:pointer-events-none disabled:opacity-50 dark:bg-gray-50 dark:text-gray-900 dark:hover:bg-gray-50/90 dark:focus-visible:ring-gray-300" href="https://prismaphp.tsnc.tech/docs?doc=get-started">
32
+ <a class="inline-flex h-10 items-center justify-center rounded-md bg-gray-900 px-8 text-sm font-medium text-gray-50 shadow transition-colors hover:bg-gray-900/90 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950 disabled:pointer-events-none disabled:opacity-50 dark:bg-gray-50 dark:text-gray-900 dark:hover:bg-gray-50/90 dark:focus-visible:ring-gray-300" href="https://prismaphp.tsnc.tech/docs?doc=get-started" target="_blank">
33
33
  Get Started
34
34
  </a>
35
35
  </div>
@@ -105,5 +105,7 @@
105
105
  /* Completeness */
106
106
  // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
107
107
  "skipLibCheck": true /* Skip type checking all .d.ts files. */
108
- }
108
+ },
109
+ "include": [],
110
+ "exclude": ["node_modules"]
109
111
  }
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.18",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",