Completed
Push — master ( a51466...8daf75 )
by Raffael
02:47
created

AbstractBasic::authenticate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Micro
6
 *
7
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
8
 * @license   MIT https://opensource.org/licenses/MIT
9
 */
10
11
namespace Micro\Auth\Adapter\Basic;
12
13
use \Micro\Auth\Exception;
14
use \Psr\Log\LoggerInterface as Logger;
15
use \Micro\Auth\Adapter\AdapterInterface;
16
use \Micro\Auth\Adapter\AbstractAdapter;
17
18
abstract class AbstractBasic extends AbstractAdapter
19
{
20
    /**
21
     * Authenticate
22
     *
23
     * @return bool
24
     */
25
    public function authenticate(): bool
26
    {
27
        if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
28
            $this->logger->debug('skip auth adapter ['.get_class($this).'], no http authorization header found', [
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
                'category' => get_class($this)
30
            ]);
31
        
32
            return false;
33
        }
34
35
        $header = $_SERVER['HTTP_AUTHORIZATION'];
36
        $parts  = explode(' ', $header);
37
        
38
        if ($parts[0] == 'Basic') {
39
            $this->logger->debug('found http basic authorization header', [
40
                'category' => get_class($this)
41
            ]);
42
43
            $username = $_SERVER['PHP_AUTH_USER'];
44
            $password = $_SERVER['PHP_AUTH_PW'];
45
46
            return $this->plainAuth($username, $password);
47
        } else {
48
            $this->logger->warning('http authorization header contains no basic string or invalid authentication string', [
49
                'category' => get_class($this)
50
            ]);
51
        
52
            return false;
53
        }
54
    }
55
56
57
    /**
58
     * Auth
59
     *
60
     * @param   string $username
61
     * @param   string $password
62
     * @return  bool
63
     */
64
    protected function plainAuth(string $username, string $password): bool
65
    {
66
        $result = $this->findIdentity($username);
67
68
        if ($result === null) {
69
            $this->logger->info('found no user named ['.$username.'] in database', [
70
                'category' => get_class($this)
71
            ]);
72
73
            return false;
74
        }
75
        
76
        if (!isset($result['password']) || empty($result['password'])) {
77
            $this->logger->info('found no password for ['.$username.'] in database', [
78
                'category' => get_class($this)
79
            ]);
80
         
81
            return false;
82
        }
83
84
        if (!password_verify($password, $result['password'])) {
85
            $this->logger->info('failed match given password for ['.$username.'] with stored hash in database', [
86
                'category' => get_class($this)
87
            ]);
88
         
89
            return false;
90
        }
91
92
        $this->identifier  = $username;
93
        return true;
94
    }
95
96
97
    /**
98
     * Find Identity
99
     *
100
     * @param  string $username
101
     * @return array
102
     */
103
    protected abstract function findIdentity(string $username): ?array;
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
104
}
105