FormAuthenticate
extends BaseAuthenticate
in package
Form authentication adapter for AuthComponent.
Allows you to authenticate users based on form POST data. Usually, this is a login form that users enter information into.
Using Form auth
Load AuthComponent
in your controller's initialize()
and add 'Form' in 'authenticate' key
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'passwd'],
'finder' => 'auth',
]
]
]);
When configuring FormAuthenticate you can pass in config to which fields, model and finder
are used. See BaseAuthenticate::$_defaultConfig
for more information.
Tags
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default config for this object.
- $_needsPasswordRehash : bool
- Whether the user authenticated by this class requires their password to be rehashed with another algorithm.
- $_passwordHasher : AbstractPasswordHasher|null
- Password hasher instance.
- $_registry : ComponentRegistry
- A Component registry, used to get more components.
- $_tableLocator : LocatorInterface|null
- Table locator instance
- $defaultTable : string|null
- This object's default table alias.
Methods
- __construct() : mixed
- Constructor
- authenticate() : array<string, mixed>|false
- Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields` to find POST data that is used to find a matching record in the `config.userModel`. Will return false if there is no post data, either username or password is missing, or if the scope conditions have not been met.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- fetchTable() : Table
- Convenience method to get a table instance.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getTableLocator() : LocatorInterface
- Gets the table locator.
- getUser() : array<string, mixed>|false
- Get a user based on information in the request. Primarily used by stateless authentication systems like basic and digest auth.
- implementedEvents() : array<string, mixed>
- Returns a list of all events that this authenticate class will listen to.
- needsPasswordRehash() : bool
- Returns whether the password stored in the repository for the logged in user requires to be rehashed with another algorithm
- passwordHasher() : AbstractPasswordHasher
- Return password hasher object
- setConfig() : $this
- Sets the config.
- setTableLocator() : $this
- Sets the table locator.
- unauthenticated() : Response|null|void
- Handle unauthenticated access attempt. In implementation valid return values can be:
- _checkFields() : bool
- Checks the fields to ensure they are supplied.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _findUser() : array<string, mixed>|false
- Find a user record using the username and password provided.
- _query() : Query
- Get query object for fetching user from database.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default config for this object.
protected
array<string, mixed>
$_defaultConfig
= ['fields' => ['username' => 'username', 'password' => 'password'], 'userModel' => 'Users', 'finder' => 'all', 'passwordHasher' => 'Default']
-
fields
The fields to use to identify a user by. -
userModel
The alias for users table, defaults to Users. -
finder
The finder method to use to fetch user record. Defaults to 'all'. You can set finder name as string or an array where key is finder name and value is an array passed toTable::find()
options. E.g. ['finderName' => ['some_finder_option' => 'some_value']] -
passwordHasher
Password hasher class. Can be a string specifying class name or an array containingclassName
key, any other keys will be passed as config to the class. Defaults to 'Default'.
$_needsPasswordRehash
Whether the user authenticated by this class requires their password to be rehashed with another algorithm.
protected
bool
$_needsPasswordRehash
= false
$_passwordHasher
Password hasher instance.
protected
AbstractPasswordHasher|null
$_passwordHasher
$_registry
A Component registry, used to get more components.
protected
ComponentRegistry
$_registry
$_tableLocator
Table locator instance
protected
LocatorInterface|null
$_tableLocator
$defaultTable
This object's default table alias.
protected
string|null
$defaultTable
= null
Methods
__construct()
Constructor
public
__construct(ComponentRegistry $registry[, array<string, mixed> $config = [] ]) : mixed
Parameters
- $registry : ComponentRegistry
-
The Component registry used on this request.
- $config : array<string, mixed> = []
-
Array of config to use.
authenticate()
Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields` to find POST data that is used to find a matching record in the `config.userModel`. Will return false if there is no post data, either username or password is missing, or if the scope conditions have not been met.
public
authenticate(ServerRequest $request, Response $response) : array<string, mixed>|false
Parameters
- $request : ServerRequest
-
The request that contains login information.
- $response : Response
-
Unused response object.
Return values
array<string, mixed>|false —False on login failure. An array of User data on success.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisfetchTable()
Convenience method to get a table instance.
public
fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
- $alias : string|null = null
-
The alias name you want to get. Should be in CamelCase format. If
null
then the value of $defaultTable property is used. - $options : array<string, mixed> = []
-
The options you want to build the table with. If a table has already been loaded the registry options will be ignored.
Tags
Return values
TablegetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getTableLocator()
Gets the table locator.
public
getTableLocator() : LocatorInterface
Return values
LocatorInterfacegetUser()
Get a user based on information in the request. Primarily used by stateless authentication systems like basic and digest auth.
public
getUser(ServerRequest $request) : array<string, mixed>|false
Parameters
- $request : ServerRequest
-
Request object.
Return values
array<string, mixed>|false —Either false or an array of user information
implementedEvents()
Returns a list of all events that this authenticate class will listen to.
public
implementedEvents() : array<string, mixed>
An authenticate class can listen to following events fired by AuthComponent:
-
Auth.afterIdentify
- Fired after a user has been identified using one of configured authenticate class. The callback function should have signature likeafterIdentify(EventInterface $event, array $user)
when$user
is the identified user record. -
Auth.logout
- Fired when AuthComponent::logout() is called. The callback function should have signature likelogout(EventInterface $event, array $user)
where$user
is the user about to be logged out.
Return values
array<string, mixed> —List of events this class listens to. Defaults to []
.
needsPasswordRehash()
Returns whether the password stored in the repository for the logged in user requires to be rehashed with another algorithm
public
needsPasswordRehash() : bool
Return values
boolpasswordHasher()
Return password hasher object
public
passwordHasher() : AbstractPasswordHasher
Tags
Return values
AbstractPasswordHasher —Password hasher instance
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thissetTableLocator()
Sets the table locator.
public
setTableLocator(LocatorInterface $tableLocator) : $this
Parameters
- $tableLocator : LocatorInterface
-
LocatorInterface instance.
Return values
$thisunauthenticated()
Handle unauthenticated access attempt. In implementation valid return values can be:
public
unauthenticated(ServerRequest $request, Response $response) : Response|null|void
- Null - No action taken, AuthComponent should return appropriate response.
- \Cake\Http\Response - A response object, which will cause AuthComponent to simply return that response.
Parameters
- $request : ServerRequest
-
A request object.
- $response : Response
-
A response object.
Return values
Response|null|void_checkFields()
Checks the fields to ensure they are supplied.
protected
_checkFields(ServerRequest $request, array<string, string> $fields) : bool
Parameters
- $request : ServerRequest
-
The request that contains login information.
- $fields : array<string, string>
-
The fields to be checked.
Return values
bool —False if the fields have not been supplied. True if they exist.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_findUser()
Find a user record using the username and password provided.
protected
_findUser(string $username[, string|null $password = null ]) : array<string, mixed>|false
Input passwords will be hashed even when a user doesn't exist. This helps mitigate timing attacks that are attempting to find valid usernames.
Parameters
- $username : string
-
The username/identifier.
- $password : string|null = null
-
The password, if not provided password checking is skipped and result of find is returned.
Return values
array<string, mixed>|false —Either false on failure, or an array of user data.
_query()
Get query object for fetching user from database.
protected
_query(string $username) : Query
Parameters
- $username : string
-
The username/identifier.