Usuario.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\web\IdentityInterface;
  5. use Firebase\JWT\JWT;
  6. use Kreait\Firebase\Factory;
  7. use Kreait\Firebase\Exception\AuthException;
  8. use Kreait\Firebase\Exception\FirebaseException;
  9. class Usuario implements IdentityInterface {
  10. public $uid;
  11. public $correo;
  12. public $nombre;
  13. protected static function firebase() {
  14. $params = \Yii::$app->params;
  15. $firebase = (new Factory())
  16. ->withServiceAccount($params['firebaseKey'])
  17. ->createAuth();
  18. return $firebase;
  19. }
  20. /**
  21. * Finds an identity by the given id.
  22. *
  23. * @param string|int $id the id to be looked for
  24. * @return IdentityInterface|null the identity object that matches the given id.
  25. */
  26. public static function findIdentity($uid) {
  27. $firebase = self::firebase();
  28. try {
  29. $fUsuario = $firebase->getUser($uid);
  30. if ($fUsuario->disabled === true) {
  31. return null;
  32. }
  33. $usuario = new static();
  34. $usuario->uid = $uid;
  35. $usuario->nombre = "";
  36. if($fUsuario->displayName) {
  37. $usuario->nombre = $fUsuario->displayName;
  38. }
  39. $usuario->correo = $fUsuario->email;
  40. return $usuario;
  41. } catch (AuthException $e) {
  42. return null;
  43. } catch (FirebaseException $e) {
  44. return null;
  45. }
  46. }
  47. /**
  48. * Finds an identity by the given token.
  49. *
  50. * @param string $token the token to be looked for
  51. * @return IdentityInterface|null the identity object that matches the given token.
  52. */
  53. public static function findIdentityByAccessToken($token, $type = null) {
  54. $firebase = self::firebase();
  55. try {
  56. $verifiedIdToken = $firebase->verifyIdToken($token);
  57. $uid = $verifiedIdToken->claims()->get('sub');
  58. $fUsuario = $firebase->getUser($uid);
  59. if ($fUsuario->disabled === true) {
  60. return null;
  61. }
  62. $usuario = new static();
  63. $usuario->uid = $uid;
  64. $usuario->nombre = "";
  65. if($fUsuario->displayName) {
  66. $usuario->nombre = $fUsuario->displayName;
  67. }
  68. $usuario->correo = $fUsuario->email;
  69. return $usuario;
  70. } catch (AuthException $e) {
  71. return null;
  72. } catch (FirebaseException $e) {
  73. return null;
  74. }
  75. }
  76. /**
  77. * @return int|string current user ID
  78. */
  79. public function getId() {
  80. return $this->uid;
  81. }
  82. /**
  83. * @return string current user auth key
  84. */
  85. public function getAuthKey() {
  86. $key = Yii::$app->params['jwt.key'];
  87. $token = [
  88. "id" => $this->id,
  89. "pass" => $this->clave
  90. ];
  91. $jwt = JWT::encode($token, $key);
  92. return $jwt;
  93. }
  94. /**
  95. * @param string $authKey
  96. * @return bool if auth key is valid for current user
  97. */
  98. public function validateAuthKey($authKey) {
  99. $key = Yii::$app->params['jwt.key'];
  100. $jwt = JWT::decode($authKey, $key);
  101. if(!isset($jwt["id"])) {
  102. return false;
  103. }
  104. return $jwt["id"] == $this->id;
  105. }
  106. }