1<?php
2
3namespace Sabre\HTTP\Auth;
4
5/**
6 * HTTP Bearer authentication utility.
7 *
8 * This class helps you setup bearer auth. The process is fairly simple:
9 *
10 * 1. Instantiate the class.
11 * 2. Call getToken (this will return null or a token as string)
12 * 3. If you didn't get a valid token, call 'requireLogin'
13 *
14 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
15 * @author François Kooman (fkooman@tuxed.net)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class Bearer extends AbstractAuth {
19
20    /**
21     * This method returns a string with an access token.
22     *
23     * If no token was found, this method returns null.
24     *
25     * @return null|string
26     */
27    function getToken() {
28
29        $auth = $this->request->getHeader('Authorization');
30
31        if (!$auth) {
32            return null;
33        }
34
35        if (strtolower(substr($auth, 0, 7)) !== 'bearer ') {
36            return null;
37        }
38
39        return substr($auth, 7);
40
41    }
42
43    /**
44     * This method sends the needed HTTP header and statuscode (401) to force
45     * authentication.
46     *
47     * @return void
48     */
49    function requireLogin() {
50
51        $this->response->addHeader('WWW-Authenticate', 'Bearer realm="' . $this->realm . '"');
52        $this->response->setStatus(401);
53
54    }
55
56}
57