-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsAuth.php
114 lines (89 loc) · 2.09 KB
/
wsAuth.php
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
<?php
class wsAuth
{
private $key = "wsAuth";
private $aToken = array();
private static $_instance;
private function __construct () {}
private function __clone () {}
public static function getInstance () {
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function APIlogin($sUser, $sPassword)
{
if($sUser == '' || $sPassword == '')
{
throw new Exception('Bad arguments', 7);
}
$oSite = accessSite::verifyLogin($sUser);
if(! $oSite instanceof Site)
{
throw new Exception('unknown user', 1);
}
if(!$oSite->isValidPassword($sPassword))
{
throw new Exception('wrong password', 2);
}
$aToken = array(
"iss" => sfContext::getInstance()->getRequest()->getHost(),
"aud" => $oSite->getIdSite());
return $this->getToken($aToken);
}
public function userLogin($sUser, $sPassword)
{
$oAccount = user::checkLoginPass($sUser, $sPassword);
if($oAccount)
{
$this->aToken["qiduser"] = $oAccount->id_global_account;
}
else
{
$oAccount = user::checkLogin($sUser);
if($oAccount)
{
throw new Exception('wrong password', 2);
}
else
{
throw new Exception('unknown user', 1);
}
}
return $this->getToken($this->aToken);
}
private function getToken($aToken = null)
{
$aToken["exp"] = time()+(30*60);
$aToken["iat"] = time();
$this->aToken = $aToken;
return JWT::encode($aToken, $this->key);
}
public function checkToken($sTokenEnc)
{
return JWT::decode($sTokenEnc, $this->key);
}
public function checkApiAuth($sToken)
{
$aToken = (array)$this->checkToken($sToken);
if(isset($aToken["aud"]))
{
return array('sToken' => $this->getToken($aToken), 'aToken' => $aToken);
}
else
{
throw new Exception('Bad token');
}
}
public function checkUserAuth($sToken)
{
if($sToken = $this->checkApiAuth($sToken))
{
return $sToken;
}
else
{
return false;
}
}
}