AbstractBasic   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B authenticate() 0 30 3
A getAttributes() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
/**
5
 * Micro
6
 *
7
 * @author    Raffael Sahli <[email protected]>
8
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
9
 * @license   MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Auth\Adapter\Basic;
13
14
use \Micro\Auth\Exception;
15
use \Psr\Log\LoggerInterface as Logger;
16
use \Micro\Auth\Adapter\AdapterInterface;
17
use \Micro\Auth\Adapter\AbstractAdapter;
18
19
abstract class AbstractBasic extends AbstractAdapter
20
{
21
    /**
22
     * Attributes
23
     *
24
     * @var array
25
     */
26
    protected $attributes = [];
27
28
29
    /**
30
     * Authenticate
31
     *
32
     * @return bool
33
     */
34
    public function authenticate(): bool
35
    {
36
        if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
37
            $this->logger->debug('skip auth adapter ['.get_class($this).'], no http authorization header found', [
38
                'category' => get_class($this)
39
            ]);
40
41
            return false;
42
        }
43
44
        $header = $_SERVER['HTTP_AUTHORIZATION'];
45
        $parts  = explode(' ', $header);
46
47
        if ($parts[0] == 'Basic') {
48
            $this->logger->debug('found http basic authorization header', [
49
                'category' => get_class($this)
50
            ]);
51
52
            $username = $_SERVER['PHP_AUTH_USER'];
53
            $password = $_SERVER['PHP_AUTH_PW'];
54
55
            return $this->plainAuth($username, $password);
56
        } else {
57
            $this->logger->warning('http authorization header contains no basic string or invalid authentication string', [
58
                'category' => get_class($this)
59
            ]);
60
61
            return false;
62
        }
63
    }
64
65
66
    /**
67
     * Get attributes
68
     *
69
     * @return array
70
     */
71
    public function getAttributes(): array
72
    {
73
        return $this->attributes;
74
    }
75
}
76