Skip to content

Commit 2e6e7e6

Browse files
committed
Added middleware
1 parent 16faaaa commit 2e6e7e6

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ $sn->track(array(
4040
'id' => '12345'
4141
]
4242
));
43-
43+
44+
45+
middleware:
46+
47+
$body = file_get_contents('php://input');
48+
$sn->middleware->verifySignature($_SERVER, $body)
49+
4450
```

src/middleware.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
const SIGNATURE_KEY = 'x-securenative';
4+
5+
class Middleware
6+
{
7+
private $apiKey;
8+
9+
public function __construct($apiKey)
10+
{
11+
$this->apiKey = $apiKey;
12+
}
13+
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+
}
20+
}

src/securenative.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
require "event-manager.php";
6+
require "middleware.php";
67
require "Models/event-options.php";
78
require "Models/verify-result.php";
89
require "Enums/risk-levels.php";
@@ -14,6 +15,7 @@ class SecureNative
1415
private static $apiKey;
1516
private static $options;
1617
private static $eventManager;
18+
private static $middleware;
1719

1820
public static function init($apiKey, SecureNativeOptions $secureNativeOptions)
1921
{
@@ -24,6 +26,7 @@ public static function init($apiKey, SecureNativeOptions $secureNativeOptions)
2426
self::$apiKey = $apiKey;
2527
self::$options = $secureNativeOptions;
2628
self::$eventManager = new EventManager($apiKey, self::$options);
29+
self::$middleware = new Middleware($apiKey);
2730
}
2831

2932
public static function track(Array $attributes)

0 commit comments

Comments
 (0)