Пример #1
0
 /**
  * Validates a set of user credentials.
  *
  * - If the user credentials are valid, a new authentication token is
  *   created and a corresponding Token instance is returned.
  * - If the user credentials are invalid, an InvalidException instance is
  *   thrown.
  * - If for some reason the user credentials cannot be validated, an
  *   AuthException instance is thrown.
  *
  * @param \Spark\Auth\Credentials $credentials
  * @return \Spark\Auth\Token
  * @throws \Spark\Auth\Exception\InvalidException if an invalid auth token
  *         is specified
  * @throws \Spark\Auth\Exception\AuthException if another error occurs
  *         during authentication
  */
 public function validateCredentials(Credentials $credentials)
 {
     #var_dump($credentials);
     $offered_identifier = $credentials->getIdentifier();
     $offered_password = $credentials->getPassword();
     $expected_identifier = $this->credentials->getIdentifier();
     $expected_password = $this->credentials->getPassword();
     if ($offered_identifier == $expected_identifier && $offered_password == $expected_password) {
         // generate a random token string
         $token_string = bin2hex(openssl_random_pseudo_bytes(16));
         $metadata = array('username' => $offered_identifier, 'expiration' => date(DateTime::ATOM, strtotime('+1 hour')));
         $token = new Token($token_string, $metadata);
         // update the token in the database and set the expiration date-time
         $this->valid_tokens->updateToken($token);
     } else {
         throw new InvalidException();
     }
     return $token;
 }