Пример #1
0
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt), MCRYPT_DEV_RANDOM);
     mcrypt_generic_init($mcrypt, $this->authKey, $iv);
     $url = $this->buildAuthUrl($username, $password, $mcrypt, $iv);
     list($code, $body) = $this->httpGet($url);
     if ($code === 404) {
         throw new Nette\Security\AuthenticationException("User '{$username}' not found.", self::IDENTITY_NOT_FOUND);
     } elseif ($code === 403) {
         throw new Nette\Security\AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
     } elseif ($code !== 200) {
         throw new Nette\Security\AuthenticationException("Nette.org endpoint hung with code {$code}.");
     }
     $json = Json::decode(trim(mdecrypt_generic($mcrypt, $body)));
     $user = $this->userManager->signInUpdate($json->id, ['username' => $username, 'email' => $json->email, 'name' => $json->realname]);
     if (!$user) {
         $user = $this->userManager->create(['id' => $json->id, 'username' => $username, 'password' => '', 'email' => $json->email, 'role' => 'user', 'active' => TRUE, 'name' => $json->realname, 'avatar' => '']);
     }
     return new Nette\Security\Identity($user->id, $user->role, ['username' => $user->username, 'name' => $user->name, 'email' => $user->email]);
 }