This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Germania\PermanentAuth; |
||
3 | |||
4 | use Germania\PermanentAuth\Exceptions\RequestException; |
||
5 | use Germania\PermanentAuth\AuthUserInterface; |
||
6 | |||
7 | use Psr\Http\Message\ServerRequestInterface as Request; |
||
8 | use Psr\Http\Message\ResponseInterface as Response; |
||
9 | use Psr\Log\LoggerInterface; |
||
10 | use Psr\Log\NullLogger; |
||
11 | |||
12 | |||
13 | /** |
||
14 | * This PSR-style Middleware tries to distill a User ID from "permanent login" data. |
||
15 | * |
||
16 | * - Tries to retrieve selector and token from client-side authentication (i.e. cookie) |
||
17 | * - Check against database and get User ID |
||
18 | * - Assign User ID to the user object passed into constructor |
||
19 | */ |
||
20 | class Middleware |
||
21 | { |
||
22 | /** |
||
23 | * @var Callable |
||
24 | */ |
||
25 | public $client_authenticator; |
||
26 | |||
27 | /** |
||
28 | * @var Callable |
||
29 | */ |
||
30 | public $client_auth_remover; |
||
31 | |||
32 | /** |
||
33 | * @var Callable |
||
34 | */ |
||
35 | public $server_authenticator; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | public $logger; |
||
41 | |||
42 | /** |
||
43 | * HTTP Status code to use on redirect after successful login |
||
44 | * @var int |
||
45 | */ |
||
46 | public $authenticated_status_code = 200; |
||
47 | |||
48 | /** |
||
49 | * @var AuthUserInterface |
||
50 | */ |
||
51 | public $user; |
||
52 | |||
53 | /** |
||
54 | * Reflects current working status |
||
55 | */ |
||
56 | public $status = 0; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | const ACTIVE = 1; |
||
62 | |||
63 | /** |
||
64 | * @var int |
||
65 | */ |
||
66 | const CLIENT_DATA = 2; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | const SERVER_MATCH = 4; |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * @param AuthUserInterface $user Authentication User Object |
||
77 | * @param Callable $client_authenticator Callable that returns selector and token sent with request |
||
78 | * @param Callable $client_auth_remover Callable that deletes errorneous Client Authentication |
||
79 | * @param Callable $server_authenticator Callable that accepts selector and token and returns User ID |
||
80 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
81 | */ |
||
82 | 30 | public function __construct( AuthUserInterface $user, Callable $client_authenticator, Callable $client_auth_remover, Callable $server_authenticator, LoggerInterface $logger = null) |
|
83 | { |
||
84 | 30 | $this->user = $user; |
|
85 | 30 | $this->client_authenticator = $client_authenticator; |
|
86 | 30 | $this->client_auth_remover = $client_auth_remover; |
|
87 | 30 | $this->server_authenticator = $server_authenticator; |
|
88 | |||
89 | 30 | $this->logger = $logger ?: new NullLogger; |
|
90 | 30 | } |
|
91 | |||
92 | |||
93 | /** |
||
94 | * @param Psr\Http\Message\ServerRequestInterface $request PSR7 request |
||
95 | * @param Psr\Http\Message\ResponseInterface $response PSR7 response |
||
96 | * @param callable $next Next middleware |
||
97 | * |
||
98 | * @return Psr\Http\Message\ResponseInterface |
||
99 | * |
||
100 | * @throws RuntimeException if Request has no 'user' attribute with AuthUserInterface instance. |
||
101 | */ |
||
102 | 30 | public function __invoke(Request $request, Response $response, $next) |
|
103 | { |
||
104 | |||
105 | // --------------------------------- |
||
106 | // Prerequisites |
||
107 | // --------------------------------- |
||
108 | |||
109 | 30 | View Code Duplication | if ($uid = $this->user->getId()): |
110 | 5 | $this->logger->debug("Before Route: User has ID, proceeding", [ |
|
111 | 4 | 'user_id' => $uid |
|
112 | 1 | ]); |
|
113 | // Call next middleware |
||
114 | 5 | return $next($request, $response); |
|
115 | else: |
||
116 | 25 | $this->status = $this->status | static::ACTIVE; |
|
117 | endif; |
||
118 | |||
119 | |||
120 | |||
121 | // --------------------------------- |
||
122 | // 2. Retrieve Client Authentication data |
||
123 | // do nothing else if none set. |
||
124 | // --------------------------------- |
||
125 | |||
126 | 25 | $client_authenticator = $this->client_authenticator; |
|
127 | 25 | View Code Duplication | if (!$client_authentication = $client_authenticator()): |
128 | 10 | $this->logger->debug("Before Route: No Client Authentication found"); |
|
129 | // Call next middleware |
||
130 | 10 | return $next($request, $response); |
|
131 | else: |
||
132 | 15 | $this->status = $this->status | static::CLIENT_DATA; |
|
133 | 15 | $this->logger->info("Before Route: Received Client Authentication"); |
|
134 | endif; |
||
135 | |||
136 | |||
137 | |||
138 | // --------------------------------- |
||
139 | // 3. Find User ID on Server using |
||
140 | // client selector |
||
141 | // --------------------------------- |
||
142 | |||
143 | 15 | $server_authenticator = $this->server_authenticator; |
|
144 | 15 | if (!$user_id = $server_authenticator( $client_authentication->selector, $client_authentication->token )): |
|
145 | 10 | $this->logger->warning( "Before Route: Client Authentication did not match any database entry, delete it." ); |
|
146 | |||
147 | // Delete Client authentication (cookie) |
||
148 | 10 | $client_auth_remover = $this->client_auth_remover; |
|
149 | 10 | $client_auth_remover(); |
|
150 | |||
151 | // Call next middleware |
||
152 | 10 | return $next($request, $response); |
|
153 | else: |
||
154 | 5 | $this->logger->info("Before Route: Client Auth matches revord on server"); |
|
155 | 5 | $this->status = $this->status | static::SERVER_MATCH; |
|
156 | endif; |
||
157 | |||
158 | |||
159 | |||
160 | // --------------------------------- |
||
161 | // 4. Assign User ID to user, reload page |
||
162 | // --------------------------------- |
||
163 | 5 | $this->logger->info("Before Route: Assign ID to user, reload page", [ |
|
164 | 4 | "user_id" => $user_id |
|
165 | 1 | ]); |
|
166 | |||
167 | 5 | $this->user->setId( $user_id ); |
|
168 | |||
169 | 5 | return $response->withStatus( $this->authenticated_status_code ) |
|
0 ignored issues
–
show
|
|||
170 | 5 | ->withHeader('Location', (string) $request->getUri()); |
|
171 | } |
||
172 | |||
173 | |||
174 | |||
175 | } |
||
176 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.