1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\Api\ControllerProviderInterface; |
10
|
|
|
use RedBeanPHP\Facade as R; |
11
|
|
|
|
12
|
|
|
class SecurityController implements ControllerProviderInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
private $app; |
16
|
|
|
|
17
|
|
|
public function connect(Application $app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
$factory = $app['controllers_factory']; |
21
|
|
|
# il mount point e' precedente e non serve prima |
22
|
|
|
$this->app['db']; |
23
|
|
|
//R::fancyDebug( TRUE ); |
|
|
|
|
24
|
|
|
$factory->post('/login', array($this, 'login')); |
25
|
|
|
$factory->get('/logout', array($this, 'logout')); |
26
|
|
|
$factory->get('/confirm', array($this, 'confirm')); |
27
|
|
|
return $factory; |
28
|
|
|
} |
29
|
|
|
public function login(Request $request) |
30
|
|
|
{ |
31
|
|
|
/*TODO remove this line in producton DBG DATA {"authMode":"Email","email":"[email protected]","name":"ugo","surname":"ugo","password":"cane"}*/ |
|
|
|
|
32
|
|
|
$data = json_decode($request->getContent(), true); |
33
|
|
|
if ($data === NULL) { |
34
|
|
|
$headers = []; |
35
|
|
|
$response = JsonResponse::create($res, 403, $headers)->setSharedMaxAge(300); |
|
|
|
|
36
|
|
|
return $response; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$authMode = $data['authMode']; |
40
|
|
|
|
41
|
|
|
if ($authMode === 'Email') { |
42
|
|
|
$email = $data['email']; |
43
|
|
|
$password = $data['password']; |
44
|
|
|
$name = $data['name']; |
45
|
|
|
$surname = $data['surname']; |
46
|
|
|
$user = R::findOne('user', "WHERE email = ? AND name = ? AND surname = ?", [$email, $name, $surname]); |
47
|
|
|
if ($user->pwd === hash("sha256", $user->salt.$password)) { |
48
|
|
|
//LOGGED IN! |
49
|
|
|
$this->app['session']->set('user', ['id' => $user->id]); |
50
|
|
|
$headers = []; |
51
|
|
|
$res = [ |
52
|
|
|
"token"=>"blablabla", //TODO CREATE token |
53
|
|
|
"clientId"=>$user->id |
54
|
|
|
]; |
55
|
|
|
$response = JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
56
|
|
View Code Duplication |
}else { |
|
|
|
|
57
|
|
|
$headers = []; |
58
|
|
|
$res = [ |
59
|
|
|
"errore"=>"sbagliato password o user" //TODO roba |
60
|
|
|
]; |
61
|
|
|
$response = JsonResponse::create($res, 401, $headers)->setSharedMaxAge(300); |
62
|
|
|
} |
63
|
|
|
}else { |
|
|
|
|
64
|
|
|
//Facebook Redirect |
65
|
|
|
} |
66
|
|
|
return $response; // JsonResponse::create($output, 200, $headers)->setSharedMaxAge(300); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
public function logout(Request $request) |
69
|
|
|
{ |
70
|
|
|
$this->app['session']->clear(); |
71
|
|
|
$response = new Response(); |
72
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
73
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
74
|
|
|
$response->setSharedMaxAge(300); |
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
public function confirm(Request $request) |
78
|
|
|
{ |
79
|
|
|
$confirmKey = $request->request->get('confirmKey'); |
80
|
|
|
$verify = R::findOne('verify', "WHERE key = ?", [$confirmKey]); |
81
|
|
|
if (!$bean->id) { |
|
|
|
|
82
|
|
|
//TODO mettere un controllo agli IP che forzano le richieste di token falsi |
83
|
|
|
$response = "<html><head></head><body>Token non esistente!</body></html>"; |
84
|
|
|
}else { |
85
|
|
|
if (strtotime($verify->inserttime) < strtotime("-15 minutes")) { |
86
|
|
|
$user = R::load('user', $verify->user); |
87
|
|
|
$user->status = "enabled"; |
88
|
|
|
$user->updatetime = date('Y-m-d H:i:s'); |
89
|
|
|
$id = R::store($user); |
|
|
|
|
90
|
|
|
$response = "<html><head></head><body>Account attivato complimenti!</body></html>"; |
91
|
|
|
}else { |
92
|
|
|
$response = "<html><head></head><body>Impossibile attivare account inserire mail e password per richiedere un nuovo token!</body></html>"; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return $response; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.