Skip to content

SecurityExtensions: add option for users data #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Bridges/SecurityDI/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function getConfigSchema(): Nette\Schema\Schema
'users' => Expect::arrayOf(
Expect::anyOf(
Expect::string(), // user => password
Expect::structure([ // user => password + roles
Expect::structure([ // user => password + roles + data
'password' => Expect::string(),
'roles' => Expect::anyOf(Expect::string(), Expect::listOf('string')),
'data' => Expect::array(),
])->castTo('array')
)
),
Expand Down Expand Up @@ -70,17 +71,18 @@ public function loadConfiguration()
}

if ($config->users) {
$usersList = $usersRoles = [];
$usersList = $usersRoles = $usersData = [];
foreach ($config->users as $username => $data) {
$data = is_array($data) ? $data : ['password' => $data];
$this->validateConfig(['password' => null, 'roles' => null], $data, $this->prefix("security.users.$username"));
$this->validateConfig(['password' => null, 'roles' => null, 'data' => []], $data, $this->prefix("security.users.$username"));
$usersList[$username] = $data['password'];
$usersRoles[$username] = $data['roles'] ?? null;
$usersData[$username] = $data['data'] ?? [];
}

$builder->addDefinition($this->prefix('authenticator'))
->setType(Nette\Security\IAuthenticator::class)
->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles]);
->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles, $usersData]);

if ($this->name === 'security') {
$builder->addAlias('nette.authenticator', $this->prefix('authenticator'));
Expand Down
9 changes: 7 additions & 2 deletions src/Security/SimpleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ class SimpleAuthenticator implements IAuthenticator
/** @var array */
private $usersRoles;

/** @var array */
private $usersData;


/**
* @param array $userlist list of pairs username => password
* @param array $usersRoles list of pairs username => role[]
* @param array $usersData list of pairs username => mixed[]
*/
public function __construct(array $userlist, array $usersRoles = [])
public function __construct(array $userlist, array $usersRoles = [], array $usersData = [])
{
$this->userlist = $userlist;
$this->usersRoles = $usersRoles;
$this->usersData = $usersData;
}


Expand All @@ -48,7 +53,7 @@ public function authenticate(array $credentials): IIdentity
foreach ($this->userlist as $name => $pass) {
if (strcasecmp($name, $username) === 0) {
if ((string) $pass === (string) $password) {
return new Identity($name, $this->usersRoles[$name] ?? null);
return new Identity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
} else {
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Security/SimpleAuthenticator.Data.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Test: Nette\Security\SimpleAuthenticator and data
*/

declare(strict_types=1);

use Nette\Security\SimpleAuthenticator;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$users = [
'john' => 'john123',
'admin' => 'admin123',
'user' => 'user123',
];
$usersData = [
'admin' => ['nick' => 'admin', 'email' => 'foo@bar.com'],
'user' => ['nick' => 'user', 'email' => 'foo@bar.com'],
];
$expectedData = [
'admin' => ['nick' => 'admin', 'email' => 'foo@bar.com'],
'user' => ['nick' => 'user', 'email' => 'foo@bar.com'],
'john' => [],
];

$authenticator = new SimpleAuthenticator($users, [], $usersData);

foreach ($users as $username => $password) {
$identity = $authenticator->authenticate([$username, $password]);
Assert::equal($username, $identity->getId());
Assert::equal($expectedData[$username], $identity->getData());
}