1
|
|
|
<?php namespace Neomerx\Limoncello\Http\Middleware; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use \Closure; |
20
|
|
|
use \Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use \Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use \Neomerx\Limoncello\Contracts\IntegrationInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Neomerx\Limoncello |
26
|
|
|
*/ |
27
|
|
|
abstract class BaseAuthMiddleware |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Authentication scheme. Child classes should override this constant. |
31
|
|
|
*/ |
32
|
|
|
const AUTHENTICATION_SCHEME = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Authorization header. |
36
|
|
|
*/ |
37
|
|
|
const HEADER_AUTHORIZATION = 'Authorization'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* WWW authenticate header. |
41
|
|
|
*/ |
42
|
|
|
const HEADER_WWW_AUTHENTICATE = 'WWW-Authenticate'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var IntegrationInterface |
46
|
|
|
*/ |
47
|
|
|
private $integration; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Closure |
51
|
|
|
*/ |
52
|
|
|
private $authenticationClosure; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Closure|null |
56
|
|
|
*/ |
57
|
|
|
private $authorizationClosure; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|null |
61
|
|
|
*/ |
62
|
|
|
private $realm; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor. |
66
|
|
|
* |
67
|
|
|
* @param IntegrationInterface $integration |
68
|
|
|
* @param Closure $authenticateClosure |
69
|
|
|
* @param Closure|null $authorizeClosure |
70
|
|
|
* @param string|null $realm |
71
|
|
|
*/ |
72
|
7 |
|
public function __construct( |
73
|
|
|
IntegrationInterface $integration, |
74
|
|
|
Closure $authenticateClosure, |
75
|
|
|
Closure $authorizeClosure = null, |
76
|
|
|
$realm = null |
77
|
|
|
) { |
78
|
7 |
|
$this->realm = $realm; |
79
|
7 |
|
$this->integration = $integration; |
80
|
7 |
|
$this->authenticationClosure = $authenticateClosure; |
81
|
7 |
|
$this->authorizationClosure = $authorizeClosure; |
82
|
7 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Request $request |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
abstract protected function authenticate(Request $request); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Handle an incoming request. |
93
|
|
|
* |
94
|
|
|
* @param Request $request |
95
|
|
|
* @param Closure $next |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
7 |
|
public function handle(Request $request, Closure $next) |
100
|
|
|
{ |
101
|
7 |
|
$isAuthenticated = $this->authenticate($request); |
102
|
7 |
|
$isAuthorized = $isAuthenticated === true ? $this->authorize($request) : $isAuthenticated; |
103
|
|
|
|
104
|
7 |
|
return $isAuthorized === true ? $next($request) : $this->getUnauthorizedResponse(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get response for invalid authentication credentials. |
109
|
|
|
* |
110
|
|
|
* @return Response |
111
|
|
|
*/ |
112
|
1 |
|
protected function getUnauthorizedResponse() |
113
|
|
|
{ |
114
|
1 |
|
$authHeaderValue = $this->realm === null ? static::AUTHENTICATION_SCHEME : |
115
|
1 |
|
static::AUTHENTICATION_SCHEME . ' realm="' . $this->realm . '"'; |
116
|
|
|
|
117
|
1 |
|
return $this->integration->createResponse( |
118
|
1 |
|
null, |
119
|
1 |
|
Response::HTTP_UNAUTHORIZED, |
120
|
1 |
|
[self::HEADER_WWW_AUTHENTICATE => $authHeaderValue] |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Request $request |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
7 |
|
protected function authorize(Request $request) |
130
|
|
|
{ |
131
|
7 |
|
$isAuthorized = true; |
132
|
|
|
|
133
|
|
|
// if authorization is required check it as well |
134
|
7 |
|
if (($authorizationClosure = $this->getAuthorizationClosure()) !== null) { |
135
|
2 |
|
$isAuthorized = $authorizationClosure($request); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
7 |
|
return $isAuthorized; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Closure|null |
143
|
|
|
*/ |
144
|
7 |
|
protected function getAuthorizationClosure() |
145
|
|
|
{ |
146
|
7 |
|
return $this->authorizationClosure; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Closure |
151
|
|
|
*/ |
152
|
7 |
|
protected function getAuthenticationClosure() |
153
|
|
|
{ |
154
|
7 |
|
return $this->authenticationClosure; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|