JsonBasicAuthenticationEntryPoint   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A start() 0 9 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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Security\Http\EntryPoint;
20
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Security\Core\Exception\AuthenticationException;
25
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
26
27
/**
28
 * JsonBasicAuthenticationEntryPoint starts an HTTP Basic authentication with a JSON response body.
29
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
30
class JsonBasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface
31
{
32
    public function __construct(private string $realmName)
33
    {
34
    }
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $authException should have a doc-comment as per coding-style.
Loading history...
37
     * {@inheritdoc}
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public function start(Request $request, AuthenticationException $authException = null): Response
40
    {
41
        $authExceptionMessage = $authException instanceof AuthenticationException ? $authException->getMessage() : '';
42
        $error = sprintf('You are required to authorise before accessing this API (%s).', $authExceptionMessage);
43
44
        return new JsonResponse(
45
            ['errors' => [$error]],
46
            Response::HTTP_UNAUTHORIZED,
47
            ['WWW-Authenticate' => sprintf('Basic realm="%s"', $this->realmName)],
48
        );
49
    }
50
}
51