|
1
|
|
|
<?php |
|
2
|
|
|
class ProfilesAPI extends Http\Rest\RestAPI |
|
3
|
|
|
{ |
|
4
|
|
|
public function setup($app) |
|
5
|
|
|
{ |
|
6
|
|
|
$app->map(['OPTIONS'], '/login[/]', array($this, 'options')); |
|
7
|
|
|
$app->post('/login[/]', array($this, 'login')); |
|
8
|
|
|
$app->post('/logout[/]', array($this, 'logout')); |
|
9
|
|
|
$app->post('/zip[/]', array($this, 'validateZip')); |
|
10
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function options($request, $response) |
|
14
|
|
|
{ |
|
15
|
|
|
return $response->withJson(true); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function login($request, $response) |
|
19
|
|
|
{ |
|
20
|
|
|
$params = $request->getParams(); |
|
21
|
|
|
if(!isset($params['username']) || !isset($params['password'])) |
|
22
|
|
|
{ |
|
23
|
|
|
return $response->withStatus(400); |
|
24
|
|
|
} |
|
25
|
|
|
$auth = AuthProvider::getInstance(); |
|
26
|
|
|
$res = $auth->login($params['username'], $params['password']); |
|
27
|
|
|
if($res === false) |
|
28
|
|
|
{ |
|
29
|
|
|
return $response->withStatus(403); |
|
30
|
|
|
} |
|
31
|
|
|
else |
|
32
|
|
|
{ |
|
33
|
|
|
$user = \FlipSession::getUser(); |
|
34
|
|
|
$privateKey = file_get_contents('/var/www/secure_settings/jwtRS256.key'); |
|
35
|
|
|
$groups = $user->getGroups(); |
|
36
|
|
|
if($groups === false) |
|
37
|
|
|
{ |
|
38
|
|
|
$groups = array(); |
|
39
|
|
|
} |
|
40
|
|
|
$count = count($groups); |
|
41
|
|
View Code Duplication |
for($i = 0; $i < $count; $i++) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$groups[$i] = $groups[$i]->getGroupName(); |
|
44
|
|
|
} |
|
45
|
|
|
$token = array( |
|
46
|
|
|
'iss' => $request->getUri()->getHost(), |
|
47
|
|
|
'sub' => $user->uid, |
|
48
|
|
|
'private' => array('Flipside'=>array( |
|
49
|
|
|
'email'=>$user->mail, |
|
50
|
|
|
'groups'=>$groups, |
|
51
|
|
|
'sessionIDs'=> array( |
|
52
|
|
|
'php'=>session_id() |
|
53
|
|
|
) |
|
54
|
|
|
) |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
$cookieParams = session_get_cookie_params(); |
|
58
|
|
|
$jwt = \Firebase\JWT\JWT::encode($token, $privateKey, 'RS256'); |
|
59
|
|
|
$response = $response->withHeader('Set-Cookie', 'Flipside_JWT='.$jwt.'; path=/; domain='.$cookieParams['domain'].'; secure'); |
|
60
|
|
|
return $response->withJson($res); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function logout($request, $response) |
|
65
|
|
|
{ |
|
66
|
|
|
FlipSession::end(); |
|
67
|
|
|
$cookieParams = session_get_cookie_params(); |
|
68
|
|
|
$response = $response->withHeader('Set-Cookie', 'Flipside_JWT=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain='.$cookieParams['domain'].'; secure'); |
|
69
|
|
|
return $response->withJson(true); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function validateZip($request, $response, $args) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$obj = $request->getQueryParams(); |
|
75
|
|
|
if(empty($obj)) |
|
76
|
|
|
{ |
|
77
|
|
|
$obj = (array)$request->getParsedBody(); |
|
78
|
|
|
} |
|
79
|
|
|
$ret = false; |
|
|
|
|
|
|
80
|
|
|
if($obj['c'] == 'US') |
|
81
|
|
|
{ |
|
82
|
|
|
if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode'])) |
|
83
|
|
|
{ |
|
84
|
|
|
$contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']); |
|
85
|
|
|
$resp = json_decode($contents); |
|
86
|
|
|
if(isset($resp->error)) |
|
87
|
|
|
{ |
|
88
|
|
|
$ret = $resp->error; |
|
89
|
|
|
} |
|
90
|
|
|
else |
|
91
|
|
|
{ |
|
92
|
|
|
$ret = true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
else |
|
96
|
|
|
{ |
|
97
|
|
|
$ret = 'Invalid Zip Code!'; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
else |
|
101
|
|
|
{ |
|
102
|
|
|
$ret = true; |
|
103
|
|
|
} |
|
104
|
|
|
return $response->withJson($ret); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
108
|
|
|
|
|
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.