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;
}