|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once '../vendor/autoload.php'; |
| 4 | + |
| 5 | +use Frontegg\Frontegg; |
| 6 | +use GuzzleHttp\Psr7\Request; |
| 7 | +use Psr\Http\Message\RequestInterface; |
| 8 | + |
| 9 | +/* Configure Frontegg SDK. */ |
| 10 | +/** |
| 11 | + * Your Client ID. You can setup env variable or change default value here. |
| 12 | + */ |
| 13 | +$clientId = getenv('FRONTEGG_CLIENT_ID') |
| 14 | + ? getenv('FRONTEGG_CLIENT_ID') |
| 15 | + : 'THE-CLIENT-ID'; |
| 16 | +/** |
| 17 | + * Your Secret API Key. You can setup env variable or change default value here. |
| 18 | + */ |
| 19 | +$apiKey = getenv('FRONTEGG_CLIENT_SECRET_KEY') |
| 20 | + ? getenv('FRONTEGG_CLIENT_SECRET_KEY') |
| 21 | + : 'THE-API-KEY'; |
| 22 | +/** |
| 23 | + * Your tenant ID. You can setup env variable or change default value here. |
| 24 | + */ |
| 25 | +$tenantId = getenv('FRONTEGG_TENANT_ID') |
| 26 | + ? getenv('FRONTEGG_TENANT_ID') |
| 27 | + : 'THE-TENANT-ID'; |
| 28 | +$config = [ |
| 29 | + 'clientId' => $clientId, |
| 30 | + 'clientSecret' => $apiKey, |
| 31 | + 'apiBaseUrl' => 'https://dev-api.frontegg.com/', |
| 32 | + 'contextResolver' => function (RequestInterface $request) use ($tenantId) { |
| 33 | + return [ |
| 34 | + 'tenantId' => $tenantId, |
| 35 | + 'userId' => 'TEST-USER-ID', |
| 36 | + 'permissions' => [], |
| 37 | + ]; |
| 38 | + }, |
| 39 | + 'disableCors' => true, |
| 40 | +]; |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * Initialize the main Frontegg SDK component. |
| 45 | + */ |
| 46 | +$frontegg = new Frontegg($config); |
| 47 | + |
| 48 | +/** |
| 49 | + * Setup routing rule for "/frontegg" URIs. |
| 50 | + * Can be a part of middleware f.e. in Laravel. |
| 51 | + */ |
| 52 | +if (isset($_SERVER['REQUEST_URI']) |
| 53 | + && strpos($_SERVER['REQUEST_URI'], '/frontegg') === 0 |
| 54 | +) { |
| 55 | + $request = new Request($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); |
| 56 | + |
| 57 | + try { |
| 58 | + $response = $frontegg->forward($request); |
| 59 | + } catch (Exception $e) { |
| 60 | + sendResponse(500, [], sprintf('Error happened: %s', $e->getMessage())); |
| 61 | + } |
| 62 | + |
| 63 | + sendResponse($response->getHttpResponseCode(), $response->getHeaders(), $response->getBody()); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Setup routing rule for POST "/audit" URIs. |
| 68 | + * Can be a part of middleware f.e. in Laravel. |
| 69 | + */ |
| 70 | +elseif (isset($_SERVER['REQUEST_URI']) |
| 71 | + && strpos($_SERVER['REQUEST_URI'], '/audit') === 0 |
| 72 | + && $_SERVER['REQUEST_METHOD'] === 'POST' |
| 73 | +) { |
| 74 | + $payload = file_get_contents('php://input'); |
| 75 | + $auditLog = json_decode($payload, true); |
| 76 | + |
| 77 | + try { |
| 78 | + $responseData = $frontegg->sendAudit($tenantId, $auditLog); |
| 79 | + } catch (Exception $e) { |
| 80 | + sendResponse(500, [], sprintf('Error happened: %s', $e->getMessage())); |
| 81 | + } |
| 82 | + |
| 83 | + file_put_contents(__DIR__ . '/logs/responses201.txt', json_encode($responseData) . "\n", FILE_APPEND); |
| 84 | + sendResponse(201, ['Content-Type' => ['application/json']], json_encode($responseData)); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * ONLY FOR CORS! |
| 89 | + * Setup routing rule for POST "/audit" URIs. |
| 90 | + * Can be a part of middleware f.e. in Laravel. |
| 91 | + */ |
| 92 | +elseif (isset($_SERVER['REQUEST_URI']) |
| 93 | + && strpos($_SERVER['REQUEST_URI'], '/audit') === 0 |
| 94 | + && $_SERVER['REQUEST_METHOD'] === 'OPTIONS' |
| 95 | +) { |
| 96 | + sendResponse(200, [], ''); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Default not found error response. |
| 101 | + */ |
| 102 | +sendResponse( |
| 103 | + 404, |
| 104 | + [], |
| 105 | + sprintf('"%s" URL not found', $_SERVER['REQUEST_URI']) |
| 106 | +); |
| 107 | + |
| 108 | + |
| 109 | +// --- Helper functions --- |
| 110 | + |
| 111 | +/** |
| 112 | + * Sends response to the client. |
| 113 | + * Stops script running. |
| 114 | + * |
| 115 | + * @param int $httpCode |
| 116 | + * @param array $headers |
| 117 | + * @param string $body |
| 118 | + */ |
| 119 | +function sendResponse($httpCode = 200, array $headers = [], string $body = ''): void |
| 120 | +{ |
| 121 | + http_response_code($httpCode); |
| 122 | + /** |
| 123 | + * This is correct handling for CORS. |
| 124 | + */ |
| 125 | + cors(); |
| 126 | + sendHeaders($headers); |
| 127 | + print $body; |
| 128 | + |
| 129 | + exit; |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Send HTTP headers if they have not been sent yet. |
| 134 | + * |
| 135 | + * @param array $headers |
| 136 | + * |
| 137 | + * @return void |
| 138 | + */ |
| 139 | +function sendHeaders(array $headers = ['Content-Type' => ['text/html']]): void |
| 140 | +{ |
| 141 | + if (headers_sent()) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($headers as $name => $headerValues) { |
| 146 | + foreach ($headerValues as $value) { |
| 147 | + header(sprintf('%s:%s', $name, $value), false); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any |
| 154 | + * origin. |
| 155 | + * |
| 156 | + * In a production environment, you probably want to be more restrictive, but this gives you |
| 157 | + * the general idea of what is involved. For the nitty-gritty low-down, read: |
| 158 | + * |
| 159 | + * - https://developer.mozilla.org/en/HTTP_access_control |
| 160 | + * - http://www.w3.org/TR/cors/ |
| 161 | + * |
| 162 | + */ |
| 163 | +function cors() { |
| 164 | + |
| 165 | + // Allow from any origin |
| 166 | + if (isset($_SERVER['HTTP_ORIGIN'])) { |
| 167 | + // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one |
| 168 | + // you want to allow, and if so: |
| 169 | + header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); |
| 170 | + header('Access-Control-Allow-Credentials: true'); |
| 171 | + header('Access-Control-Max-Age: 86400'); // cache for 1 day |
| 172 | + } |
| 173 | + |
| 174 | + // Access-Control headers are received during OPTIONS requests |
| 175 | + if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
| 176 | + |
| 177 | + if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) |
| 178 | + // may also be using PUT, PATCH, HEAD etc |
| 179 | + header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); |
| 180 | + |
| 181 | + if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) |
| 182 | + header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); |
| 183 | + } |
| 184 | +} |
0 commit comments