ludofleury
8/24/2012 - 10:52 PM

OAuth2 bearer token

OAuth2 bearer token

<?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;
    }
}
// because php sucks at providing custom headers...
        $headers = apache_request_headers();

        if (isset($headers['Authorization'])) {
            $this->headers->set('Authorization', $headers['Authorization']);
        }