1: <?php
2: /**
3: *
4: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6: *
7: * Licensed under The MIT License
8: * For full copyright and license information, please see the LICENSE.txt
9: * Redistributions of files must retain the above copyright notice.
10: *
11: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12: * @link https://cakephp.org CakePHP(tm) Project
13: * @since 2.0.0
14: * @license https://opensource.org/licenses/mit-license.php MIT License
15: */
16: namespace Cake\Auth;
17:
18: use Cake\Http\Response;
19: use Cake\Http\ServerRequest;
20:
21: /**
22: * Form authentication adapter for AuthComponent.
23: *
24: * Allows you to authenticate users based on form POST data.
25: * Usually, this is a login form that users enter information into.
26: *
27: * ### Using Form auth
28: *
29: * Load `AuthComponent` in your controller's `initialize()` and add 'Form' in 'authenticate' key
30: *
31: * ```
32: * $this->loadComponent('Auth', [
33: * 'authenticate' => [
34: * 'Form' => [
35: * 'fields' => ['username' => 'email', 'password' => 'passwd'],
36: * 'finder' => 'auth',
37: * ]
38: * ]
39: * ]);
40: * ```
41: *
42: * When configuring FormAuthenticate you can pass in config to which fields, model and finder
43: * are used. See `BaseAuthenticate::$_defaultConfig` for more information.
44: *
45: * @see https://book.cakephp.org/3.0/en/controllers/components/authentication.html
46: */
47: class FormAuthenticate extends BaseAuthenticate
48: {
49:
50: /**
51: * Checks the fields to ensure they are supplied.
52: *
53: * @param \Cake\Http\ServerRequest $request The request that contains login information.
54: * @param array $fields The fields to be checked.
55: * @return bool False if the fields have not been supplied. True if they exist.
56: */
57: protected function _checkFields(ServerRequest $request, array $fields)
58: {
59: foreach ([$fields['username'], $fields['password']] as $field) {
60: $value = $request->getData($field);
61: if (empty($value) || !is_string($value)) {
62: return false;
63: }
64: }
65:
66: return true;
67: }
68:
69: /**
70: * Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
71: * to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
72: * there is no post data, either username or password is missing, or if the scope conditions have not been met.
73: *
74: * @param \Cake\Http\ServerRequest $request The request that contains login information.
75: * @param \Cake\Http\Response $response Unused response object.
76: * @return mixed False on login failure. An array of User data on success.
77: */
78: public function authenticate(ServerRequest $request, Response $response)
79: {
80: $fields = $this->_config['fields'];
81: if (!$this->_checkFields($request, $fields)) {
82: return false;
83: }
84:
85: return $this->_findUser(
86: $request->getData($fields['username']),
87: $request->getData($fields['password'])
88: );
89: }
90: }
91: