1 <?php
2
3 namespace Http\Message\Authentication;
4
5 use Http\Message\Authentication;
6 use Psr\Http\Message\RequestInterface;
7
8 9 10 11 12
13 final class BasicAuth implements Authentication
14 {
15 16 17
18 private $username;
19
20 21 22
23 private $password;
24
25 26 27 28
29 public function __construct($username, $password)
30 {
31 $this->username = $username;
32 $this->password = $password;
33 }
34
35 36 37
38 public function authenticate(RequestInterface $request)
39 {
40 $header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->username, $this->password)));
41
42 return $request->withHeader('Authorization', $header);
43 }
44 }
45