Completed
Push — master ( 2e9783...8d84bc )
by Raffael
02:19
created

AbstractBasic::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * Auth
68
     *
69
     * @param   string $username
70
     * @param   string $password
71
     * @return  bool
72
     */
73
    protected function plainAuth(string $username, string $password): bool
74
    {
75
        $result = $this->findIdentity($username);
76
77
        if ($result === null) {
78
            $this->logger->info('found no user named ['.$username.'] in database', [
79
                'category' => get_class($this)
80
            ]);
81
82
            return false;
83
        }
84
85
        if (!isset($result['password']) || empty($result['password'])) {
86
            $this->logger->info('found no password for ['.$username.'] in database', [
87
                'category' => get_class($this)
88
            ]);
89
         
90
            return false;
91
        }
92
93
        if (!password_verify($password, $result['password'])) {
94
            $this->logger->info('failed match given password for ['.$username.'] with stored hash in database', [
95
                'category' => get_class($this)
96
            ]);
97
         
98
            return false;
99
        }
100
101
        $this->attributes = $result;        
102
        $this->identifier = $username;
103
        return true;
104
    }
105
106
107
    /**
108
     * Find Identity
109
     *
110
     * @param  string $username
111
     * @return array
112
     */
113
    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...
114
115
116
    /**
117
     * Get attributes
118
     * 
119
     * @return array
120
     */
121
    public function getAttributes(): array 
122
    {
123
        return $this->attributes;
124
    }
125
}
126