Skip to content

Commit 18e90cf

Browse files
nuzzyklebster
andauthored
Release of the Version 0.2 (#14)
* Task "Build authenticator". (#1) * Task "Build authenticator". * Created Frontegg main class. * Created Helper classes. * Created "readme.md" file. * Created "composer.json" file with package dependencies. * Created tests for base functionality. * Task "Build authenticator". * Update Frontegg main class. * Updated Helper classes. * Updated "readme.md" file. * Updated "composer.json" file "ext-curl" dependency. * Updated tests for base functionality. * Task "Build authenticator". * Update Frontegg main class. * Updated Helper classes. * Updated "readme.md" file. * Updated "composer.json" file "ext-curl" dependency. * Updated tests for base functionality. * Task "Build Authenticator" * Updated code style using CodeSniffer and PhpStan. * Updated "composer.json" * Audits SDK (#2) * Task "Build authenticator". * Created Frontegg main class. * Created Helper classes. * Created "readme.md" file. * Created "composer.json" file with package dependencies. * Created tests for base functionality. * Task "Build authenticator". * Update Frontegg main class. * Updated Helper classes. * Updated "readme.md" file. * Updated "composer.json" file "ext-curl" dependency. * Updated tests for base functionality. * Task "Build authenticator". * Update Frontegg main class. * Updated Helper classes. * Updated "readme.md" file. * Updated "composer.json" file "ext-curl" dependency. * Updated tests for base functionality. * Task "Build Authenticator" * Updated code style using CodeSniffer and PhpStan. * Updated "composer.json" * Task "Audits SDK" * Created new AuditsClient class for API calls to Audit Logs endpoints. * Updated main classes. * Created new tests for Audit Logs classes. * Updated and refactored existing tests. * Updated "readme.md" file. * Task "Audits SDK" * Fixed code styles. * Task "Audits Log" * Small fixes * Events SDK (#4) * Task "Events SDK" * Created basic classes for Events API. * Updated existing classes. * Created tests for Events API. * Other small changes. * Task "Events SDK" * Created basic classes for Events API. * Updated existing classes. * Created tests for Events API. * Other small changes. * Task "Events SDK" * Updated Events classes and tests. * Created class and tests for Slack channel. * Other small updates. * Task "Events SDK" * Fixed code styles. * Task "Events SDK" * Fixed an error with API URL. * Updated basic classes and tests. * Task "Events SDK" * Renamed config services constants. * Frontegg Middleware Proxy (#5) * Added Proxy functionality * Included `psr/http-message`, 'relay/relay', `guzzlehttp/psr7` packages * Intruduced `Proxy` class * Introduced `FronteggAdapter` class * Added `FronteggRequestHeaderResolver` and `FronteggRequestMethodResolver` * Adjusted `FronteggCurlHttpClient` with `DEFAULT_TIMEOUT` constant * Task "Events SDK" * Created basic classes for Events API. * Updated existing classes. * Created tests for Events API. * Other small changes. * Task "Events SDK" * Created basic classes for Events API. * Updated existing classes. * Created tests for Events API. * Other small changes. * Task "Events SDK" * Updated Events classes and tests. * Created class and tests for Slack channel. * Other small updates. * Task "Events SDK" * Fixed code styles. * Task "Frontegg middleware" * Updated Proxy classes. * Created tests for Proxy classes. * Task "Events SDK" * Fixed an error with API URL. * Updated basic classes and tests. * Task "Frontegg Middleware" * Updated basic classes and tests. * Created Proxy filter classes. * Other changes. * Task "Events SDK" * Renamed config services constants. * Task "Frontegg Middleware" * Updated and refactored Proxy classes. * Added new tests for Proxy middleware. * Task "Frontegg Middleware" * Fixed code styles. * Task "Frontegg Middleware" * Fixed merge conflicts. * Task "Frontegg Middleware" * Updated Proxy classes to properly handle POST requests. * Created tests for Proxy API. Co-authored-by: Daniel Kleba <[email protected]> * Task "Docs Update" (#9) * Updated "readme.md" * Task "Code Updates and Refactoring" (#11) * Fixed "composer.json" file to be valid. * Refactored ApiErrors to be a part of ApiErrorTrait. * Refactored getDecodedJsonData() method to be a part of ApiJsonTrait. * Updated directory structure for Events. * Updated Audits GET request to properly send query parameters. * Updated tests. * Other small changes. * Task "Run tests on github on push" (#12) * Added Github workflow to run phpunit tests. * Removed credentials from the tests. * Created phpunit.xml config file * Demo Using Docker Env (#10) * Task "Docker demo for raw PHP" * Created demo environment with docker for raw PHP. * Docker demo for raw PHP * Removed credentials from the code. * Updated code. * Task "Demo site on Docker env" * Updated demo site in "docker/index.php". * Updated "docker-compose.yml" file. * Removed credentials from the tests. * Task "Fix sending event trigger channels bug" (#13) * Updated Events channel classes. * Updated and added tests for new code changes. * Updated "readme.md" file. Co-authored-by: Daniel Kleba <[email protected]>
1 parent 2a32326 commit 18e90cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3275
-640
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build-test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: php-actions/composer@v1
12+
- name: PHPUnit Tests
13+
uses: php-actions/phpunit@v9
14+
with:
15+
configuration: ./phpunit.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea/
22
/vendor/
33
composer.lock
4+
.phpunit.result.cache
45
test-authenticator.php
56
test-audits.php
67
test-events.php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"ext-json": "*",
1717
"ext-curl": "*",
1818
"psr/http-message": "^1.0",
19-
"relay/relay": "1.0",
19+
"relay/relay": "^1.0",
2020
"guzzlehttp/psr7": "^1.6"
2121
},
2222
"require-dev": {

docker/docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: "3.1"
2+
3+
services:
4+
web:
5+
image: nginx:alpine
6+
volumes:
7+
- ./:/app/docker
8+
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
9+
ports:
10+
- 8080:80
11+
12+
app:
13+
build:
14+
context: ../
15+
dockerfile: ./docker/php/Dockerfile
16+
volumes:
17+
- ../:/app
18+
- ./php/php.ini:/usr/local/etc/php/php.ini
19+
environment:
20+
- FRONTEGG_CLIENT_ID=THE-CLIENT-ID
21+
- FRONTEGG_CLIENT_SECRET_KEY=THE-API-KEY
22+
- FRONTEGG_TENANT_ID=THE-TENANT-ID

docker/index.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
}

docker/nginx/nginx.conf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
server {
2+
listen 80 default;
3+
4+
client_max_body_size 108M;
5+
6+
access_log /var/log/nginx/application.access.log;
7+
8+
root /app/docker;
9+
index index.php;
10+
11+
location ~* \.(js|jpg|png|css)$ {
12+
root /app/docker;
13+
}
14+
15+
if (!-e $request_filename) {
16+
rewrite ^.*$ /index.php last;
17+
}
18+
19+
location ~ \.php$ {
20+
fastcgi_pass app:9000;
21+
fastcgi_index index.php;
22+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
23+
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
24+
fastcgi_buffers 16 16k;
25+
fastcgi_buffer_size 32k;
26+
include fastcgi_params;
27+
}
28+
29+
}

docker/php/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM php:7.3-fpm
2+
3+
RUN apt-get update \
4+
&& apt-get -y install bash curl \
5+
&& cd / && rm -fr /src \
6+
&& apt-get -y update -qq \
7+
&& apt-get -y clean > /dev/null \
8+
&& rm -rf \
9+
/tmp/* \
10+
/usr/share/doc/* \
11+
/var/lib/apt/lists/* \
12+
/var/tmp/* \
13+
/var/www/*
14+
15+
RUN mkdir /app
16+
17+
WORKDIR /app
18+
19+
## Install Composer
20+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
21+
22+
COPY . /app
23+
24+
USER www-data

0 commit comments

Comments
 (0)