Skip to content

Commit c0dde63

Browse files
committed
Notification decode
1 parent 1e2d219 commit c0dde63

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/Fident.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Fident\Web;
33

4+
use Fident\Web\Notifications\FidentNotification;
45
use Fident\Web\UserData\FidentJwtData;
56
use Packaged\Helpers\Objects;
67

@@ -62,6 +63,18 @@ public function decodeJwtPayload(string $rawJwt): ?FidentJwtData
6263
return $data;
6364
}
6465

66+
public function decodeNotification($requestBody): ?FidentNotification
67+
{
68+
$notification = json_decode($requestBody);
69+
$data = Objects::property($notification, 'Data', '');
70+
$sig = self::urlsafeB64Decode(Objects::property($notification, 'Signature', ''));
71+
if(openssl_verify($data, $sig, $this->getConfig()->getPublicKey(), OPENSSL_ALGO_SHA256))
72+
{
73+
return FidentNotification::fromString($data);
74+
}
75+
return null;
76+
}
77+
6578
public static function urlsafeB64Decode($input)
6679
{
6780
$remainder = strlen($input) % 4;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace Fident\Web\Notifications;
3+
4+
use Fident\Web\UserData\UserAttribute;
5+
use Packaged\Helpers\Objects;
6+
7+
class FidentNotification
8+
{
9+
protected $_id;
10+
protected $_created;
11+
protected $_username;
12+
protected $_attributes;
13+
protected $_type;
14+
15+
const SUCCESS_RESPONSE = 'con';
16+
17+
public static function fromString($rawNotificationData)
18+
{
19+
$notification = new static();
20+
$notificationData = json_decode($rawNotificationData);
21+
$notification->_id = Objects::property($notificationData, 'ID');
22+
$notification->_created = Objects::property($notificationData, 'Created');
23+
$notification->_username = Objects::property($notificationData, 'Username');
24+
$notification->_type = Objects::property($notificationData, 'Type');
25+
$notification->_attributes = [];
26+
foreach(Objects::property($notificationData, 'Attributes', []) as $attr)
27+
{
28+
$notification->addAttribute(Objects::property($attr, 'Key'), Objects::property($attr, 'Value'));
29+
}
30+
return $notification;
31+
}
32+
33+
public function addAttribute($key, $value)
34+
{
35+
$this->_attributes[$key] = new UserAttribute($key, $value);
36+
return $this;
37+
}
38+
39+
/**
40+
* @return mixed
41+
*/
42+
public function getId()
43+
{
44+
return $this->_id;
45+
}
46+
47+
/**
48+
* @return mixed
49+
*/
50+
public function getCreated()
51+
{
52+
return $this->_created;
53+
}
54+
55+
/**
56+
* @return mixed
57+
*/
58+
public function getUsername()
59+
{
60+
return $this->_username;
61+
}
62+
63+
/**
64+
* @return UserAttribute[]
65+
*/
66+
public function getAttributes()
67+
{
68+
return $this->_attributes;
69+
}
70+
71+
/**
72+
* @return mixed
73+
*/
74+
public function getType()
75+
{
76+
return $this->_type;
77+
}
78+
79+
}

src/UserData/UserAttribute.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ class UserAttribute
77
protected $_key;
88
protected $_value;
99

10+
public function __construct($key = null, $value = null)
11+
{
12+
$this->_key = $key;
13+
$this->_value = $value;
14+
}
15+
1016
/**
1117
* @return mixed
1218
*/

0 commit comments

Comments
 (0)