-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-secure.php
More file actions
127 lines (92 loc) · 3.59 KB
/
api-secure.php
File metadata and controls
127 lines (92 loc) · 3.59 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
<?php
//Sort of an index page for the api
/*
TODO:
- Change and migrate the secret key into a secure place. (Enviromental variable?)
- Change the mysql users to secure place. (Env too?)
- Make sure time works when servers are independant of each other.
- Test and fill in mysql.
*/
//URL SAFE B64 encode and decode
//https://gist.github.com/nathggns/6652997
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
//HEADERS
//https://developer.okta.com/blog/2019/03/08/simple-rest-api-php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//Grab our token
$token_full = $_GET['token'];
$token_full = filter_var ($token_full, FILTER_SANITIZE_EMAIL); //D0n't trust Users
$headers = apache_request_headers(); //left in here if tokens over headers is wanted > best use is in Authorization: Bearer
//Make sure our token is formated correctly and part it out to be used later
$token_separate = explode(".", $token_full);
if(sizeof($token_separate) != 3) {
header("HTTP/1.1 401 Unauthorized");
exit('Bad Token');
}
$token_payload_to_utf8 = utf8_encode(base64url_decode($token_separate[1]));
$token_payload_to_utf8 = (string) $token_payload_to_utf8;
$json_payload = json_decode($token_payload_to_utf8, true);
if(time() > (int)$json_payload['exp'] || !array_key_exists('exp', $json_payload)) { //this will be a problem when time is > than 32 bit
header("HTTP/1.1 401 Unauthorized");
exit('Invalid Token');
}
//COMPARE TOKENS AND MAKE SURE WE ARE NOT BEING SCAMMED
$token_unsigned = $token_separate[0] . '.' . $token_separate[1];
$secret = 'yTAnNB06TxQI0aEIc3y8l19k1i5zeKJYaxyDkILfpqqMk0ojQyfbAO9wlPQW4HU2'; //64 letter secret
$token_real_signature = hash_hmac('sha256', $token_unsigned, $secret, true);
if(base64url_encode($token_real_signature) !== $token_separate[2]) {
header("HTTP/1.1 401 Unauthorized");
exit('Unauthorized');
}
//Now we can do api here
//Connect to the mysql database
$link = mysqli_connect("localhost", "mysql_user", "mysql_password", "mysql_database");
if (mysqli_connect_errno()) {
http_response_code(500);
exit('Database Failure');
}
mysqli_set_charset($link, "utf8")
$requestMethod = $_SERVER["REQUEST_METHOD"]; //Useable, for GET, POST, PUT, DELETE
switch ($requestMethod) {
case 'GET':
//POSSIBLE GET PARAMETERS [id] //Sample ID
$get_parameters = array();
$get_parameters["id"] = $_GET["id"]; // NUll if does not exist
//Check to make sure that data was actually given
$i = 0;
foreach ($get_parameters as $v) {
if($v == NULL) {
$i += 1;
}
}
if($i == count($get_parameters)) {
echo '{}';
break; //no need to continue
}
//id select.
if($get_parameters["id"] !== NULL) {
$id = $get_parameters["id"];
$id = mysqli_real_escape_string($link, $id); //Again don't trust user input.
mysqli_query($link, 'SELECT * FROM Users WHERE id=' . $id . ';');
}
break;
case 'POST':
break;
case 'PUT':
break;
case 'DELETE':
break;
default:
break;
}
mysqli_close($link);
?>