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
159 changes: 159 additions & 0 deletions .datatable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- MySQL dump 10.13 Distrib 8.0.41, for Win64 (x86_64)
--
-- Host: 127.0.0.1 Database: jsonms_local
-- ------------------------------------------------------
-- Server version 8.0.41

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `errors`
--

DROP TABLE IF EXISTS `errors`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `errors` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`key` varchar(255) NOT NULL,
`message` text,
`source` varchar(256) DEFAULT NULL,
`line` smallint DEFAULT NULL,
`column` smallint DEFAULT NULL,
`stack` text,
`occurred_on` datetime NOT NULL,
`last_timestamp` bigint NOT NULL,
`version` varchar(16) NOT NULL,
`route` varchar(128) NOT NULL,
`count` smallint NOT NULL DEFAULT '0',
`user_agent` varchar(255) NOT NULL,
`created_on` datetime DEFAULT (now()),
`created_by` int unsigned NOT NULL,
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `errors_pk` (`key`)
) ENGINE=InnoDB AUTO_INCREMENT=88 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `history`
--

DROP TABLE IF EXISTS `history`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `history` (
`id` int NOT NULL AUTO_INCREMENT,
`uuid` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` text COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `history_interfaces_uuid_fk` (`uuid`),
KEY `history_users_id_fk` (`created_by`),
CONSTRAINT `history_interfaces_uuid_fk` FOREIGN KEY (`uuid`) REFERENCES `structures` (`uuid`) ON DELETE CASCADE,
CONSTRAINT `history_users_id_fk` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=378 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `permissions`
--

DROP TABLE IF EXISTS `permissions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
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,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `structures`
--

DROP TABLE IF EXISTS `structures`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `structures` (
`uuid` char(36) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT (uuid()),
`hash` char(10) COLLATE utf8mb4_unicode_ci NOT NULL,
`label` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`logo` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`content` text COLLATE utf8mb4_unicode_ci NOT NULL,
`webhook` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_by` int NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`uuid`),
KEY `interfaces_users_id_fk` (`created_by`),
KEY `interfaces__hash_index` (`hash`),
KEY `interfaces_webhooks_uuid_fk` (`webhook`),
CONSTRAINT `interfaces_users_id_fk` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`),
CONSTRAINT `interfaces_webhooks_uuid_fk` FOREIGN KEY (`webhook`) REFERENCES `webhooks` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`google_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `google_id` (`google_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `webhooks`
--

DROP TABLE IF EXISTS `webhooks`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `webhooks` (
`uuid` char(36) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT (uuid()),
`url` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`secret` varchar(84) COLLATE utf8mb4_unicode_ci NOT NULL,
`cypher` varchar(84) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_by` int NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`uuid`),
KEY `interfaces_webhooks_users_id_fk` (`created_by`),
CONSTRAINT `interfaces_webhooks_users_id_fk` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2025-04-26 18:13:45
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# General
INTERFACE_EDITOR_URL=http://localhost:3000
ACCESS_CONTROL_ALLOW_ORIGIN=http://localhost:3000
JSONMS_CYPHER_KEY=urjMdK071cL935eKdczjEQ==
JSONMS_CYPHER_KEY=

# Database
DATABASE_HOST=localhost
DATABASE_DBNAME=
DATABASE_USERNAME=root
DATABASE_USERNAME=
DATABASE_PASSWORD=

# Google OAuth
Expand Down
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
# @jsonms/server

The server to use with your instance of [jsonms-www](https://github.com/JSON-ms/www).
Welcome to the **@jsonms/server** project!
This package provides the server-side foundation for managing MySQL schemas and environment configurations for the JSONMS system.

## Requirements
- PHP 8.x
- MySQL server

## Getting Started

Follow these steps to set up and run the project locally:

### 1. Prepare the MySQL Database

A `.datatable.sql` file is included in the project.
It contains the necessary schema definitions required to run the application.

To set up your database:

```bash
mysql -u your_user -p your_database_name < .datatable.sql
```

Replace your_user and your_database_name with your MySQL username and target database name.

Make sure your MySQL server is running and accessible.

### 2. Set Up Environment Variables
The project uses environment variables for configuration.
A sample file `.env.example` is provided.

To create your local `.env` file:

```bash
cp .env.example .env
```

Edit `.env` to match your environment settings, such as database connection credentials, ports, and other options.

### 3. Running the Server

You can define your own virtual host with Apache or Nginx but the fastest way to run the server is with a built-in PHP server.

```bash
php -S localhost:9001 index.php
```

## Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.

## License
This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.
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.8",
"version": "1.0.12",
"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
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

use JSONms\Controllers\RestfulController;

class WebhookController extends RestfulController {
class EndpointController extends RestfulController {

public function saveAction($webhooks) {
foreach($webhooks as $webhook) {
if (isset($webhook->uuid)) {
$this->query('update-webhook-by-uuid', [
'uuid' => $webhook->uuid,
'url' => $webhook->url,
public function saveAction($endpoints) {
foreach($endpoints as $endpoint) {
if (isset($endpoint->uuid)) {
$this->query('update-endpoint-by-uuid', [
'uuid' => $endpoint->uuid,
'url' => $endpoint->url,
'userId' => $this->getCurrentUserId(),
]);
} else {
$cypherKey = $this->getHash(24);
$serverSecret = $this->encrypt($this->getHash(24), $cypherKey);
$encryptedCypherKey = $this->encrypt($cypherKey, $_ENV['JSONMS_CYPHER_KEY']);
$this->query('insert-webhook', [
'url' => $webhook->url,
$this->query('insert-endpoint', [
'url' => $endpoint->url,
'secret' => $serverSecret,
'cypher' => $encryptedCypherKey,
'created_by' => $this->getCurrentUserId(),
]);
}
}
$stmt = $this->query('get-all-webhooks', [
$stmt = $this->query('get-all-endpoints', [
'userId' => $this->getCurrentUserId(),
]);

Expand All @@ -34,7 +34,7 @@ public function saveAction($webhooks) {
}

public function deleteAction($id) {
$stmt = $this->query('delete-webhook-by-uuid', [
$stmt = $this->query('delete-endpoint-by-uuid', [
'uuid' => $id,
'userId' => $this->getCurrentUserId(),
]);
Expand All @@ -44,28 +44,28 @@ public function deleteAction($id) {
}

public function secretKeyAction($uuid) {
$webhook = $this->getWebhook($uuid);
$decryptedCypherKey = $this->decrypt($webhook->cypher, $_ENV['JSONMS_CYPHER_KEY']);
$decryptedServerKey = $this->decrypt($webhook->secret, $decryptedCypherKey);
$endpoint = $this->getEndpoint($uuid);
$decryptedCypherKey = $this->decrypt($endpoint->cypher, $_ENV['JSONMS_CYPHER_KEY']);
$decryptedServerKey = $this->decrypt($endpoint->secret, $decryptedCypherKey);
$this->responseJson($decryptedServerKey);
}

public function cypherKeyAction($uuid) {
$webhook = $this->getWebhook($uuid);
$decryptedCypherKey = $this->decrypt($webhook->cypher, $_ENV['JSONMS_CYPHER_KEY']);
$endpoint = $this->getEndpoint($uuid);
$decryptedCypherKey = $this->decrypt($endpoint->cypher, $_ENV['JSONMS_CYPHER_KEY']);
$this->responseJson($decryptedCypherKey);
}

private function getWebhook($uuid, $showError = true): false | stdClass {
$stmt = $this->query('get-webhook-by-uuid', [
private function getEndpoint($uuid, $showError = true): false | stdClass {
$stmt = $this->query('get-endpoint-by-uuid', [
'uuid' => $uuid,
'userId' => $this->getCurrentUserId(),
]);
if ($stmt->rowCount() > 0) {
return $stmt->fetch(PDO::FETCH_OBJ);
}
if ($showError) {
throwError(403, 'You don\'t have permission to access this webhook');
throwError(403, 'You don\'t have permission to access this endpoint');
}
return false;
}
Expand Down
Loading