Overview

Namespaces

  • Authoritarian
    • Exception
      • Flow
    • Flow
  • PHP

Classes

  • AbstractFlow
  • AuthorizationCodeFlow
  • ClientCredentialsFlow
  • ResourceOwnerPasswordFlow
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Authoritarian\Flow;
  4: 
  5: use Authoritarian\Exception\Flow\MissingAuthorizationCodeException;
  6: use Authoritarian\Credential\ClientCredential;
  7: 
  8: /**
  9:  * Implementation of Authorization Code Flow
 10:  **/
 11: class AuthorizationCodeFlow extends AbstractFlow
 12: {
 13:     const GRANT_TYPE = 'authorization_code';
 14:     const RESPONSE_TYPE = 'code';
 15: 
 16:     protected $authorizationUrl;
 17:     protected $code;
 18:     protected $redirectUri;
 19:     protected $state;
 20:     protected $parameters;
 21: 
 22:     /**
 23:      * @param string $authorization_url OAuth 2's Authorization endpoint url
 24:      */
 25:     public function __construct($authorization_url = null)
 26:     {
 27:         $this->setAuthorizationUrl($authorization_url);
 28:     }
 29: 
 30:     /**
 31:      * @param string $authorization_url OAuth 2's Authorization endpoint url
 32:      */
 33:     public function setAuthorizationUrl($authorization_url)
 34:     {
 35:         $this->authorizationUrl = $authorization_url;
 36:     }
 37: 
 38:     /**
 39:      * @param string $code The authorization code retrieved in the callback page
 40:      */
 41:     public function setCode($code)
 42:     {
 43:         $this->code = $code;
 44:     }
 45: 
 46:     /**
 47:      * @param string $uri the callback URI to retrieve the authorization code
 48:      */
 49:     public function setRedirectUri($uri)
 50:     {
 51:         $this->redirectUri = $uri;
 52:     }
 53: 
 54:     /**
 55:      * @param string $state The app's state to be resumed at the callback
 56:      */
 57:     public function setState($state)
 58:     {
 59:         $this->state = $state;
 60:     }
 61: 
 62:     /**
 63:      * Get the URL to user's authentication and authorization
 64:      *
 65:      * @return string
 66:      */
 67:     public function getAuthUrl()
 68:     {
 69:         return $this->authorizationUrl . '?' . $this->getAuthorizeQueryParameters();
 70:     }
 71: 
 72:     /**
 73:      * {@inheritDoc}
 74:      * @throws MissingAuthorizationCodeException When the authorization code
 75:      * wasn't set
 76:      */
 77:     public function getRequest()
 78:     {
 79:         parent::getRequest();
 80: 
 81:         if (is_null($this->code)) {
 82:             throw new MissingAuthorizationCodeException(
 83:                 'No authorization code given to generate a request'
 84:             );
 85:         }
 86: 
 87:         return $this->client->post(
 88:             $this->tokenUrl,
 89:             null,
 90:             $this->getRequestPostParameters()
 91:         );
 92:     }
 93: 
 94:     private function getRequestPostParameters()
 95:     {
 96:         $parameters = array(
 97:             'code' => $this->code,
 98:             'client_id' => $this->clientId,
 99:             'client_secret' => $this->clientSecret,
100:             'grant_type' => self::GRANT_TYPE,
101:             'redirect_uri' => $this->redirectUri,
102:             'scope' => $this->scope,
103:         );
104: 
105:         return $this->removeNullItems($parameters);
106:     }
107: 
108:     private function getAuthorizeQueryParameters()
109:     {
110:         $parameters = array(
111:             'redirect_uri' => $this->redirectUri,
112:             'client_id' => $this->clientId,
113:             'response_type' => self::RESPONSE_TYPE,
114:             'scope' => $this->scope,
115:             'state' => $this->state,
116:         );
117: 
118:         return http_build_query(
119:             $this->removeNullItems($parameters)
120:         );
121:     }
122: }
123: 
API documentation generated by ApiGen 2.8.0