26 if (!isset(
$config[
'issuer'])) {
27 throw new \LogicException(
'config parameter "issuer" must be set');
29 $this->config = array_merge(array(
30 'id_lifetime' => 3600,
37 $result = array(
'query' => array());
38 $params += array(
'scope' =>
null,
'state' =>
null,
'nonce' =>
null);
41 list($user_id, $auth_time) = $this->getUserIdAndAuthTime($userInfo);
42 $userClaims = $this->userClaimsStorage->getUserClaims($user_id, $params[
'scope']);
44 $id_token = $this->
createIdToken($params[
'client_id'], $userInfo, $params[
'nonce'], $userClaims,
null);
45 $result[
"fragment"] = array(
'id_token' => $id_token);
46 if (isset($params[
'state'])) {
47 $result[
"fragment"][
"state"] = $params[
'state'];
50 return array($params[
'redirect_uri'], $result);
53 public function createIdToken($client_id, $userInfo, $nonce =
null, $userClaims =
null, $access_token =
null)
56 list($user_id, $auth_time) = $this->getUserIdAndAuthTime($userInfo);
59 'iss' => $this->config[
'issuer'],
63 'exp' =>
time() + $this->config[
'id_lifetime'],
64 'auth_time' => $auth_time,
68 $token[
'nonce'] = $nonce;
72 $token += $userClaims;
76 $token[
'at_hash'] = $this->
createAtHash($access_token, $client_id);
85 $algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);
86 $hash_algorithm =
'sha' . substr($algorithm, 2);
87 $hash = hash($hash_algorithm, $access_token);
88 $at_hash = substr(
$hash, 0, strlen(
$hash) / 2);
90 return $this->encryptionUtil->urlSafeB64Encode($at_hash);
93 protected function encodeToken(array $token, $client_id =
null)
95 $private_key = $this->publicKeyStorage->getPrivateKey($client_id);
96 $algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);
98 return $this->encryptionUtil->encode($token, $private_key, $algorithm);
101 private function getUserIdAndAuthTime($userInfo)
106 if (is_array($userInfo)) {
107 if (!isset($userInfo[
'user_id'])) {
108 throw new \LogicException(
'if $user_id argument is an array, user_id index must be set');
111 $auth_time = isset($userInfo[
'auth_time']) ? $userInfo[
'auth_time'] :
null;
112 $user_id = $userInfo[
'user_id'];
114 $user_id = $userInfo;
117 if (is_null($auth_time)) {
122 return array($user_id, $auth_time);