Repository navigation
Expand file tree
/
Copy pathHawk.php
More file actions
191 lines (159 loc) · 7.08 KB
/
Copy pathHawk.php
File metadata and controls
191 lines (159 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Dflydev\Stack;
use Dflydev\Hawk\Credentials\CredentialsInterface;
use Dflydev\Hawk\Credentials\CredentialsProviderInterface;
use Dflydev\Hawk\Crypto\Crypto;
use Dflydev\Hawk\Header\HeaderFactory;
use Dflydev\Hawk\Server\ServerBuilder;
use Dflydev\Hawk\Server\UnauthorizedException;
use Pimple;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class Hawk implements HttpKernelInterface
{
private $app;
private $container;
public function __construct(HttpKernelInterface $app, array $options = array())
{
$this->app = $app;
$this->container = $this->setupContainer($options);
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
// The challenge callback is called if a 401 response is detected that
// has a "WWW-Authenticate: Stack" header. This is per the Stack
// Authentication and Authorization proposals. It is passed the existing
// response object.
$challenge = function (Response $response) {
$response->headers->set('WWW-Authenticate', 'Hawk');
return $response;
};
// The authenticate callback is called if the request has no Stack
// authentication token but there is an authorization header. It is
// passed an app we should delegate to (assuming we do not return
// beforehand) and a boolean value indicating whether or not anonymous
// requests should be allowed.
$authenticate = function ($app, $anonymous) use ($request, $type, $catch, $challenge) {
try {
$header = HeaderFactory::createFromString(
'Authorization',
$request->headers->get('authorization')
);
} catch (NotHawkAuthorizationException $e) {
if ($anonymous) {
// This is not a Hawk request but the firewall allows
// anonymous requests so we should wrap the application
// so that we might be able to challenge if authorization
// fails.
return (new WwwAuthenticateStackChallenge($app, $challenge))
->handle($request, $type, $catch);
}
// Anonymous requests are not allowed so we should challenge
// immediately.
return call_user_func($challenge, (new Response)->setStatusCode(401));
} catch (FieldValueParserException $e) {
// Something horribly wrong has happened.
return (new Response)->setStatusCode(400);
}
try {
$payload = $this->container['validate_payload_request']
? ($request->getMethod() !== 'GET' ? $request->getContent() : null)
: null;
$authenticationResponse = $this->container['server']->authenticate(
$request->getMethod(),
$request->getHost(),
$request->getPort(),
$request->getRequestUri(),
$request->headers->get('content-type'),
$payload,
$header
);
} catch (UnauthorizedException $e) {
$response = (new Response)->setStatusCode(401);
$header = $e->getHeader();
$response->headers->set($header->fieldName(), $header->fieldValue());
return $response;
}
// Stack authentication compatibility.
$request->attributes->set(
'stack.authn.token',
$this->container['token_translator']($authenticationResponse->credentials())
);
// Hawk specific information
$request->attributes->set('hawk.credentials', $authenticationResponse->credentials());
$request->attributes->set('hawk.artifacts', $authenticationResponse->artifacts());
$response = $app->handle($request, $type, $catch);
if ($this->container['sign_response']) {
$options = [];
if ($this->container['validate_payload_response']) {
$options['payload'] = $response->getContent();
$options['content_type'] = $response->headers->get('content-type');
}
$header = $this->container['server']->createHeader(
$authenticationResponse->credentials(),
$authenticationResponse->artifacts(),
$options
);
$response->headers->set($header->fieldName(), $header->fieldValue());
}
return $response;
};
return (new Firewall($this->app, [
'challenge' => $challenge,
'authenticate' => $authenticate,
'firewall' => $this->container['firewall'],
]))
->handle($request, $type, $catch);
}
private function setupContainer(array $options = array())
{
if (!isset($options['credentials_provider'])) {
throw new \RuntimeException("No 'credentials_provider' callback or service specified");
}
if ($options['credentials_provider'] instanceof CredentialsProviderInterface ||
is_callable($options['credentials_provider'])) {
$credentialsProvider = $options['credentials_provider'];
} else {
throw new \InvalidArgumentException(
"The 'credentials_provider' must either be an instance of CredentialsProviderInterface " .
"or it must be callable"
);
}
unset($options['credentials_provider']);
$c = new Pimple([
'sign_response' => true,
'validate_payload_response' => true,
'validate_payload_request' => true,
'firewall' => [],
]);
$c['crypto'] = $c->share(function () {
return new Crypto;
});
$c['server'] = $c->share(function () use ($c, $credentialsProvider) {
$builder = (new ServerBuilder($credentialsProvider))
->setCrypto($c['crypto']);
if (isset($c['time_provider'])) {
$builder->setTimeProvider($c['time_provider']);
}
return $builder->build();
});
$c['token_translator'] = $c->protect(function (CredentialsInterface $credentials) {
return $credentials->id();
});
foreach ($options as $name => $value) {
if (in_array($name, ['crypto', 'server', 'time_provider', 'token_translator'])) {
if (is_callable($value)) {
$c[$name] = $c->share($value);
} else {
$c[$name] = $c->share(function () use ($value) {
return $value;
});
}
continue;
}
$c[$name] = $value;
}
return $c;
}
}