123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- use kornrunner\Keccak;
- class Account extends Model {
- protected $tableName = 'user_info';
- protected $dbKey = 'dw_chat';
- const PRE_KEY_TOKEN = 'globals:token:';
- const PRE_KEY_RANDOM = 'globals:random_1:';
- const PRE_KEY_ACCESS_TOKEN = 'globals:access_token:';
- const TYPE_EOS = 'eos';
- const TYPE_ETH = 'eth';
- const TYPE_TRON = 'tron';
- const TYPE_TG = 'tg';
- const TYPE_MEETONE = 'meetone';
- const TTL_TOKEN = 86400;
- private static $arrType = [
- self::TYPE_EOS => 'EOS',
- self::TYPE_MEETONE => 'MEETONE',
- self::TYPE_TG => 'TG',
- self::TYPE_ETH => 'ETH',
- self::TYPE_TRON => 'TRON',
- ];
- public static function getAllType() {
- return self::$arrType;
- }
- public static function getTypeName($type) {
- return self::$arrType[$type];
- }
- public static function getTokenKey($token) {
- return self::PRE_KEY_TOKEN . $token;
- }
-
- public static function checkToken() {
- $objRedis = dwRedis::init('dw_chat');
- $token = $_REQUEST['token'];
- $user_id = $_REQUEST['user_id'];
- if ($token) {
- $tokenKey = self::getTokenKey($token);
- $value = $objRedis->get($tokenKey);
- if ($value) {
- if ($value == $user_id) {
- return true;
- }
- } else {
- self::clearCookie();
- }
- }
- return false;
- }
-
- public static function refreshToken() {
- $objRedis = dwRedis::init('dw_chat');
- $token = $_REQUEST['token'];
- $user_id = $_REQUEST['user_id'];
- if ($token) {
- $tokenKey = self::getTokenKey($token);
- $value = $objRedis->get($tokenKey);
- if ($value && $value == $user_id) {
- $objRedis->expire($tokenKey, self::TTL_TOKEN);
- return true;
- }
- }
- return false;
- }
-
- public static function genToken($user_id) {
- $token = uuid16();
- $tokenKey = self::getTokenKey($token);
- $objRedis = dwRedis::init();
- $objRedis->setex($tokenKey, self::TTL_TOKEN, $user_id);
- return $token;
- }
-
- public static function setCookie($user_id) {
- $token = Account::genToken($user_id);
-
- return compact('user_id', 'token');
- }
- public static function clearCookie() {
- $expire = time() - 86400;
- $path = '/';
-
- setcookie('token', null, $expire, $path, 'mee.chat');
- setcookie('user_id', null, $expire, $path, 'mee.chat');
- if (ENV == ENV_DEV) {
-
- setcookie('token', null, $expire, $path);
- setcookie('user_id', null, $expire, $path);
- }
- }
- public static function getRandomKey($account) {
- return self::PRE_KEY_RANDOM . $account;
- }
- private static function getAccessTokenKey($accessToken) {
- return self::PRE_KEY_ACCESS_TOKEN . $accessToken;
- }
-
- private static function refreshAccessToken($random, $objRedis = null) {
- !$objRedis && $objRedis = dwRedis::init();
- $accessToken = uuid();
- $key = self::getAccessTokenKey($accessToken);
- $objRedis->setex($key, 900, $random);
- return $accessToken;
- }
-
- public static function getRandom($account, $withToken = false) {
- $objRedis = dwRedis::init();
- $randomKey = self::getRandomKey($account);
- $random = $objRedis->get($randomKey);
- if (!$random) {
- $random = date('H:i:s ', time() - 8 * 3600) . rand(0, 999);
- }
- $objRedis->setex($randomKey, 900, $random);
- $accessToken = self::refreshAccessToken($random, $objRedis);
- return !$withToken ? $random : compact('random', 'accessToken');
- }
-
- public static function verifyPlayer($account, $random, $token, $netId = self::TYPE_EOS) {
- if (!$account) {
- throw new Exception('参数错误', CODE_PARAM_ERROR);
- }
- $objRedis = dwRedis::init();
- $tokenKey = self::getAccessTokenKey($token);
- $storeRandom = $objRedis->get($tokenKey);
- if ($storeRandom != $random) {
- throw new Exception('access_token unmatch', CODE_PARAM_ERROR);
- }
- $accountType = Eos::getNetAccount($netId);
- $user_id = User::login($account, $accountType);
- return self::setCookie($user_id);
- }
-
- public static function verifyEosOnly($account, $random, $token) {
- if (!$account) {
- throw new Exception('参数错误', CODE_PARAM_ERROR);
- }
- $objRedis = dwRedis::init();
- $tokenKey = self::getAccessTokenKey($token);
- $storeRandom = $objRedis->get($tokenKey);
- if ($storeRandom != $random) {
- throw new Exception('access_token unmatch', CODE_PARAM_ERROR);
- }
- return true;
- }
-
- public static function verifyMsg($pubkey, $account, $data, $sign) {
- $json = EosBase::getAccount($account);
- $user = json_decode($json, true);
- $active = $user['permissions'][0]['required_auth']['keys'][0]['key'];
- $owner = $user['permissions'][1]['required_auth']['keys'][0]['key'];
- if ($pubkey != $active && $pubkey != $owner) {
- throw new Exception('pubkey error', CODE_PARAM_ERROR);
- }
- $cmd = "node {$GLOBALS['verifyPath']} '$data' '$pubkey' '$sign'";
-
- $ret = EosBase::execCmd($cmd);
- return trim($ret) == 'true';
- }
-
- public static function verifyEth($account, $data, $signed) {
- $data = "Welcome to MeeChat! Decentralization Social Platform ({$data})";
- $hash = Utils::hashPersonalMessage(Utils::strToHex($data));
- $messageGmp = gmp_init("0x" . $hash);
- $rHex = substr($signed, 2, 64);
- $sHex = substr($signed, 66, 64);
- $vValue = hexdec(substr($signed, 130, 2));
- $r = $rHex;
- $s = $sHex;
- $v = $vValue;
- $rGmp = gmp_init("0x" . $r);
- $sGmp = gmp_init("0x" . $s);
- $recovery = $v - 27;
- if ($recovery !== 0 && $recovery !== 1) {
- throw new Exception('Invalid signature v value');
- }
- $publicKey = Signature::recoverPublicKey($rGmp, $sGmp, $messageGmp, $recovery);
- $publicKeyString = $publicKey["x"] . $publicKey["y"];
- $recoveryAccount = '0x'. substr(self::keccak256(hex2bin($publicKeyString)), -40);
- return strtolower($recoveryAccount) == strtolower($account);
- }
-
- public static function verifyTron($account, $data, $signed) {
- $data = Utils::sha3($data);
- $cmd = "node {$GLOBALS['tronVerifyPath']} '$data' '$signed' '$account'";
-
- $ret = EosBase::execCmd($cmd);
- return trim($ret) == 'true';
- }
-
- private static function keccak256($str) {
- return '0x'. Keccak::hash($str, 256);
- }
- }
|