Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
151 lines (128 loc) · 4.8 KB
/
Copy pathindex.php
File metadata and controls
151 lines (128 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Step 0: Required frameworks and basic setup
*
*/
// Add our Env Vars - my free-hosting doesn't allow .httaccess SetEnv so I use a .php file
require '.env.php';
// HttpException
require 'http_exception.php';
// Route handler
require 'router.php';
// JWT encode e decode methods
require 'jwt_helper.php';
// Basic ORM
require 'query_builder.php';
// Turn PHP error report on screen on-off
error_reporting(getenv('PHP_ERROR_REPORTING'));
ini_set('display_errors', getenv('PHP_DISPLAY_ERROS'));
// Setting the correct date & time, used in all php timestamps
date_default_timezone_set(getenv('APP_TIME_ZONE'));
/**
* Step 1: Add the helper classes and route implementation
*
*/
// PHP file with some data models customizations - used by our ORM
//include 'models.php';
/**
* Step 2: Helper class with methods required by the application.
*
*/
include 'helper.php';
/**
* Step 3: Instantiate our Router application and add routes to it.
*
*/
try {
$router = new Router;
$jwt_helper = new JWTHelper;
// JWT Helper needs a factory to retrieve the tokens
$jwt_helper->registerTokenFactory(function($kid) use ($router) {
// Retriving route details from database
$qb = new QueryBuilder();
$tokens = $qb
->table('tokens')
->fields(['secret'])
->where(["id = '$kid'"])
->select();
// Each user must have just one token
if (count($tokens->values) != 1)
throw new HttpException(400, "Error finding the user`s token");
return $tokens->values[0]->secret;
});
// A middlware to perform JwtAuthorization
$router->addMiddleware('JwtAuthorization', function() use ($router, $jwt_helper) {
// 'auth' URL use Basic HTTP Auth, so let's skip
$uri = $router->uri();
if ($uri == '/auth' || $uri == '/cron') return;
$headers = null;
if (isset($_SERVER['Authorization'])) {
$headers = trim($_SERVER["Authorization"]);
}
else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
} elseif (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
// Server-side fix for bug in old Android versions (a nice
// side-effect of this fix means we don't care about capitalization
// for Authorization)
$requestHeaders = array_combine(
array_map('ucwords',
array_keys($requestHeaders)),
array_values($requestHeaders));
if (isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
}
// HEADER: Get the access token from the header
$token = null;
if (!empty($headers)) {
if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
$token = $matches[1];
}
}
if ($token == null) throw new HttpException(401, "No JWT token found");
// Decode JWT token here
try {
$jwt = $jwt_helper->decode($token, null, true);
} catch (Exception $e) {
throw new HttpException(401, $e->getMessage());
}
// Parameters to search for the current route in the DB
$uriPattern = $router->uriPattern();
$uriMethod = strtoupper($router->method());
// Retrieve route details from database
$qb = new QueryBuilder();
$routes = $qb
->table('routes')
->fields(['id'])
->where(["route = '$uriPattern' AND ", "verb = '$uriMethod'"])
->select();
// The pattern should match only one route
if (count($routes->values) != 1)
throw new HttpException(400, "Error finding the route parameters");
$route = $routes->values[0];
// Checking the route against the list of allowed routes
if ( ! in_array($route->id, $jwt->allowed_routes )) {
// user doesn't have access to it
throw new HttpException(403, 'You are not allowed to call this route');
}
});
require 'routes/routes_devices.php';
require 'routes/routes_sensors.php';
require 'routes/routes_data.php';
require 'routes/routes_users.php';
require 'routes/routes_authorization.php';
/**
* Step 4: Run the application.
*
* This method should be called last. This executes the Router application
* and returns the HTTP response to the HTTP client.
*/
// execute the received method and output the result
echo $router($router->method(), $router->uri());
}
catch (HttpException $e) {
// checking for application exceptions/errors and output it as result
$e->getResponse();
}