1<?php
2
3namespace Sabre\HTTP\Auth;
4
5/**
6 * HTTP Basic authentication utility.
7 *
8 * This class helps you setup basic auth. The process is fairly simple:
9 *
10 * 1. Instantiate the class.
11 * 2. Call getCredentials (this will return null or a user/pass pair)
12 * 3. If you didn't get valid credentials, call 'requireLogin'
13 *
14 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
15 * @author Evert Pot (http://evertpot.com/)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class Basic extends AbstractAuth {
19
20    /**
21     * This method returns a numeric array with a username and password as the
22     * only elements.
23     *
24     * If no credentials were found, this method returns null.
25     *
26     * @return null|array
27     */
28    function getCredentials() {
29
30        $auth = $this->request->getHeader('Authorization');
31
32        if (!$auth) {
33            return null;
34        }
35
36        if (strtolower(substr($auth, 0, 6)) !== 'basic ') {
37            return null;
38        }
39
40        $credentials = explode(':', base64_decode(substr($auth, 6)), 2);
41
42        if (2 !== count($credentials)) {
43            return null;
44        }
45
46        return $credentials;
47
48    }
49
50    /**
51     * This method sends the needed HTTP header and statuscode (401) to force
52     * the user to login.
53     *
54     * @return void
55     */
56    function requireLogin() {
57
58        $this->response->addHeader('WWW-Authenticate', 'Basic realm="' . $this->realm . '"');
59        $this->response->setStatus(401);
60
61    }
62
63}
64