Skip to content

Commit

Permalink
Added helper class for HTTP basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Oct 7, 2012
1 parent 693c250 commit 00bf7aa
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/Sabre/HTTP/Auth/Basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Sabre\HTTP\Auth;

use Sabre\HTTP\Request;
use Sabre\HTTP\Response;

/**
* HTTP Basic authentication utility.
*
* This class helps you setup basic auth. The process is fairly simple:
*
* 1. Instantiate the class.
* 2. Call getCredentials (this will return null or a user/pass pair)
* 3. If you didn't get valid credentials, call 'requireLogin'
*
* @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
* @author Evert Pot (http://www.rooftopsolutions.nl/)
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
*/
class Basic {

/**
* Authentication realm
*
* @var string
*/
protected $realm;

/**
* Creates the basic auth helper.
*
* @param string $realm
* @return void
*/
public function __construct($realm = 'SabreTooth') {

$this->realm = $realm;

}

/**
* This method returns a numeric array with a username and password as the
* only elements.
*
* If no credentials were found, this method returns null.
*
* @param Sabre\HTTP\Request $request
* @return null|array
*/
public function getCredentials(Request $request) {

$auth = $request->getHeader('Authorization');

if (!$auth) {
return null;
}

if (strtolower(substr($auth,0,6))!=='basic ') {
return null;
}

return explode(':',base64_decode(substr($auth, 6)), 2);

}

/**
* This method sends the needed HTTP header and statuscode (401) to force
* the user to login.
*
* @param Sabre\HTTP\Response
* @return void
*/
public function requireLogin(Response $response) {

$response->setHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
$response->setStatus(401);

}

}
57 changes: 57 additions & 0 deletions tests/Sabre/HTTP/Auth/BasicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Sabre\HTTP\Auth;

use Sabre\HTTP\Request;
use Sabre\HTTP\Response;

class BasicTest extends \PHPUnit_Framework_TestCase {

function testGetCredentials() {

$request = new Request('GET','/',array(
'Authorization' => 'Basic ' . base64_encode('user:pass:bla')
));

$basic = new Basic('Dagger');

$this->assertEquals(array(
'user',
'pass:bla',
), $basic->getCredentials($request));

}

function testGetCredentialsNoheader() {

$request = new Request('GET','/',array());
$basic = new Basic('Dagger');

$this->assertNull($basic->getCredentials($request));

}

function testGetCredentialsNotBasic() {

$request = new Request('GET','/',array(
'Authorization' => 'QBasic ' . base64_encode('user:pass:bla')
));
$basic = new Basic('Dagger');

$this->assertNull($basic->getCredentials($request));

}

function testRequireLogin() {

$response = new Response();
$basic = new Basic('Dagger');

$basic->requireLogin($response);

$this->assertEquals('Basic realm="Dagger"', $response->getHeader('WWW-Authenticate'));
$this->assertEquals('401 Unauthorized', $response->getStatus());

}

}

0 comments on commit 00bf7aa

Please sign in to comment.