Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
mkdir .compress
mv archive.zip .compress/.
cd .compress
echo "<?php ($$z=new ZipArchive)->open('archive.zip')===TRUE && $$z->extractTo('.') && $$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
echo "<?php (\$z=new ZipArchive)->open('archive.zip')===TRUE && \$z->extractTo('.') && \$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
echo "INTERFACE_EDITOR_URL=https://dev.json.ms" > .env
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://dev.json.ms" >> .env
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy DEV to host
name: Deploy PROD to host

on:
push:
Expand Down Expand Up @@ -30,13 +30,13 @@ jobs:
mkdir .compress
mv archive.zip .compress/.
cd .compress
echo "<?php ($$z=new ZipArchive)->open('archive.zip')===TRUE && $$z->extractTo('.') && $$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
echo "INTERFACE_EDITOR_URL=https://dev.json.ms" > .env
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://dev.json.ms" >> .env
echo "<?php (\$z=new ZipArchive)->open('archive.zip')===TRUE && \$z->extractTo('.') && \$z->close(); unlink('archive.zip'); unlink('update.php') ?>" > update.php
echo "INTERFACE_EDITOR_URL=https://json.ms" > .env
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://json.ms" >> .env
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env
echo "GOOGLE_OAUTH_CLIENT_ID=637442439591-qrrpb3v9d3n5m8b8gheorfa1fbi5o6qc.apps.googleusercontent.com" >> .env
echo "GOOGLE_OAUTH_CLIENT_SECRET=${{ secrets.GOOGLE_OAUTH_CLIENT_SECRET }}" >> .env
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.dev.json.ms/google/callback" >> .env
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.json.ms/google/callback" >> .env
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env
echo "DATABASE_DBNAME=${{ secrets.DATABASE_PROD_DBNAME }}" >> .env
echo "DATABASE_USERNAME=${{ secrets.DATABASE_USERNAME }}" >> .env
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.12",
"version": "1.0.13",
"name": "jsonms/server",
"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.",
"license": "BSD-3-Clause",
Expand Down
2 changes: 1 addition & 1 deletion datatable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ DROP TABLE IF EXISTS `permissions`;
CREATE TABLE `permissions` (
`id` int NOT NULL AUTO_INCREMENT,
`structure_uuid` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`type` enum('admin','interface') COLLATE utf8mb4_unicode_ci NOT NULL,
`type` enum('admin','structure') COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand Down
10 changes: 2 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 30);
session_set_cookie_params(60 * 60 * 24 * 30);
session_start();

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

setcookie("PHPSESSID", session_id(), [
'expires' => time() + 60 * 60 * 24 * 30,
'path' => '/',
'domain' => '.' . $_SERVER['HTTP_HOST'],
'secure' => isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https',
'samesite' => 'None'
]);

// Respond with a 200 OK status for preflight requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
http_response_code(200);
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function getCurrentUserId() {
}
return null;
}
if ($this->user == null) {
return null;
}
return $this->user->id;
}

Expand Down
70 changes: 26 additions & 44 deletions src/controllers/SessionController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use JSONms\Controllers\RestfulController;
use GuzzleHttp\Client as GuzzleClient;

class SessionController extends RestfulController {

Expand Down Expand Up @@ -28,6 +29,29 @@ private function getEndpoints($userId) {
return [];
}

private function getLoginUrl() {

$httpClient = new GuzzleClient([
'timeout' => 3.0,
'connect_timeout' => 3.0,
]);

try {
// Google Client Configuration
$client = new Google_Client();
$client->setHttpClient($httpClient);
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
$client->addScope('email');
$client->addScope('profile');

return $client->createAuthUrl();
} catch(\Exception $e) {
throwError(500, $e->getMessage());
}
}

public function indexAction() {

$loggedIn = isset($_SESSION['access_token']) && $_SESSION['access_token'];
Expand All @@ -54,53 +78,11 @@ public function indexAction() {
// User exists, fetch data
$user = $stmt->fetch(PDO::FETCH_OBJ);
} else {
// Google Client Configuration
$client = new Google_Client();
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
$client->addScope('email');
$client->addScope('profile');
$loginUrl = $client->createAuthUrl();
$loginUrl = $this->getLoginUrl();
}
}
else {
try {
// Google Client Configuration
$client = new Google_Client();
$client->setClientId($_ENV['GOOGLE_OAUTH_CLIENT_ID']);
$client->setClientSecret($_ENV['GOOGLE_OAUTH_CLIENT_SECRET']);
$client->setRedirectUri($_ENV['GOOGLE_OAUTH_CALLBACK_URL']);
$client->addScope('email');
$client->addScope('profile');
$loginUrl = $client->createAuthUrl();
} catch(\Exception $e) {
throwError(500, $e->getMessage());
}

try {
$oauth2 = new Google_Service_Oauth2($client);
$oauth2->userinfo->get();
$loggedIn = true;
} catch(\Exception $e) {
$this->responseJson([
'error' => $e->getMessage(),
'loggedIn' => false,
'user' => $user,
'googleOAuthSignInUrl' => $loginUrl,
'structures' => $structures,
'endpoints' => $endpoints,
]);
}

// Check if user already exists
$stmt = $this->query('get-user-by-google-id', [
'id' => $this->getCurrentUserId(),
]);

if ($stmt->rowCount() > 0) {
$user = $stmt->fetch(PDO::FETCH_OBJ);
}
$loginUrl = $this->getLoginUrl();
}

if ($loggedIn && isset($user)) {
Expand Down