BackPassport.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Data\ResultWrapper;
  4. use KIF\Verify;
  5. use KIF\Core\Config;
  6. use KIF\Exception\ParamsException;
  7. use KIF\Core\Request;
  8. use Cas\Dao\BackUser;
  9. /**
  10. *
  11. * 后台管理通行证
  12. * @author lishumingoo@gmail.com
  13. */
  14. class BackPassport {
  15. /**
  16. *
  17. * 安全key
  18. * @var string
  19. */
  20. private $securityKey;
  21. private $login_cookie_name = 'BACK_PASSPORT_MEMBER';
  22. public function __construct() {
  23. $PASSPORT_SIGN_KEY = Config::getInstance()->get('passport_sign_key');
  24. if (!$PASSPORT_SIGN_KEY) {
  25. throw new ParamsException('请指定securityKey');
  26. }
  27. $this->securityKey = $PASSPORT_SIGN_KEY;
  28. }
  29. /**
  30. *
  31. * 登录
  32. * @param string $name 用户名
  33. * @param string $password 密码
  34. * @param int | null $expire 登录有效期 值为null时:表示该cookie为当前会话期有效
  35. * @return ResultWrapper
  36. */
  37. public function login($name, $password, $project, $expire = null) {
  38. $objBackUser = new BackUser();
  39. $tmpResult = $objBackUser->getByNameAndPassword($name, $password);
  40. if (!$tmpResult->isSuccess()) {
  41. return $tmpResult;
  42. }
  43. $user = $tmpResult->getData();
  44. if ($user['permission'] != 'admin' && $user['project'] != $project) {
  45. return ResultWrapper::fail('无权管理此项目');
  46. }
  47. $loginInfo = array(
  48. 'uid' => $user['uid'],
  49. 'name' => $user['name'],
  50. 'expire' => $expire,
  51. );
  52. return $this->writeLoginCookie($loginInfo);
  53. }
  54. /**
  55. *
  56. * 推出登录
  57. */
  58. public function logout() {
  59. setcookie($this->login_cookie_name, '', 0, '/', $_SERVER['HTTP_HOST']);
  60. }
  61. /**
  62. *
  63. * 注册
  64. * @param array $info
  65. * @return InternalResultTransfer
  66. */
  67. public function register(array $info) {
  68. if (isset($info['expire'])) {
  69. $expire = $info['expire'];
  70. if (!Verify::unsignedInt($expire)) {
  71. $expire = null;
  72. }
  73. unset($info['expire']);
  74. } else {
  75. $expire = null;
  76. }
  77. $objBackUser = new BackUser();
  78. $tmpResult = $objBackUser->add($info);
  79. if (!$tmpResult->isSuccess()) {
  80. return $tmpResult;
  81. }
  82. $user = $tmpResult->getData();
  83. $loginInfo = array(
  84. 'uid' => $user['id'],
  85. 'uname' => $user['name'],
  86. 'expire' => $expire,
  87. );
  88. $tmpResult = $this->writeLoginCookie($loginInfo);
  89. if (!$tmpResult->isSuccess()) {
  90. return $tmpResult;
  91. }
  92. return ResultWrapper::success($user);
  93. }
  94. /**
  95. *
  96. * 获取登录用户信息
  97. * @return ResultWrapper 成功:存在登录cookie里的信息作为数组返回;失败:失败原因
  98. */
  99. public function getLoginInfo() {
  100. if (!isset($_COOKIE[$this->login_cookie_name])) {
  101. return ResultWrapper::fail('没有登录cookie');
  102. }
  103. $loginCookie = $_COOKIE[$this->login_cookie_name];
  104. $loginInfo = array();
  105. parse_str($loginCookie, $loginInfo);
  106. $tmpResult = $this->verifySign($loginInfo);
  107. if (!$tmpResult->isSuccess()) {
  108. return $tmpResult;
  109. }
  110. return ResultWrapper::success($loginInfo);
  111. }
  112. /**
  113. *
  114. * 写登录信息到cookie
  115. * @param array $info
  116. * @return ResultWrapper
  117. */
  118. public function writeLoginCookie(array $info) {
  119. if (is_null($info['expire'])) {// 说明这是个当前会话期有效的登录用户
  120. $info['expire'] = time() + 12 * 60 * 60;//登录cookie签名的有效性,设为4小时
  121. $cookie_expire = null;//让登录cookie在当前会话期有效
  122. } else {
  123. # 让登录cookie的存在期比 登录cookie签名有效性多1天。避免用户电脑时间与服务器时间不一致,导致签名在有效期,但cookie却已失效的情况。
  124. $cookie_expire = $info['expire'] + 24 * 60 * 60;
  125. }
  126. if (is_null($info['timestamp'])) {//
  127. $info['timestamp'] = time();
  128. }
  129. # 版本号,为以后安全性升级做准备
  130. $info['version'] = '0.2';
  131. $tmpResult = $this->sign($info);
  132. if (!$tmpResult->isSuccess()) {//签名失败
  133. return $tmpResult;
  134. }
  135. $info['sign'] = $tmpResult->getData();
  136. setcookie($this->login_cookie_name, http_build_query($info), $cookie_expire, '/', $_SERVER['HTTP_HOST']);
  137. return ResultWrapper::success();
  138. }
  139. /**
  140. *
  141. * 对 $info 做签名
  142. * @param array $info 格式:array (
  143. * // 这些是必要字段
  144. * 'uid' => (int),//用户id
  145. * 'uname' => (string),//用户名
  146. * 'expire' => (int),//签名有效期,过了这个有效期,即使签名正确,也不认。
  147. * 'register_type' => (int),//用户注册类型
  148. * )
  149. * @return ResultWrapper 成功:返回签名$sign;失败:返回失败原因
  150. */
  151. public function sign(array $info) {
  152. if (!isset($info['uid']) || !Verify::int($info['uid'])) {
  153. return ResultWrapper::fail('无效的uid');
  154. }
  155. if (!isset($info['expire']) || !Verify::int($info['expire'])) {
  156. return ResultWrapper::fail('无效的expire');
  157. }
  158. if (!isset($info['name']) || !is_string($info['name']) || empty($info['name'])) {
  159. return ResultWrapper::fail('无效的name');
  160. }
  161. ksort($info);
  162. $tmpStr = '';
  163. foreach ($info as $k => $v) {
  164. if ($v != '' && $k != 'sign') {
  165. $tmpStr .= "{$k}={$v}&";
  166. }
  167. }
  168. $tmpStr .= "securityKey={$this->securityKey}";
  169. $sign = strtolower(sha1($tmpStr));
  170. return ResultWrapper::success($sign);
  171. }
  172. /**
  173. *
  174. * 验证 $info 的有效性
  175. * @param array $info
  176. * @return ResultWrapper
  177. */
  178. public function verifySign(array $info) {
  179. if (!isset($info['sign'])) {
  180. return ResultWrapper::fail('没有指定sign值,无法校验');
  181. }
  182. $tmpResult = $this->sign($info);
  183. if (!$tmpResult->isSuccess()) {
  184. return $tmpResult;
  185. }
  186. $sign = $tmpResult->getData();
  187. if ($sign != $info['sign']) {
  188. return ResultWrapper::fail("给定的sign:{$info['sign']} 与 计算后的sign:{$sign} 不等");
  189. }
  190. if ($info['expire'] < time()) {
  191. return ResultWrapper::fail('expire存储的签名有效期已过');
  192. }
  193. return ResultWrapper::success();
  194. }
  195. }