1 | <?php |
||
31 | class Pdo extends AbstractAdapter |
||
32 | { |
||
33 | /** |
||
34 | * @var \PDO The database connection |
||
35 | */ |
||
36 | protected $pdo; |
||
37 | /** |
||
38 | * @var string The document field containing the username |
||
39 | */ |
||
40 | protected $fieldUser; |
||
41 | /** |
||
42 | * @var string The document field containing the password |
||
43 | */ |
||
44 | protected $fieldPass; |
||
45 | /** |
||
46 | * @var string The table (and possible JOINs) from which to SELECT |
||
47 | */ |
||
48 | protected $table; |
||
49 | /** |
||
50 | * @var string Any additional WHERE parameters |
||
51 | */ |
||
52 | protected $where; |
||
53 | |||
54 | /** |
||
55 | * Creates a new PDO authentication adapter. |
||
56 | * |
||
57 | * @param \PDO $pdo The PDO driver |
||
58 | * @param string $fieldUser The document field containing the username |
||
59 | * @param string $fieldPass The document field containing the hashed password |
||
60 | * @param string $table The table (and possible JOINs) from which to SELECT |
||
61 | * @param string $where Any additional WHERE parameters (e.g. "foo = 'bar'") |
||
62 | */ |
||
63 | 5 | public function __construct(\PDO $pdo, string $fieldUser, string $fieldPass, string $table, string $where = '') |
|
71 | |||
72 | /** |
||
73 | * Authenticates the current principal using the provided credentials. |
||
74 | * |
||
75 | * This method expects two request body values to be available. These are |
||
76 | * `username` and `password`, as provided by the authenticating user. |
||
77 | * |
||
78 | * The principal details will include `ip` (remote IP address), and `ua` |
||
79 | * (remote User Agent). |
||
80 | * |
||
81 | * @param ServerRequestInterface $request The Server Request message containing credentials |
||
82 | * @return \Caridea\Auth\Principal An authenticated principal |
||
83 | * @throws \Caridea\Auth\Exception\MissingCredentials If the username or password is empty |
||
84 | * @throws \Caridea\Auth\Exception\UsernameNotFound if the provided username wasn't found |
||
85 | * @throws \Caridea\Auth\Exception\UsernameAmbiguous if the provided username matches multiple accounts |
||
86 | * @throws \Caridea\Auth\Exception\InvalidPassword if the provided password is invalid |
||
87 | * @throws \Caridea\Auth\Exception\ConnectionFailed if a PDO error is encountered |
||
88 | */ |
||
89 | 5 | public function login(ServerRequestInterface $request): \Caridea\Auth\Principal |
|
90 | { |
||
91 | 5 | $post = (array) $request->getParsedBody(); |
|
92 | 5 | $username = $this->ensure($post, 'username'); |
|
93 | try { |
||
94 | 5 | $stmt = $this->execute($username, $request); |
|
95 | 4 | $row = $this->fetchResult($stmt->fetchAll(\PDO::FETCH_NUM), $username); |
|
96 | 2 | $this->verify($this->ensure($post, 'password'), $row[1]); |
|
97 | 1 | return \Caridea\Auth\Principal::get( |
|
98 | 1 | $username, |
|
99 | 1 | $this->details($request, []) |
|
100 | ); |
||
101 | 4 | } catch (\PDOException $e) { |
|
102 | 1 | throw new \Caridea\Auth\Exception\ConnectionFailed($e); |
|
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Queries the database table. |
||
108 | * |
||
109 | * Override this method if you want to bind additonal values to the SQL |
||
110 | * query. |
||
111 | * |
||
112 | * @param string $username The username to use for parameter binding |
||
113 | * @param ServerRequestInterface $request The Server Request message (to use for additional parameter binding) |
||
114 | */ |
||
115 | 5 | protected function execute(string $username, ServerRequestInterface $request): \PDOStatement |
|
121 | |||
122 | /** |
||
123 | * Builds the SQL query to be executed. |
||
124 | * |
||
125 | * @return string The SQL query |
||
126 | */ |
||
127 | 1 | protected function getSql(): string |
|
135 | |||
136 | /** |
||
137 | * Fetches a single result from the database resultset. |
||
138 | * |
||
139 | * @param array $results The results as returned from `fetchAll` |
||
140 | * @param string $username The attempted username (for Exception purposes) |
||
141 | * @return array A single database result |
||
142 | * @throws \Caridea\Auth\Exception\UsernameAmbiguous If there is more than 1 result |
||
143 | * @throws \Caridea\Auth\Exception\UsernameNotFound If there are 0 results |
||
144 | */ |
||
145 | 4 | protected function fetchResult(array $results, string $username): array |
|
154 | } |
||
155 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.