Skip to content

Commit b95a594

Browse files
Merge pull request #4 from JSON-ms/dev
3 seconds timeout when accessing Google API
2 parents d652768 + 34b3e3c commit b95a594

File tree

7 files changed

+39
-60
lines changed

7 files changed

+39
-60
lines changed

.github/workflows/deploy-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
mkdir .compress
3131
mv archive.zip .compress/.
3232
cd .compress
33-
echo "<?php ($$z=new ZipArchive)->open('archive.zip')===TRUE && $$z->extractTo('.') && $$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
33+
echo "<?php (\$z=new ZipArchive)->open('archive.zip')===TRUE && \$z->extractTo('.') && \$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
3434
echo "INTERFACE_EDITOR_URL=https://dev.json.ms" > .env
3535
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://dev.json.ms" >> .env
3636
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env

.github/workflows/deploy-prod.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy DEV to host
1+
name: Deploy PROD to host
22

33
on:
44
push:
@@ -30,13 +30,13 @@ jobs:
3030
mkdir .compress
3131
mv archive.zip .compress/.
3232
cd .compress
33-
echo "<?php ($$z=new ZipArchive)->open('archive.zip')===TRUE && $$z->extractTo('.') && $$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
34-
echo "INTERFACE_EDITOR_URL=https://dev.json.ms" > .env
35-
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://dev.json.ms" >> .env
33+
echo "<?php (\$z=new ZipArchive)->open('archive.zip')===TRUE && \$z->extractTo('.') && \$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
34+
echo "INTERFACE_EDITOR_URL=https://json.ms" > .env
35+
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://json.ms" >> .env
3636
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env
3737
echo "GOOGLE_OAUTH_CLIENT_ID=637442439591-qrrpb3v9d3n5m8b8gheorfa1fbi5o6qc.apps.googleusercontent.com" >> .env
3838
echo "GOOGLE_OAUTH_CLIENT_SECRET=${{ secrets.GOOGLE_OAUTH_CLIENT_SECRET }}" >> .env
39-
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.dev.json.ms/google/callback" >> .env
39+
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.json.ms/google/callback" >> .env
4040
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env
4141
echo "DATABASE_DBNAME=${{ secrets.DATABASE_PROD_DBNAME }}" >> .env
4242
echo "DATABASE_USERNAME=${{ secrets.DATABASE_USERNAME }}" >> .env

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.12",
2+
"version": "1.0.13",
33
"name": "jsonms/server",
44
"description": "The JSON.ms Request Handler Server is a robust backend solution designed to manage and process all incoming requests from the main JSON.ms website.",
55
"license": "BSD-3-Clause",

datatable.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ DROP TABLE IF EXISTS `permissions`;
7575
CREATE TABLE `permissions` (
7676
`id` int NOT NULL AUTO_INCREMENT,
7777
`structure_uuid` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
78-
`type` enum('admin','interface') COLLATE utf8mb4_unicode_ci NOT NULL,
78+
`type` enum('admin','structure') COLLATE utf8mb4_unicode_ci NOT NULL,
7979
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
8080
PRIMARY KEY (`id`)
8181
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

index.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 30);
4+
session_set_cookie_params(60 * 60 * 24 * 30);
35
session_start();
46

57
error_reporting(E_ALL);
@@ -26,14 +28,6 @@
2628
header("Access-Control-Allow-Credentials: true");
2729
header('Content-Type: application/json');
2830

29-
setcookie("PHPSESSID", session_id(), [
30-
'expires' => time() + 60 * 60 * 24 * 30,
31-
'path' => '/',
32-
'domain' => '.' . $_SERVER['HTTP_HOST'],
33-
'secure' => isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https',
34-
'samesite' => 'None'
35-
]);
36-
3731
// Respond with a 200 OK status for preflight requests
3832
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
3933
http_response_code(200);

src/controllers/BaseController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public function getCurrentUserId() {
9090
}
9191
return null;
9292
}
93+
if ($this->user == null) {
94+
return null;
95+
}
9396
return $this->user->id;
9497
}
9598

src/controllers/SessionController.php

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use JSONms\Controllers\RestfulController;
4+
use GuzzleHttp\Client as GuzzleClient;
45

56
class SessionController extends RestfulController {
67

@@ -28,6 +29,29 @@ private function getEndpoints($userId) {
2829
return [];
2930
}
3031

32+
private function getLoginUrl() {
33+
34+
$httpClient = new GuzzleClient([
35+
'timeout' => 3.0,
36+
'connect_timeout' => 3.0,
37+
]);
38+
39+
try {
40+
// Google Client Configuration
41+
$client = new Google_Client();
42+
$client->setHttpClient($httpClient);
43+
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
44+
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
45+
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
46+
$client->addScope('email');
47+
$client->addScope('profile');
48+
49+
return $client->createAuthUrl();
50+
} catch(\Exception $e) {
51+
throwError(500, $e->getMessage());
52+
}
53+
}
54+
3155
public function indexAction() {
3256

3357
$loggedIn = isset($_SESSION['access_token']) && $_SESSION['access_token'];
@@ -54,53 +78,11 @@ public function indexAction() {
5478
// User exists, fetch data
5579
$user = $stmt->fetch(PDO::FETCH_OBJ);
5680
} else {
57-
// Google Client Configuration
58-
$client = new Google_Client();
59-
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
60-
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
61-
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
62-
$client->addScope('email');
63-
$client->addScope('profile');
64-
$loginUrl = $client->createAuthUrl();
81+
$loginUrl = $this->getLoginUrl();
6582
}
6683
}
6784
else {
68-
try {
69-
// Google Client Configuration
70-
$client = new Google_Client();
71-
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
72-
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
73-
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
74-
$client->addScope('email');
75-
$client->addScope('profile');
76-
$loginUrl = $client->createAuthUrl();
77-
} catch(\Exception $e) {
78-
throwError(500, $e->getMessage());
79-
}
80-
81-
try {
82-
$oauth2 = new Google_Service_Oauth2($client);
83-
$oauth2->userinfo->get();
84-
$loggedIn = true;
85-
} catch(\Exception $e) {
86-
$this->responseJson([
87-
'error' => $e->getMessage(),
88-
'loggedIn' => false,
89-
'user' => $user,
90-
'googleOAuthSignInUrl' => $loginUrl,
91-
'structures' => $structures,
92-
'endpoints' => $endpoints,
93-
]);
94-
}
95-
96-
// Check if user already exists
97-
$stmt = $this->query('get-user-by-google-id', [
98-
'id' => $this->getCurrentUserId(),
99-
]);
100-
101-
if ($stmt->rowCount() > 0) {
102-
$user = $stmt->fetch(PDO::FETCH_OBJ);
103-
}
85+
$loginUrl = $this->getLoginUrl();
10486
}
10587

10688
if ($loggedIn && isset($user)) {

0 commit comments

Comments
 (0)