1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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
|
|
|
namespace Surfnet\StepupGateway\ApiBundle\Security\Http\EntryPoint; |
20
|
|
|
|
21
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
24
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* JsonBasicAuthenticationEntryPoint starts an HTTP Basic authentication with a JSON response body. |
28
|
|
|
*/ |
29
|
|
|
class JsonBasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface |
30
|
|
|
{ |
31
|
|
|
private $realmName; |
32
|
|
|
|
33
|
|
|
public function __construct($realmName) |
34
|
|
|
{ |
35
|
|
|
$this->realmName = $realmName; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
42
|
|
|
{ |
43
|
|
|
$authExceptionMessage = $authException ? $authException->getMessage() : ''; |
44
|
|
|
$error = sprintf('You are required to authorise before accessing this API (%s).', $authExceptionMessage); |
45
|
|
|
|
46
|
|
|
$response = new JsonResponse( |
47
|
|
|
['errors' => [$error]], |
48
|
|
|
401, |
49
|
|
|
['WWW-Authenticate' => sprintf('Basic realm="%s"', $this->realmName)] |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return $response; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|