|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
|
14
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
15
|
|
|
* |
|
16
|
|
|
* Licensed under The MIT License |
|
17
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
18
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
19
|
|
|
* |
|
20
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
21
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
|
22
|
|
|
* @since 0.10.0 |
|
23
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace CakeDC\Api\Service\Auth\Authenticate; |
|
27
|
|
|
|
|
28
|
|
|
use Cake\Datasource\EntityInterface; |
|
29
|
|
|
use Cake\Http\Response; |
|
30
|
|
|
use Cake\Http\ServerRequest; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class FormAuthenticate. |
|
34
|
|
|
*/ |
|
35
|
|
|
class Psr7Authenticate extends BaseAuthenticate |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields` |
|
40
|
|
|
* to find POST data that is used to find a matching record in the `config.userModel`. Will return false if |
|
41
|
|
|
* there is no post data, either username or password is missing, or if the scope conditions have not been met. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Cake\Http\ServerRequest $request The request that contains login information. |
|
44
|
|
|
* @param \Cake\Http\Response $response Unused response object. |
|
45
|
|
|
* @return mixed False on login failure. An array of User data on success. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function authenticate(ServerRequest $request, Response $response) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getUser($request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get a user based on information in the request. Primarily used by stateless authentication |
|
54
|
|
|
* systems like basic and digest auth. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Cake\Http\ServerRequest $request Request object. |
|
57
|
|
|
* @return mixed Either false or an array of user information |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getUser(ServerRequest $request) |
|
60
|
|
|
{ |
|
61
|
|
|
$authResult = $request->getAttribute('authentication')->getResult(); |
|
62
|
|
|
if (!$authResult->isValid()) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$result = $request->getAttribute('identity'); |
|
67
|
|
|
|
|
68
|
|
|
if ($result instanceof EntityInterface) { |
|
69
|
|
|
return $result->toArray(); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.