7
7
/**
8
8
* Include required sources from composer.
9
9
*/
10
- require __DIR__ . '/../vendor/autoload.php ' ;
10
+
11
+ require __DIR__ . '/vendor/autoload.php ' ;
11
12
12
13
/**
13
14
* Create simple router to check the request url.
14
15
*/
16
+
15
17
$ router = new \Bramus \Router \Router ();
16
18
17
19
/**
18
20
* This to validate secure requests and set user permissions.
19
21
*/
20
- $ router ->before ('GET|POST ' ,
22
+
23
+ $ router ->before ('GET|POST|DELETE ' ,
21
24
'(log.*|save.*|imgs.*|google.*|profiles.*|comment.*) ' ,
22
25
function ()
23
26
{
@@ -33,52 +36,57 @@ function ()
33
36
$ authorizationHeader = isset ($ requestHeaders ['Authorization ' ]) ? $ requestHeaders ['Authorization ' ] : null ;
34
37
35
38
if ($ authorizationHeader == null ) {
36
- header ('HTTP/1.0 401 Unauthorized ' );
37
-
38
- /**
39
- * No authorization header sent.
40
- */
41
-
42
39
$ events ['auth0 ' ]['method ' ] = 'secure ' ;
43
40
$ events ['auth0 ' ]['authorized ' ] = false ;
44
41
$ events ['auth0 ' ]['api ' ] = true ;
45
42
$ events ['auth0 ' ]['user ' ] = false ;
46
43
$ events ['auth0 ' ]['message ' ] = 'No authorization header sent. ' ;
47
44
48
- echo json_encode (
49
- array (
50
- 'events ' => $ events
51
- ));
45
+ /**
46
+ * No authorization header sent.
47
+ * Allow to GET comments as a public service.
48
+ * POST and DELETE are restricted only to authorized users.
49
+ */
52
50
53
- exit ();
51
+ if ($ _SERVER ['REQUEST_METHOD ' ] != 'GET ' &&
52
+ ! strpos ($ requestUri , 'comment.php ' )) {
53
+
54
+ header ('HTTP/1.0 401 Unauthorized ' );
55
+
56
+ echo json_encode (
57
+ array (
58
+ 'events ' => $ events
59
+ ));
60
+
61
+ exit ();
62
+ } else {
63
+ return ;
64
+ }
54
65
}
55
66
56
67
/**
57
68
* Validate token.
58
69
*/
59
70
60
71
$ token = str_replace ('Bearer ' , '' , $ authorizationHeader );
61
- $ secret = '<--!secret--> ' ;
62
- $ client = '<--!client--> ' ;
63
- $ domain = '<--!forplay.eu.auth0.com--> ' ;
64
- $ domainUrl = '<--!https://forplay.eu.auth0.com/--> ' ;
72
+ $ client = 'P8wrSYlMVUu5rZDEFGSqFL18tVfgo9Gz ' ;
73
+ $ domain = 'forplay.eu.auth0.com ' ;
74
+ $ domainUrl = 'https://forplay.eu.auth0.com/ ' ;
65
75
66
76
$ decodedToken = null ;
67
77
$ auth0Api = new Management ($ token , $ domain );
68
78
69
79
$ verifier = new JWTVerifier (
70
80
[
71
81
'suported_algs ' => [
72
- 'RS256 ' ,
73
- 'HS256 '
82
+ 'RS256 '
74
83
],
75
84
'valid_audiences ' => [
76
85
$ client
77
86
],
78
87
'authorized_iss ' => [
79
88
$ domainUrl
80
- ],
81
- 'client_secret ' => $ secret
89
+ ]
82
90
]);
83
91
84
92
try {
@@ -160,7 +168,8 @@ function ()
160
168
/**
161
169
* These is the public API to get Forplay content.
162
170
*/
163
- $ router ->match ('POST|GET ' , '(tags.*|search.*|forplay.*|sitemap.*) ' ,
171
+
172
+ $ router ->match ('GET|POST|DELETE ' , '(tags.*|search.*|forplay.*|sitemap.*) ' ,
164
173
function ()
165
174
{
166
175
global $ events ;
@@ -174,7 +183,9 @@ function ()
174
183
/**
175
184
* These is the private API save Forplay content and see the log.
176
185
*/
177
- $ router ->match ('POST|GET ' , '(log.*|save.*|imgs.*|google.*|profile.*|comment.*) ' ,
186
+
187
+ $ router ->match ('GET|POST|DELETE ' ,
188
+ '(log.*|save.*|imgs.*|google.*|profile.*|comment.*) ' ,
178
189
function ()
179
190
{
180
191
global $ events ;
@@ -188,6 +199,7 @@ function ()
188
199
/**
189
200
* If someone tries to access unknown API.
190
201
*/
202
+
191
203
$ router ->set404 (
192
204
function ()
193
205
{
@@ -204,5 +216,6 @@ function ()
204
216
/**
205
217
* Run the router.
206
218
*/
219
+
207
220
$ router ->run ();
208
221
?>
0 commit comments