Skip to content

Commit

Permalink
Release of the Version 0.2 (#14)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
nuzzy and klebster authored Sep 1, 2020
1 parent 2a32326 commit 18e90cf
Show file tree
Hide file tree
Showing 59 changed files with 3,275 additions and 640 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: php-actions/composer@v1
- name: PHPUnit Tests
uses: php-actions/phpunit@v9
with:
configuration: ./phpunit.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
/vendor/
composer.lock
.phpunit.result.cache
test-authenticator.php
test-audits.php
test-events.php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ext-json": "*",
"ext-curl": "*",
"psr/http-message": "^1.0",
"relay/relay": "1.0",
"relay/relay": "^1.0",
"guzzlehttp/psr7": "^1.6"
},
"require-dev": {
Expand Down
22 changes: 22 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.1"

services:
web:
image: nginx:alpine
volumes:
- ./:/app/docker
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 8080:80

app:
build:
context: ../
dockerfile: ./docker/php/Dockerfile
volumes:
- ../:/app
- ./php/php.ini:/usr/local/etc/php/php.ini
environment:
- FRONTEGG_CLIENT_ID=THE-CLIENT-ID
- FRONTEGG_CLIENT_SECRET_KEY=THE-API-KEY
- FRONTEGG_TENANT_ID=THE-TENANT-ID
184 changes: 184 additions & 0 deletions docker/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

require_once '../vendor/autoload.php';

use Frontegg\Frontegg;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;

/* Configure Frontegg SDK. */
/**
* Your Client ID. You can setup env variable or change default value here.
*/
$clientId = getenv('FRONTEGG_CLIENT_ID')
? getenv('FRONTEGG_CLIENT_ID')
: 'THE-CLIENT-ID';
/**
* Your Secret API Key. You can setup env variable or change default value here.
*/
$apiKey = getenv('FRONTEGG_CLIENT_SECRET_KEY')
? getenv('FRONTEGG_CLIENT_SECRET_KEY')
: 'THE-API-KEY';
/**
* Your tenant ID. You can setup env variable or change default value here.
*/
$tenantId = getenv('FRONTEGG_TENANT_ID')
? getenv('FRONTEGG_TENANT_ID')
: 'THE-TENANT-ID';
$config = [
'clientId' => $clientId,
'clientSecret' => $apiKey,
'apiBaseUrl' => 'https://dev-api.frontegg.com/',
'contextResolver' => function (RequestInterface $request) use ($tenantId) {
return [
'tenantId' => $tenantId,
'userId' => 'TEST-USER-ID',
'permissions' => [],
];
},
'disableCors' => true,
];


/**
* Initialize the main Frontegg SDK component.
*/
$frontegg = new Frontegg($config);

/**
* Setup routing rule for "/frontegg" URIs.
* Can be a part of middleware f.e. in Laravel.
*/
if (isset($_SERVER['REQUEST_URI'])
&& strpos($_SERVER['REQUEST_URI'], '/frontegg') === 0
) {
$request = new Request($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

try {
$response = $frontegg->forward($request);
} catch (Exception $e) {
sendResponse(500, [], sprintf('Error happened: %s', $e->getMessage()));
}

sendResponse($response->getHttpResponseCode(), $response->getHeaders(), $response->getBody());
}

/**
* Setup routing rule for POST "/audit" URIs.
* Can be a part of middleware f.e. in Laravel.
*/
elseif (isset($_SERVER['REQUEST_URI'])
&& strpos($_SERVER['REQUEST_URI'], '/audit') === 0
&& $_SERVER['REQUEST_METHOD'] === 'POST'
) {
$payload = file_get_contents('php://input');
$auditLog = json_decode($payload, true);

try {
$responseData = $frontegg->sendAudit($tenantId, $auditLog);
} catch (Exception $e) {
sendResponse(500, [], sprintf('Error happened: %s', $e->getMessage()));
}

file_put_contents(__DIR__ . '/logs/responses201.txt', json_encode($responseData) . "\n", FILE_APPEND);
sendResponse(201, ['Content-Type' => ['application/json']], json_encode($responseData));
}

/**
* ONLY FOR CORS!
* Setup routing rule for POST "/audit" URIs.
* Can be a part of middleware f.e. in Laravel.
*/
elseif (isset($_SERVER['REQUEST_URI'])
&& strpos($_SERVER['REQUEST_URI'], '/audit') === 0
&& $_SERVER['REQUEST_METHOD'] === 'OPTIONS'
) {
sendResponse(200, [], '');
}

/**
* Default not found error response.
*/
sendResponse(
404,
[],
sprintf('"%s" URL not found', $_SERVER['REQUEST_URI'])
);


// --- Helper functions ---

/**
* Sends response to the client.
* Stops script running.
*
* @param int $httpCode
* @param array $headers
* @param string $body
*/
function sendResponse($httpCode = 200, array $headers = [], string $body = ''): void
{
http_response_code($httpCode);
/**
* This is correct handling for CORS.
*/
cors();
sendHeaders($headers);
print $body;

exit;
}

/**
* Send HTTP headers if they have not been sent yet.
*
* @param array $headers
*
* @return void
*/
function sendHeaders(array $headers = ['Content-Type' => ['text/html']]): void
{
if (headers_sent()) {
return;
}

foreach ($headers as $name => $headerValues) {
foreach ($headerValues as $value) {
header(sprintf('%s:%s', $name, $value), false);
}
}
}

/**
* An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any
* origin.
*
* In a production environment, you probably want to be more restrictive, but this gives you
* the general idea of what is involved. For the nitty-gritty low-down, read:
*
* - https://developer.mozilla.org/en/HTTP_access_control
* - http://www.w3.org/TR/cors/
*
*/
function cors() {

// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
}
29 changes: 29 additions & 0 deletions docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
server {
listen 80 default;

client_max_body_size 108M;

access_log /var/log/nginx/application.access.log;

root /app/docker;
index index.php;

location ~* \.(js|jpg|png|css)$ {
root /app/docker;
}

if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}

location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
}

}
24 changes: 24 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM php:7.3-fpm

RUN apt-get update \
&& apt-get -y install bash curl \
&& cd / && rm -fr /src \
&& apt-get -y update -qq \
&& apt-get -y clean > /dev/null \
&& rm -rf \
/tmp/* \
/usr/share/doc/* \
/var/lib/apt/lists/* \
/var/tmp/* \
/var/www/*

RUN mkdir /app

WORKDIR /app

## Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

COPY . /app

USER www-data
Loading

0 comments on commit 18e90cf

Please sign in to comment.