Skip to content

Added missing security.yaml config #13418

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
Oct 14, 2020
Merged
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
65 changes: 61 additions & 4 deletions security/guard_authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,63 @@ Don't forget to generate and execute the migration:
$ php bin/console make:migration
$ php bin/console doctrine:migrations:migrate

Next, configure your "user provider" to use this new ``apiToken`` property:

.. configuration-block::

.. code-block:: yaml

# config/packages/security.yaml
security:
# ...

providers:
your_db_provider:
entity:
class: App\Entity\User
property: apiToken

# ...

.. code-block:: xml

<!-- config/packages/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<config>
<!-- ... -->

<provider name="your_db_provider">
<entity class="App\Entity\User" property="apiToken"/>
</provider>

<!-- ... -->
</config>
</srv:container>

.. code-block:: php

// config/packages/security.php
$container->loadFromExtension('security', [
// ...

'providers' => [
'your_db_provider' => [
'entity' => [
'class' => 'App\Entity\User',
'property' => 'apiToken',
],
],
],

// ...
]);

Step 2) Create the Authenticator Class
--------------------------------------

Expand Down Expand Up @@ -108,10 +165,10 @@ This requires you to implement several methods::
return null;
}

// if a User is returned, checkCredentials() is called
return $this->em->getRepository(User::class)
->findOneBy(['apiToken' => $credentials])
;
// The "username" in this case is the apiToken, see the key `property`
// of `your_db_provider` in `security.yaml`.
// If this returns a user, checkCredentials() is called next:
return $userProvider->loadUserByUsername($apiToken);
}

public function checkCredentials($credentials, UserInterface $user)
Expand Down