Skip to content

Instantly share code, notes, and snippets.

@ludofleury
Created August 24, 2012 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludofleury/3456755 to your computer and use it in GitHub Desktop.
Save ludofleury/3456755 to your computer and use it in GitHub Desktop.
OAuth2 bearer token
// because php sucks at providing custom headers...
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
$this->headers->set('Authorization', $headers['Authorization']);
}
<?php
namespace Kutio\Security\OAuth2;
trait Request
{
/**
* An OAuth access token object
*
* @var OAuth2\AccessInterface
*/
private $access;
public function isAuthorized()
{
return true;
}
public function hasScope($scope)
{
return $this->access->hasScope($scope);
}
public function setAccess(AccessInterface $access)
{
$this->access = $access;
}
public function getAccess()
{
return $this->access;
}
/**
* Return the OAuth token provided in the Authorization header
*
* @param string $type The OAuth2 access token type expected (Bearer, HMAC)
*
* @return string|null The OAuth2 access token or NULL
*/
public function getOAuthToken($type = 'Bearer')
{
$oauthToken = null;
$base64pattern = '(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})';
if (preg_match('#^'.$type.' ('.$base64pattern.')$#', $this->headers->get('Authorization'), $matches)) {
$oauthToken = $matches[1];
}
return $oauthToken;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment