Example #1
0
 /**
  * Try logging in as a user with the given email and password.
  *
  * @return a valid {@link User}
  * @throws UserAuthenticationException if the user could not be logged in, with a reason
  */
 static function tryLogin(\Db\Connection $db, $email, $password)
 {
     if ($email === null) {
         throw new UserAuthenticationException("Email required for password login.");
     }
     // find the user with the email
     $q = $db->prepare("SELECT users.* FROM users\n        JOIN user_passwords ON users.id=user_passwords.user_id\n        WHERE email=? AND user_passwords.password_hash=? LIMIT 1");
     $q->execute(array($email, UserPassword::hash($password)));
     if ($user = $q->fetch()) {
         $result = new User($user);
         $result->setIdentity("(password)");
         return $result;
     } else {
         throw new UserAuthenticationMissingAccountException("No such email/password found.");
     }
 }
Example #2
0
 /**
  * Try logging in as a user with the given email and password.
  *
  * @param $redirect the registered redirect URI
  * @return a valid {@link User}
  * @throws UserAuthenticationException if the user could not be logged in, with a reason
  */
 static function tryLogin(\Db\Connection $db, $openid, $redirect)
 {
     if (!is_valid_url($openid)) {
         throw new UserSignupException("That is not a valid OpenID identity.");
     }
     if (!$redirect) {
         throw new \InvalidArgumentException("No redirect provided.");
     }
     $light = new \LightOpenID(\Openclerk\Config::get("openid_host"));
     if (!$light->mode) {
         // we still need to authenticate
         $light->identity = $openid;
         $light->returnUrl = $redirect;
         redirect($light->authUrl());
         return false;
     } else {
         if ($light->mode == 'cancel') {
             // user has cancelled
             throw new UserSignupException("User has cancelled authentication.");
         } else {
             // otherwise login as necessary
             // optionally check for abuse etc
             if (!\Openclerk\Events::trigger('openid_validate', $light)) {
                 throw new UserAuthenticationException("Login was cancelled.");
             }
             if ($light->validate()) {
                 $q = $db->prepare("SELECT users.* FROM users\n            JOIN user_openid_identities ON users.id=user_openid_identities.user_id\n            WHERE identity=? LIMIT 1");
                 $q->execute(array($light->identity));
                 if ($user = $q->fetch()) {
                     $result = new User($user);
                     $result->setIdentity("openid:" . $light->identity);
                     return $result;
                 } else {
                     throw new UserAuthenticationMissingAccountException("No account for the OpenID identity '" . $light->identity . "' was found.");
                 }
             } else {
                 $error = $light->validate_error ? $light->validate_error : "Please try again.";
                 throw new UserSignupException("OpenID validation was not successful: " . $error);
             }
         }
     }
 }