BasicAuthMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B authenticate() 0 26 4
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 \Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * @package Neomerx\Limoncello
23
 */
24
class BasicAuthMiddleware extends BaseAuthMiddleware
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    const AUTHENTICATION_SCHEME = 'Basic';
30
31
    /**
32
     * @inheritdoc
33
     */
34 6
    protected function authenticate(Request $request)
35
    {
36 6
        $isAuthenticated = false;
37
38 6
        $authHeader = $request->headers->get(self::HEADER_AUTHORIZATION);
39
        // 6 is a length of 'Basic ' in Basic Auth header
40 6
        $idAndPassInBase64 = substr($authHeader, 6);
41 6
        if (empty($idAndPassInBase64) === false) {
42 6
            $idAndPassDecoded = base64_decode($idAndPassInBase64);
43 6
            if (is_string($idAndPassDecoded) === true) {
44
                // RFC2617 #2 says user name can't contain ':' thus we have to split by the first ':'
45 6
                $splitPos = strpos($idAndPassDecoded, ':');
46 6
                if ($splitPos !== false) {
47 6
                    $userId = substr($idAndPassDecoded, 0, $splitPos);
48
                    // if $splitPos is the last character (substr returns false) it will be empty string
49 6
                    $password = (string)substr($idAndPassDecoded, $splitPos + 1);
50
51
                    // check user authentication (login and password)
52 6
                    $authenticateClosure = $this->getAuthenticationClosure();
53 6
                    $isAuthenticated = $authenticateClosure($userId, $password);
54 6
                }
55 6
            }
56 6
        }
57
58 6
        return $isAuthenticated;
59
    }
60
}
61