@@ -43,7 +43,9 @@ function __construct(Dispatcher $events)
43
43
public function store ($ version , $ deviceToken , $ websitePushId , Request $ request )
44
44
{
45
45
// Decrypt our user info.
46
- $ userInfo = decrypt ($ this ->extractAuthenticationToken ($ request ));
46
+ $ userInfo = $ this ->extractUserInfo (
47
+ $ this ->extractAuthenticationToken ($ request )
48
+ );
47
49
48
50
$ this ->events ->dispatch (new WebPushSubscribed ($ version , $ deviceToken , $ websitePushId , $ userInfo ));
49
51
@@ -64,14 +66,43 @@ public function store($version, $deviceToken, $websitePushId, Request $request)
64
66
public function destroy ($ version , $ deviceToken , $ websitePushId , Request $ request )
65
67
{
66
68
// Decrypt our user info.
67
- $ userInfo = decrypt ($ this ->extractAuthenticationToken ($ request ));
69
+ $ userInfo = $ this ->extractUserInfo (
70
+ $ this ->extractAuthenticationToken ($ request )
71
+ );
68
72
69
73
$ this ->events ->dispatch (new WebPushUnsubscribed ($ version , $ deviceToken , $ websitePushId , $ userInfo ));
70
74
71
75
// Return with an empty OK response.
72
76
return response ('' );
73
77
}
74
78
79
+ /**
80
+ * Extract the user information from auth token.
81
+ *
82
+ * @param string $authenticationToken
83
+ * @return array
84
+ */
85
+ private function extractUserInfo (string $ authenticationToken ): array
86
+ {
87
+ try {
88
+ $ userInfo = json_decode (
89
+ decrypt ($ authenticationToken ),
90
+ true /* extract as assoc array */
91
+ );
92
+
93
+ if ($ userInfo === null ) {
94
+ throw new Exception ('UserInfo is not in a valid JSON format. ' );
95
+ }
96
+
97
+ // Return successful decoded user info.
98
+ return $ userInfo ;
99
+ } catch (Exception $ exc ) {
100
+ // Return the plain auth token in error case. This could be, because
101
+ // the user decided to handle the token resolver by himself.
102
+ return $ authenticationToken ;
103
+ }
104
+ }
105
+
75
106
/**
76
107
* Extract the authentication header token.
77
108
*
0 commit comments