fkooman\OAuth\Client\Api::getAuthorizeUri PHP Method

getAuthorizeUri() public method

public getAuthorizeUri ( Context $context, $stateValue = null )
$context Context
    public function getAuthorizeUri(Context $context, $stateValue = null)
    {
        // allow caller to override a random generated state
        // FIXME: is this actually used anywhere?
        if (null === $stateValue) {
            $stateValue = bin2hex(openssl_random_pseudo_bytes(self::RANDOM_LENGTH));
        } else {
            if (!is_string($stateValue) || 0 >= strlen($stateValue)) {
                throw new ApiException('state must be a non-empty string');
            }
        }
        // try to get a new access token
        $this->tokenStorage->deleteStateForContext($this->clientConfigId, $context);
        $state = new State(array('client_config_id' => $this->clientConfigId, 'user_id' => $context->getUserId(), 'scope' => $context->getScope(), 'issue_time' => time(), 'state' => $stateValue));
        if (false === $this->tokenStorage->storeState($state)) {
            throw new ApiException('unable to store state');
        }
        $q = array('client_id' => $this->clientConfig->getClientId(), 'response_type' => 'code', 'state' => $state->getState());
        // scope
        $contextScope = $context->getScope();
        if (!$contextScope->isEmpty()) {
            if ($this->clientConfig->getUseCommaSeparatedScope()) {
                $q['scope'] = $contextScope->toString(',');
            } else {
                $q['scope'] = $contextScope->toString();
            }
        }
        // redirect_uri
        if ($this->clientConfig->getRedirectUri()) {
            $q['redirect_uri'] = $this->clientConfig->getRedirectUri();
        }
        $separator = false === strpos($this->clientConfig->getAuthorizeEndpoint(), '?') ? '?' : '&';
        $authorizeUri = $this->clientConfig->getAuthorizeEndpoint() . $separator . http_build_query($q, null, '&');
        return $authorizeUri;
    }

Usage Example

Example #1
0
 public function testGetAccessTokenWithoutToken()
 {
     $client = new Client();
     $mock = new MockPlugin();
     $mock->addResponse(new Response(200));
     $client->addSubscriber($mock);
     $api = new Api("foo", $this->clientConfig[0], $this->storage, $client);
     $context = new Context("a_user", array("foo", "bar"));
     $this->assertFalse($api->getAccessToken($context));
     $this->assertEquals("http://www.example.org/authorize?client_id=foo&response_type=code&state=my_custom_state&scope=bar+foo", $api->getAuthorizeUri($context, "my_custom_state"));
 }
All Usage Examples Of fkooman\OAuth\Client\Api::getAuthorizeUri