prihlasovanie -chyba → password is incorect
- Takeshi
- Člen | 596
Ahojte,
takze pokusam sa zpojazdnit prihlasovanie … lenze mi hadze problem … patral som patral, ale nedopatral som sa, tak sa obraciam na skusenejsich …
pri logine mi vzdy hadze chybu : password is incorrect
uz som skusal dat heslo aj cez md5() funkciu, ale stale nic … viete kde mam
problem?
- Takeshi
- Člen | 596
Ospravedlnujem sa za neohrabanost, ale prihlasovanie som este nerobil a tak som na totalnom zaciatku a ani neviem co mam kde dat …
Authenticator som sa snazil pouzit ako je na stranke … teda vytvoril som si v presenteri MyAuthenticator
<?php
use Nette\Security as NS;
class MyAuthenticator extends Nette\Object implements NS\IAuthenticator
{
public $database;
function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->database->table('users')
->where('username', $username)->fetch();
if (!$row) {
throw new NS\AuthenticationException('User not found.');
}
if ($row->password !== md5($password)) {
throw new NS\AuthenticationException('Invalid password.');
}
return new NS\Identity($row->id, $row->role);
}
}
?>
tabulku mam vytvorenu zatial len na username a password … snazil som sa aj pouzit
<?php
common:
services:
authenticator: MyAuthenticator
?>
ale neviem kde to mam dat, kedze som to skusal do oboch config suboroch, a vzdy mi to hadzalo chybu
Editoval Takeshi (26. 2. 2014 8:36)
- Tirus91
- Člen | 199
@Takeshi
neboj, taky jsem začal nedávno a nic jsem nevěděl :)
Já tam neyužívám md5, ale hashování co bylo v sandboxu
můj config.neon vypadá takto
php:
date.timezone: Europe/Prague
# zlib.output_compression: yes
nette:
application:
errorPresenter: Error
mapping:
*: App\*Module\Presenters\*Presenter
session:
expiration: 14 days
services:
authenticator: \App\FrontModule\Model\UserManager
- App\RouterFactory
router: @App\RouterFactory::createRouter
No a \App\FrontModule\Model\UserManager vypadá takto
public function authenticate(array $credentials) {
list($username, $password) = $credentials;
$password = self::removeCapsLock($password);
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
$row = $this->database->query('SELECT * FROM ' . self::TABLE_NAME . ' WHERE ' . self::COLUMN_EMAIL . ' = %s', $username)->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException(_('The email is incorrect.'), self::IDENTITY_NOT_FOUND);
}
} else {
$row = $this->database->query('SELECT * FROM ' . self::TABLE_NAME . ' WHERE ' . self::COLUMN_USERNAME . ' = %s', $username)->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException(_('The username is incorrect.'), self::IDENTITY_NOT_FOUND);
}
}
if ($row[self::COLUMN_ACTIVATED] != 1) {
throw new Nette\Security\AuthenticationException(_('The account is not activated.'), self::FAILURE);
}
if (!\App\Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new Nette\Security\AuthenticationException(_('The password is incorrect.'), self::INVALID_CREDENTIAL);
} elseif (\App\Passwords::needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$this->database->query('UPDATE ' . self::TABLE_NAME . ' SET ' . self::COLUMN_PASSWORD_HASH . ' = %s ' . \App\Passwords::hash($password) . ' WHERE ' . self::COLUMN_EMAIL . ' = %s ', $row[self::COLUMN_EMAIL]);
}
$arr = $row->toArray();
unset($arr[self::COLUMN_PASSWORD_HASH]);
$this->database->query('UPDATE ' . self::TABLE_NAME . ' SET ' . self::COLUMN_LOGGED_AT . ' = ', new \DateTime, ' WHERE id = %i', $row['id']);
$groups = $this->database->query('SELECT security_group.id,security_group.name FROM security_usgr LEFT JOIN security_group ON security_usgr.group_id = security_group.id WHERE security_usgr.user_id = %i ', $row['id'])->fetchPairs('id', 'name');
$arr['groups'] = $groups;
$datetime = new \DateTime;
if ($this->database->query('SELECT count(*) FROM security_bans WHERE user_id = %i ', $row['id'], ' AND from_date <= ', $datetime, ' AND (to_date IS NULL OR to_date >= ', $datetime, ')')->fetchSingle() > 0) {
throw new Nette\Security\AuthenticationException(_('Your account has been banned.'), self::INVALID_CREDENTIAL);
}
return new Nette\Security\Identity($row[self::COLUMN_ID], null, $arr);
}
No a to heslo je hashováno pomocí \App\Passwords::hash a ověřováno pomocí \App\Passwords::verifyHash
Zde je celá ta třída
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace App;
use Nette;
/**
* Passwords tools. Requires PHP >= 5.3.7.
*
* @author David Grudl
*/
class Passwords
{
const PASSWORD_MAX_LENGTH = 4096;
const BCRYPT_COST = 10;
/**
* Computes salted password hash.
* @param string
* @param array with cost (4-31), salt (22 chars)
* @return string 60 chars long
*/
public static function hash($password, array $options = NULL)
{
$cost = isset($options['cost']) ? (int) $options['cost'] : self::BCRYPT_COST;
$salt = isset($options['salt']) ? (string) $options['salt'] : Nette\Utils\Strings::random(22, '0-9A-Za-z./');
if (PHP_VERSION_ID < 50307) {
throw new Nette\NotSupportedException(__METHOD__ . ' requires PHP >= 5.3.7.');
} elseif (($len = strlen($salt)) < 22) {
throw new Nette\InvalidArgumentException("Salt must be 22 characters long, $len given.");
} elseif ($cost < 4 || $cost > 31) {
throw new Nette\InvalidArgumentException("Cost must be in range 4-31, $cost given.");
}
$password = substr($password, 0, self::PASSWORD_MAX_LENGTH);
$hash = crypt($password, '$2y$' . ($cost < 10 ? 0 : '') . $cost . '$' . $salt);
if (strlen($hash) < 60) {
throw new Nette\InvalidStateException('Hash returned by crypt is invalid.');
}
return $hash;
}
/**
* Verifies that a password matches a hash.
* @return bool
*/
public static function verify($password, $hash)
{
return preg_match('#^\$2y\$(?P<cost>\d\d)\$(?P<salt>.{22})#', $hash, $m)
&& $m['cost'] > 3 && $m['cost'] < 31
&& self::hash($password, $m) === $hash;
}
/**
* Checks if the given hash matches the options.
* @param string
* @param array with cost (4-31)
* @return bool
*/
public static function needsRehash($hash, array $options = NULL)
{
$cost = isset($options['cost']) ? (int) $options['cost'] : self::BCRYPT_COST;
return !preg_match('#^\$2y\$(?P<cost>\d\d)\$(?P<salt>.{22})#', $hash, $m)
|| $m['cost'] < $cost;
}
}
Snad ti to poradí či tě to nakopne.
Jak jsem sám psal, sám s tím bojuji :)
Editoval Tirus91 (26. 2. 2014 9:01)
- Takeshi
- Člen | 596
Tirus91 napsal(a):
No tak zacal som s tym, ze som do config.neon dal nasledovne riadky …
<?php
services:
authenticator: \App\FrontModule\Model\UserManager
- App\UserManager
- App\RouterFactory
router: @App\RouterFactory::createRouter
?>
a hned mi to hodilo chybu
<?php
Class \App\FrontModule\Model\UserManager used in service 'authenticator' has not been found or is not instantiable.
?>
- Takeshi
- Člen | 596
greeny napsal(a):
<?php $user->getRoles() // vrati pole roli ?>
:-) Skusal som to tvoje a nasledne som to dal do foreach cyklu a vratilo mi username co mam v databze
<?php
$this->template->roles = $user->getRoles();
{foreach $roles as $role}
{$role}
{/foreach}
?>
tak som z toho vedle jak ta jedle
Editoval Takeshi (26. 2. 2014 12:08)
- Takeshi
- Člen | 596
Tak sa zase vraciam k mojmu predoslemu problemu … neviem co sa stalo, ale
zase mi to hadze tu isty chybu
teda predosla otazka …
OK A ako vyzera v mysql tabulka users? … username a password mi to preslo, ale teraz mi hadze chybu pri ‚role‘ aj ked ho v tabulke mam
<?php
Cannot read an undeclared column "role".
?>
neda sem niekto ako vyzera jeho tabulka users? … teda hadam ze ma problem zo stlpcom role … ale co? to neviem
- Takeshi
- Člen | 596
OK … zoberiem to pekne od zaciatku :-)
1. vytvoril som si tabulku:
<?php
create table users(
id int not null auto_increment primary key,
username text,
password text,
role text,
)
?>
2. Vytvoril som si v app/presenters subor MyAuthenticate.php a naplnil ho podla stranky https://doc.nette.org/…thentication
3. do config.neon som pridal jeden riadok …
<?php
services:
authenticator: MyAuthenticator
- App\RouterFactory
router: @App\RouterFactory::createRouter
?>
4. potom uz som len pridal formular a ak bol uspesne odoslany, tak som sa prihlasil
- Takeshi
- Člen | 596
Takeshi napsal(a):
OK … zoberiem to pekne od zaciatku :-)
1. vytvoril som si tabulku:
<?php create table users( id int not null auto_increment primary key, username text, password text, role text, ) ?>
2. Vytvoril som si v app/presenters subor MyAuthenticate.php a naplnil ho podla stranky https://doc.nette.org/…thentication
3. do config.neon som pridal jeden riadok …
<?php services: authenticator: MyAuthenticator - App\RouterFactory router: @App\RouterFactory::createRouter ?>
4. potom uz som len pridal formular a ak bol uspesne odoslany, tak som sa prihlasil
A ozaj … ked sa skusam prihlasovat, tak som si este vlozil do tabulky nasledovne udaje …
<?php
username => aaa
password => bbb (ale ako vysledok funkcie md5(bbb) )
role => admin
?>
- leninzprahy
- Člen | 150
OT:
Pro bezpečné ukládání hesel, není doporučené používat ani md5 ani sha1 otisk, viz třeba http://www.php.net/…asswords.php.
Doporučená je funkce crypt() a metoda CRYPT_BLOWFISH