|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Temitope Olotin <[email protected]> |
|
4
|
|
|
* @license <https://opensource.org/license/MIT> MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Laztopaz\EmojiRestfulAPI; |
|
7
|
|
|
|
|
8
|
|
|
use Firebase\JWT\JWT; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
12
|
|
|
use Laztopaz\EmojiRestfulAPI\UserController; |
|
13
|
|
|
|
|
14
|
|
|
class Oauth |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This method register a new user |
|
19
|
|
|
* |
|
20
|
|
|
* @param $request |
|
21
|
|
|
* @param $response |
|
22
|
|
|
* |
|
23
|
|
|
* @return json response |
|
24
|
|
|
*/ |
|
25
|
|
|
public function registerUser(Request $request, Response $response) |
|
26
|
|
|
{ |
|
27
|
|
|
$userParams = $request->getParsedBody(); |
|
28
|
|
|
|
|
29
|
|
|
if (is_array($userParams)) { |
|
30
|
|
|
$user = new UserController(); |
|
31
|
|
|
|
|
32
|
|
|
if (! $this->verifyUserRegistration($userParams['username'], $userParams['email'])) { |
|
33
|
|
|
$boolResponse = $user->createUser([ |
|
34
|
|
|
'firstname' => $userParams['firstname'], |
|
35
|
|
|
'lastname' => $userParams['lastname'], |
|
36
|
|
|
'username' => $userParams['username'], |
|
37
|
|
|
'password' => $userParams['password'], |
|
38
|
|
|
'email' => $userParams['email'], |
|
39
|
|
|
'created_at' => date('Y-m-d h:i:s'), |
|
40
|
|
|
'updated_at' => date('Y-m-d h:i:s') |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
if ($boolResponse) { |
|
44
|
|
|
return $response->withJson(['message' => 'User successfully created'], 200); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $response->withJson(['message' => 'User not created'], 400); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $response->withJson(['message' => 'User already exists'], 400); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* This method authenticate the user and log them in if the supplied |
|
57
|
|
|
* credentials are valid. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $loginParams |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @return json jwt |
|
62
|
|
|
*/ |
|
63
|
|
|
public function loginUser(Request $request, Response $response) |
|
64
|
|
|
{ |
|
65
|
|
|
$loginParams = $request->getParsedBody(); |
|
66
|
|
|
|
|
67
|
|
|
if (is_array($loginParams)) { |
|
68
|
|
|
$user = User::where('username', '=', $loginParams['username'])->get(); |
|
69
|
|
|
$user = $user->first(); |
|
70
|
|
|
|
|
71
|
|
|
$userInfo = ['id' => $user->id, 'username' => $user->username, 'email' => $user->email]; |
|
72
|
|
|
|
|
73
|
|
|
if (password_verify($loginParams['password'], $user->password)) { |
|
74
|
|
|
$token = $this->buildAcessToken($userInfo); |
|
75
|
|
|
|
|
76
|
|
|
return $response->withAddedHeader('HTTP_AUTHORIZATION', $token)->withStatus(200)->write($token); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $response->withJson(['status'], 400); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This method logout the user. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $args logout |
|
87
|
|
|
* |
|
88
|
|
|
* @return $reponse |
|
|
|
|
|
|
89
|
|
|
*/ |
|
90
|
|
|
public function logoutUser(Request $request, Response $response, $args) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
return $response->withJson(['message' => 'Logout successful'], 200); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* This method verifies a registered user |
|
97
|
|
|
* |
|
98
|
|
|
* @param $email |
|
99
|
|
|
* @param $username |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean true |
|
102
|
|
|
*/ |
|
103
|
|
|
public function verifyUserRegistration($username, $email) |
|
104
|
|
|
{ |
|
105
|
|
|
if (isset($username) && isset($email)) { |
|
106
|
|
|
$userFound = Capsule::table('users') |
|
107
|
|
|
->where('username', '=', strtoupper($username)) |
|
108
|
|
|
->orWhere('username', '=', strtolower($username)) |
|
109
|
|
|
->orWhere('username', '=', ucwords($username)) |
|
110
|
|
|
->where('email', '=', strtoupper($email)) |
|
111
|
|
|
->orWhere('email', '=', strtolower($email)) |
|
112
|
|
|
->orWhere('email', '=', ucwords($email)) |
|
113
|
|
|
->get(); |
|
114
|
|
|
|
|
115
|
|
|
if (count($userFound) > 0) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* This method builds an access token for a login user;. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $userData |
|
127
|
|
|
* |
|
128
|
|
|
* @return string $token |
|
129
|
|
|
*/ |
|
130
|
|
|
public function buildAcessToken(array $userData) |
|
131
|
|
|
{ |
|
132
|
|
|
$tokenId = base64_encode(mcrypt_create_iv(32)); |
|
133
|
|
|
$issuedAt = time(); |
|
134
|
|
|
$notBefore = $issuedAt; |
|
135
|
|
|
$expire = $notBefore + (float) strtotime('+30 days'); // Adding 30 days expiry date |
|
136
|
|
|
$serverName = 'http://sweatemoji.com/api'; // Retrieve the server name |
|
137
|
|
|
|
|
138
|
|
|
/* |
|
139
|
|
|
* |
|
140
|
|
|
* Create the token params as an array |
|
141
|
|
|
*/ |
|
142
|
|
|
$data = [ |
|
143
|
|
|
'iat' => $issuedAt, // Issued at: time when the token was generated |
|
144
|
|
|
'jti' => $tokenId, // Json Token Id: an unique identifier for the token |
|
145
|
|
|
'iss' => $serverName, // Issuer |
|
146
|
|
|
'nbf' => $notBefore, // Not before |
|
147
|
|
|
'exp' => $expire, // Expire |
|
148
|
|
|
|
|
149
|
|
|
'dat' => $userData // User Information retrieved from the database |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
$loadEnv = DatabaseConnection::loadEnv(); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
$secretKey = base64_decode(getenv('secret')); |
|
155
|
|
|
|
|
156
|
|
|
$jwt = JWT::encode( |
|
157
|
|
|
$data, //Data to be encoded in the JWT |
|
158
|
|
|
$secretKey, // The signing key |
|
159
|
|
|
'HS512' // Algorithm used to sign the token |
|
160
|
|
|
); |
|
161
|
|
|
$unencodedArray = ['jwt' => $jwt]; |
|
162
|
|
|
|
|
163
|
|
|
return json_encode($unencodedArray); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.