function makeNew($user)
 {
     $login_token = Login_token::staticGet('user_id', $user->id);
     if (!empty($login_token)) {
         $login_token->delete();
     }
     $login_token = new Login_token();
     $login_token->user_id = $user->id;
     $login_token->token = common_good_rand(16);
     $login_token->created = common_sql_now();
     $result = $login_token->insert();
     if (!$result) {
         common_log_db_error($login_token, 'INSERT', __FILE__);
         throw new Exception(sprintf(_('Could not create login token for %s'), $user->nickname));
     }
     return $login_token;
 }
Exemplo n.º 2
0
 function makeNew($user)
 {
     $login_token = Login_token::getKV('user_id', $user->id);
     if (!empty($login_token)) {
         $login_token->delete();
     }
     $login_token = new Login_token();
     $login_token->user_id = $user->id;
     $login_token->token = common_random_hexstr(16);
     $login_token->created = common_sql_now();
     $result = $login_token->insert();
     if (!$result) {
         common_log_db_error($login_token, 'INSERT', __FILE__);
         // TRANS: Exception thrown when trying creating a login token failed.
         // TRANS: %s is the user nickname for which token creation failed.
         throw new Exception(sprintf(_('Could not create login token for %s'), $user->nickname));
     }
     return $login_token;
 }
Exemplo n.º 3
0
 function prepare($args)
 {
     parent::prepare($args);
     if (common_is_real_login()) {
         // TRANS: Client error displayed trying to use "one time password login" when already logged in.
         $this->clientError(_('Already logged in.'));
         return false;
     }
     $id = $this->trimmed('user_id');
     if (empty($id)) {
         // TRANS: Client error displayed trying to use "one time password login" without specifying a user.
         $this->clientError(_('No user ID specified.'));
         return false;
     }
     $this->user = User::staticGet('id', $id);
     if (empty($this->user)) {
         // TRANS: Client error displayed trying to use "one time password login" without using an existing user.
         $this->clientError(_('No such user.'));
         return false;
     }
     $this->token = $this->trimmed('token');
     if (empty($this->token)) {
         // TRANS: Client error displayed trying to use "one time password login" without specifying a login token.
         $this->clientError(_('No login token specified.'));
         return false;
     }
     $this->lt = Login_token::staticGet('user_id', $id);
     if (empty($this->lt)) {
         // TRANS: Client error displayed trying to use "one time password login" without requesting a login token.
         $this->clientError(_('No login token requested.'));
         return false;
     }
     if ($this->lt->token != $this->token) {
         // TRANS: Client error displayed trying to use "one time password login" while specifying an invalid login token.
         $this->clientError(_('Invalid login token specified.'));
         return false;
     }
     if ($this->lt->modified > time() + Login_token::TIMEOUT) {
         //token has expired
         //delete the token as it is useless
         $this->lt->delete();
         $this->lt = null;
         // TRANS: Client error displayed trying to use "one time password login" while specifying an expired login token.
         $this->clientError(_('Login token expired.'));
         return false;
     }
     $this->rememberme = $this->boolean('rememberme');
     $this->returnto = $this->trimmed('returnto');
     return true;
 }
Exemplo n.º 4
0
 function prepare($args)
 {
     parent::prepare($args);
     if (common_is_real_login()) {
         $this->clientError(_('Already logged in.'));
         return false;
     }
     $id = $this->trimmed('user_id');
     if (empty($id)) {
         $this->clientError(_('No user ID specified.'));
         return false;
     }
     $this->user = User::staticGet('id', $id);
     if (empty($this->user)) {
         $this->clientError(_('No such user.'));
         return false;
     }
     $this->token = $this->trimmed('token');
     if (empty($this->token)) {
         $this->clientError(_('No login token specified.'));
         return false;
     }
     $this->lt = Login_token::staticGet('user_id', $id);
     if (empty($this->lt)) {
         $this->clientError(_('No login token requested.'));
         return false;
     }
     if ($this->lt->token != $this->token) {
         $this->clientError(_('Invalid login token specified.'));
         return false;
     }
     if ($this->lt->modified > time() + Login_token::TIMEOUT) {
         //token has expired
         //delete the token as it is useless
         $this->lt->delete();
         $this->lt = null;
         $this->clientError(_('Login token expired.'));
         return false;
     }
     $this->rememberme = $this->boolean('rememberme');
     $this->returnto = $this->trimmed('returnto');
     return true;
 }
Exemplo n.º 5
0
 function handle($channel)
 {
     $disabled = common_config('logincommand', 'disabled');
     $disabled = isset($disabled) && $disabled;
     if ($disabled) {
         $channel->error($this->user, _('Login command is disabled'));
         return;
     }
     try {
         $login_token = Login_token::makeNew($this->user);
     } catch (Exception $e) {
         $channel->error($this->user, $e->getMessage());
     }
     $channel->output($this->user, sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'), common_local_url('otp', array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
 }
 static function login($email, $password)
 {
     $domain = self::toDomain($email);
     $sn = self::siteForDomain($domain);
     if (empty($sn)) {
         throw new ClientException(_("No such site."));
     }
     StatusNet::switchSite($sn->nickname);
     $user = common_check_user($email, $password);
     if (empty($user)) {
         // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
         throw new ClientException(_('Incorrect username or password.'));
     }
     $loginToken = Login_token::makeNew($user);
     if (empty($loginToken)) {
         throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
     }
     $url = common_local_url('otp', array('user_id' => $loginToken->user_id, 'token' => $loginToken->token));
     if (empty($url)) {
         throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
     }
     return $url;
 }