Completed
Push — master ( 1e88b2...6b6b05 )
by Boy
03:57
created

JsonBasicAuthenticationEntryPoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A start() 0 13 2
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