Skip to content

Commit b1717f7

Browse files
committed
Added middleware - tested
1 parent 2e6e7e6 commit b1717f7

3 files changed

Lines changed: 72 additions & 68 deletions

File tree

src/http-client.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<?php
22

3-
4-
53
use GuzzleHttp\Client;
64
use GuzzleHttp\HandlerStack;
7-
use GuzzleHttp\Psr7\Uri;
8-
use Psr\Http\Message\RequestInterface;
9-
5+
106
class HttpClient extends Client
117
{
128
public function __construct($apiKey, SecureNativeOptions $options)
139
{
14-
$defaultOptions = [
15-
'handler' => $this->getHandlerStack(),
16-
'base_uri' => $options->getApiUrl(),
17-
'timeout' => $options->getTimeout() / 1000,
18-
'headers' => [
19-
'User-Agent' => 'SecureNative-PHP',
20-
'SN-Version'=> '1.0.0',
21-
'Authorization' => $apiKey,
22-
'Content-Type' => 'application/json',
10+
$defaultOptions = [
11+
'handler' => $this->getHandlerStack(),
12+
'base_uri' => $options->getApiUrl(),
13+
'timeout' => $options->getTimeout() / 1000,
14+
'headers' => [
15+
'User-Agent' => 'SecureNative-PHP',
16+
'SN-Version' => '1.0.0',
17+
'Authorization' => $apiKey,
18+
'Content-Type' => 'application/json',
2319
]
2420
];
2521

@@ -35,7 +31,8 @@ protected function getHandlerStack()
3531
return $handlerStack;
3632
}
3733

38-
protected function getHandler() {
34+
protected function getHandler()
35+
{
3936
return null;
4037
}
41-
}
38+
}

src/middleware.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
class Middleware
66
{
7-
private $apiKey;
7+
private $apiKey;
88

9-
public function __construct($apiKey)
10-
{
11-
$this->apiKey = $apiKey;
12-
}
9+
public function __construct($apiKey)
10+
{
11+
$this->apiKey = $apiKey;
12+
}
1313

14-
public function verifySignature($headers, $body)
15-
{
16-
$signature = headers[SIGNATURE_KEY] ? headers[SIGNATURE_KEY] : "";
17-
$comparison_signature = hash_hmac('sha512', Utils::serialize($body), $this->apiKey, true);
18-
return hash_equals($signature, $comparison_signature);
19-
}
14+
public function verifySignature()
15+
{
16+
$headers = $_SERVER;
17+
$body = file_get_contents("php://input");
18+
$signature = isset($headers[SIGNATURE_KEY]) ? $headers[SIGNATURE_KEY] : "";
19+
$comparison_signature = bin2hex(hash_hmac('sha512', $body, $this->apiKey, true));
20+
return hash_equals($signature, $comparison_signature);
21+
}
2022
}

src/securenative.php

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,59 @@
1212

1313
class SecureNative
1414
{
15-
private static $apiKey;
16-
private static $options;
17-
private static $eventManager;
18-
private static $middleware;
15+
private static $apiKey;
16+
private static $options;
17+
private static $eventManager;
18+
private static $middleware;
1919

20-
public static function init($apiKey, SecureNativeOptions $secureNativeOptions)
21-
{
22-
if ($apiKey == '') {
23-
throw new Exception('You must pass your SecureNative api key');
24-
}
20+
public static function init($apiKey, SecureNativeOptions $secureNativeOptions)
21+
{
22+
if ($apiKey == '') {
23+
throw new Exception('You must pass your SecureNative api key');
24+
}
2525

26-
self::$apiKey = $apiKey;
27-
self::$options = $secureNativeOptions;
28-
self::$eventManager = new EventManager($apiKey, self::$options);
29-
self::$middleware = new Middleware($apiKey);
30-
}
26+
self::$apiKey = $apiKey;
27+
self::$options = $secureNativeOptions;
28+
self::$eventManager = new EventManager($apiKey, self::$options);
29+
self::$middleware = new Middleware($apiKey);
30+
}
31+
32+
public static function track(Array $attributes)
33+
{
34+
$opts = new EventOptions(json_encode($attributes));
35+
if (count($opts->params) > MAX_CUSTOM_PARAMS) {
36+
throw new Exception(sprintf('You can only specify maximum of %d params', MAX_CUSTOM_PARAMS));
37+
}
3138

32-
public static function track(Array $attributes)
33-
{
34-
$opts = new EventOptions(json_encode($attributes));
35-
if (count($opts->params) > MAX_CUSTOM_PARAMS) {
36-
throw new Exception(sprintf('You can only specify maximum of %d params', MAX_CUSTOM_PARAMS));
39+
$requestUrl = sprintf('%s/track', self::$options->getApiUrl());
40+
$event = self::$eventManager->buildEvent($opts);
41+
self::$eventManager->sendAsync($event, $requestUrl);
3742
}
3843

39-
$requestUrl = sprintf('%s/track', self::$options->getApiUrl());
40-
$event = self::$eventManager->buildEvent($opts);
41-
self::$eventManager->sendAsync($event, $requestUrl);
42-
}
44+
public static function verify(Array $attributes)
45+
{
46+
$opts = new EventOptions(json_encode($attributes));
47+
$requestUrl = sprintf('%s/verify', self::$options->getApiUrl());
48+
$event = self::$eventManager->buildEvent($opts);
49+
$result = self::$eventManager->sendSync($event, $requestUrl);
50+
51+
if ($result == null) {
52+
return new VerifyResult();
53+
}
4354

44-
public static function verify(Array $attributes)
45-
{
46-
$opts = new EventOptions(json_encode($attributes));
47-
$requestUrl = sprintf('%s/verify', self::$options->getApiUrl());
48-
$event = self::$eventManager->buildEvent($opts);
49-
$result = self::$eventManager->sendSync($event, $requestUrl);
50-
51-
if($result == null){
52-
return new VerifyResult();
55+
return $result;
5356
}
5457

55-
return $result;
56-
}
58+
public static function flow($flowId, Array $attributes)
59+
{
60+
$opts = new EventOptions(json_encode($attributes));
61+
$requestUrl = sprintf('%s/flow/%s', self::$options->getApiUrl(), $flowId);
62+
$event = self::$eventManager->buildEvent($opts);
63+
return self::$eventManager->sendSync($event, $requestUrl);
64+
}
5765

58-
public static function flow($flowId, Array $attributes)
59-
{
60-
$opts = new EventOptions(json_encode($attributes));
61-
$requestUrl = sprintf('%s/flow/%s', self::$options->getApiUrl(), $flowId);
62-
$event = self::$eventManager->buildEvent($opts);
63-
return self::$eventManager->sendSync($event, $requestUrl);
64-
}
66+
public static function getMiddleware()
67+
{
68+
return self::$middleware;
69+
}
6570
}

0 commit comments

Comments
 (0)