-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
209 lines (176 loc) · 7.88 KB
/
server.php
File metadata and controls
209 lines (176 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Keep Web UI Server
*
* Lightweight HTTP server for local web UI.
* Run with: php -S localhost:4000 src/Server/server.php
*/
// Find the autoloader - works both when developing Keep and when installed as a package
$autoloadPaths = [
__DIR__ . '/../../vendor/autoload.php', // Keep development (keep/vendor/autoload.php)
__DIR__ . '/../../../../autoload.php', // Installed as package (vendor/stechstudio/keep/src/Server -> vendor/autoload.php)
__DIR__ . '/../../../../../vendor/autoload.php', // Installed globally with Composer
];
$autoloadFound = false;
foreach ($autoloadPaths as $path) {
if (file_exists($path)) {
require_once $path;
$autoloadFound = true;
break;
}
}
if (!$autoloadFound) {
die("Error: Unable to find Composer autoload.php. Please run 'composer install'.\n");
}
use STS\Keep\KeepManager;
use STS\Keep\KeepContainer;
use STS\Keep\Data\Settings;
use STS\Keep\Data\Collections\VaultConfigCollection;
use STS\Keep\Server\Router;
use STS\Keep\Server\Controllers\SecretController;
use STS\Keep\Server\Controllers\VaultController;
use STS\Keep\Server\Controllers\ExportController;
use STS\Keep\Server\Controllers\ImportController;
use STS\Keep\Server\Controllers\TemplateController;
use STS\Keep\Server\Controllers\WorkspaceController;
// Initialize server - get token from environment or generate one
$AUTH_TOKEN = $_ENV['KEEP_AUTH_TOKEN'] ?? $_SERVER['KEEP_AUTH_TOKEN'] ?? bin2hex(random_bytes(32));
// Get request details
$method = $_SERVER['REQUEST_METHOD'];
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$query = [];
parse_str($_SERVER['QUERY_STRING'] ?? '', $query);
// Serve static files
if ($path === '/' || $path === '/index.html') {
serveIndexWithToken(__DIR__ . '/public/index.html', $AUTH_TOKEN);
}
if (str_starts_with($path, '/assets/')) {
$file = __DIR__ . '/public' . $path;
if (file_exists($file)) {
$mimeTypes = [
'js' => 'application/javascript',
'css' => 'text/css',
'json' => 'application/json',
'png' => 'image/png',
'svg' => 'image/svg+xml',
];
$ext = pathinfo($file, PATHINFO_EXTENSION);
serveFile($file, $mimeTypes[$ext] ?? 'application/octet-stream');
}
}
// API routes require authentication
if (str_starts_with($path, '/api/')) {
// Check auth token
$headers = getallheaders();
$token = $headers['X-Auth-Token'] ?? $headers['x-auth-token'] ?? '';
if (!hash_equals($AUTH_TOKEN, $token)) {
jsonResponse(['error' => 'Unauthorized'], 401);
}
// Get JSON body if present
$body = [];
if (in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'])) {
$input = file_get_contents('php://input');
if ($input) {
$body = json_decode($input, true) ?? [];
}
}
// Initialize Keep with proper settings and register in container
$container = KeepContainer::getInstance();
$manager = new KeepManager(Settings::load(), VaultConfigCollection::load());
$container->instance(KeepManager::class, $manager);
// Setup router and register routes
$router = new Router($manager);
// Secret routes
$router->get('/api/secrets', [SecretController::class, 'list']);
$router->get('/api/secrets/validation-rules', [SecretController::class, 'validationRules']);
$router->get('/api/secrets/:key', [SecretController::class, 'get']);
$router->post('/api/secrets', [SecretController::class, 'create']);
$router->put('/api/secrets/:key', [SecretController::class, 'update']);
$router->delete('/api/secrets/:key', [SecretController::class, 'delete']);
$router->post('/api/secrets/:key/rename', [SecretController::class, 'rename']);
$router->post('/api/secrets/:key/copy-to-env', [SecretController::class, 'copyToEnv']);
$router->get('/api/secrets/:key/history', [SecretController::class, 'history']);
$router->get('/api/search', [SecretController::class, 'search']);
// Vault & Settings routes
$router->get('/api/vaults', [VaultController::class, 'list']);
$router->post('/api/vaults', [VaultController::class, 'addVault']);
$router->put('/api/vaults/:slug', [VaultController::class, 'updateVault']);
$router->delete('/api/vaults/:slug', [VaultController::class, 'deleteVault']);
$router->get('/api/envs', [VaultController::class, 'listEnvs']);
$router->post('/api/envs', [VaultController::class, 'addEnv']);
$router->delete('/api/envs', [VaultController::class, 'removeEnv']);
$router->get('/api/settings', [VaultController::class, 'getSettings']);
$router->put('/api/settings', [VaultController::class, 'updateSettings']);
$router->post('/api/verify', [VaultController::class, 'verify']);
$router->get('/api/diff', [VaultController::class, 'diff']);
// Workspace routes
$router->get('/api/workspace', [WorkspaceController::class, 'get']);
$router->put('/api/workspace', [WorkspaceController::class, 'update']);
$router->post('/api/workspace/verify', [WorkspaceController::class, 'verify']);
// Export routes
$router->post('/api/export', [ExportController::class, 'export']);
// Import routes
$router->post('/api/import/analyze', [ImportController::class, 'analyze']);
$router->post('/api/import/execute', [ImportController::class, 'execute']);
// Template routes
$router->get('/api/templates', [TemplateController::class, 'index']);
$router->get('/api/templates/placeholders', [TemplateController::class, 'placeholders']);
$router->get('/api/templates/:filename', [TemplateController::class, 'show']);
$router->put('/api/templates/:filename', [TemplateController::class, 'update']);
$router->delete('/api/templates/:filename', [TemplateController::class, 'delete']);
$router->post('/api/templates/generate', [TemplateController::class, 'generate']);
$router->post('/api/templates/validate', [TemplateController::class, 'validate']);
$router->post('/api/templates/process', [TemplateController::class, 'process']);
$router->post('/api/templates/create', [TemplateController::class, 'create']);
// Dispatch the request
$response = $router->dispatch($method, $path, $query, $body);
// Send response
$status = $response['_status'] ?? 200;
unset($response['_status']);
jsonResponse($response, $status);
}
// Default 404
http_response_code(404);
echo "Not found";
exit;
// ============================================================================
// Helper Functions
// ============================================================================
function serveFile(string $path, string $contentType): void
{
if (!file_exists($path)) {
http_response_code(404);
echo "Not found";
exit;
}
header('Content-Type: ' . $contentType);
header('Cache-Control: public, max-age=3600');
readfile($path);
exit;
}
function serveIndexWithToken(string $path, string $token): void
{
if (!file_exists($path)) {
http_response_code(404);
echo "Not found";
exit;
}
$html = file_get_contents($path);
// Inject the token as a JavaScript variable
$injection = "<script>window.KEEP_AUTH_TOKEN = '" . htmlspecialchars($token) . "';</script>";
$html = str_replace('</head>', $injection . "\n</head>", $html);
header('Content-Type: text/html');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'none'");
echo $html;
exit;
}
function jsonResponse(array $data, int $status = 200): void
{
http_response_code($status);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}