PageRenderTime 102ms CodeModel.GetById 64ms app.highlight 3ms RepoModel.GetById 33ms app.codeStats 0ms

/lib/Sabre/DAV/Auth/Backend/AbstractDigest.php

https://github.com/KOLANICH/SabreDAV
PHP | 101 lines | 37 code | 19 blank | 45 comment | 5 complexity | 5046461909f3427e3c8983bab9966e10 MD5 | raw file
  1<?php
  2
  3namespace Sabre\DAV\Auth\Backend;
  4
  5use Sabre\HTTP;
  6use Sabre\DAV;
  7
  8/**
  9 * HTTP Digest authentication backend class
 10 *
 11 * This class can be used by authentication objects wishing to use HTTP Digest
 12 * Most of the digest logic is handled, implementors just need to worry about
 13 * the getDigestHash method
 14 *
 15 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
 16 * @author Evert Pot (http://evertpot.com/)
 17 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 18 */
 19abstract class AbstractDigest implements BackendInterface {
 20
 21    /**
 22     * This variable holds the currently logged in username.
 23     *
 24     * @var array|null
 25     */
 26    protected $currentUser;
 27
 28    /**
 29     * Returns a users digest hash based on the username and realm.
 30     *
 31     * If the user was not known, null must be returned.
 32     *
 33     * @param string $realm
 34     * @param string $username
 35     * @return string|null
 36     */
 37    abstract public function getDigestHash($realm, $username);
 38
 39    /**
 40     * Authenticates the user based on the current request.
 41     *
 42     * If authentication is successful, true must be returned.
 43     * If authentication fails, an exception must be thrown.
 44     *
 45     * @param DAV\Server $server
 46     * @param string $realm
 47     * @throws DAV\Exception\NotAuthenticated
 48     * @return bool
 49     */
 50    public function authenticate(DAV\Server $server, $realm) {
 51
 52        $digest = new HTTP\DigestAuth();
 53
 54        // Hooking up request and response objects
 55        $digest->setHTTPRequest($server->httpRequest);
 56        $digest->setHTTPResponse($server->httpResponse);
 57
 58        $digest->setRealm($realm);
 59        $digest->init();
 60
 61        $username = $digest->getUsername();
 62
 63        // No username was given
 64        if (!$username) {
 65            $digest->requireLogin();
 66            throw new DAV\Exception\NotAuthenticated('No digest authentication headers were found');
 67        }
 68
 69        $hash = $this->getDigestHash($realm, $username);
 70        // If this was false, the user account didn't exist
 71        if ($hash===false || is_null($hash)) {
 72            $digest->requireLogin();
 73            throw new DAV\Exception\NotAuthenticated('The supplied username was not on file');
 74        }
 75        if (!is_string($hash)) {
 76            throw new DAV\Exception('The returned value from getDigestHash must be a string or null');
 77        }
 78
 79        // If this was false, the password or part of the hash was incorrect.
 80        if (!$digest->validateA1($hash)) {
 81            $digest->requireLogin();
 82            throw new DAV\Exception\NotAuthenticated('Incorrect username');
 83        }
 84
 85        $this->currentUser = $username;
 86        return true;
 87
 88    }
 89
 90    /**
 91     * Returns the currently logged in username.
 92     *
 93     * @return string|null
 94     */
 95    public function getCurrentUser() {
 96
 97        return $this->currentUser;
 98
 99    }
100
101}