|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Auth; |
| 4 | + |
| 5 | +use Pdsinterop\Solid\Auth\Utils\Jwks; |
| 6 | +use Pdsinterop\Solid\Auth\Enum\OpenId\OpenIdConnectMetadata as OidcMeta; |
| 7 | + |
| 8 | +class TokenGenerator |
| 9 | +{ |
| 10 | + ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 11 | + |
| 12 | + /** @var Config */ |
| 13 | + public $config; |
| 14 | + |
| 15 | + //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 16 | + |
| 17 | + final public function __construct( |
| 18 | + Config $config |
| 19 | + ) { |
| 20 | + $this->config = $config; |
| 21 | + } |
| 22 | + |
| 23 | + public function generateRegistrationAccessToken($clientId, $privateKey) { |
| 24 | + $issuer = $this->config->getServer()->get(OidcMeta::ISSUER); |
| 25 | + |
| 26 | + // Create JWT |
| 27 | + $signer = new \Lcobucci\JWT\Signer\Rsa\Sha256(); |
| 28 | + $keychain = new \Lcobucci\JWT\Signer\Keychain(); |
| 29 | + $builder = new \Lcobucci\JWT\Builder(); |
| 30 | + $token = $builder |
| 31 | + ->setIssuer($issuer) |
| 32 | + ->permittedFor($clientId) |
| 33 | + ->set("sub", $clientId) |
| 34 | + ->sign($signer, $keychain->getPrivateKey($privateKey)) |
| 35 | + ->getToken(); |
| 36 | + return $token->__toString(); |
| 37 | + } |
| 38 | + |
| 39 | + public function generateIdToken($accessToken, $clientId, $subject, $nonce, $privateKey) { |
| 40 | + $issuer = $this->config->getServer()->get(OidcMeta::ISSUER); |
| 41 | + |
| 42 | + $jwks = $this->getJwks(); |
| 43 | + $tokenHash = $this->generateTokenHash($accessToken); |
| 44 | + |
| 45 | + // Create JWT |
| 46 | + $signer = new \Lcobucci\JWT\Signer\Rsa\Sha256(); |
| 47 | + $keychain = new \Lcobucci\JWT\Signer\Keychain(); |
| 48 | + $builder = new \Lcobucci\JWT\Builder(); |
| 49 | + $token = $builder |
| 50 | + ->setIssuer($issuer) |
| 51 | + ->permittedFor($clientId) |
| 52 | + ->setIssuedAt(time()) |
| 53 | + ->setNotBefore(time() - 1) |
| 54 | + ->setExpiration(time() + 14*24*60*60) |
| 55 | + ->set("azp", $clientId) |
| 56 | + ->set("sub", $subject) |
| 57 | + ->set("jti", $this->generateJti()) |
| 58 | + ->set("nonce", $nonce) |
| 59 | + ->set("at_hash", $tokenHash) //FIXME: at_hash should only be added if the response_type is a token |
| 60 | + ->set("c_hash", $tokenHash) // FIXME: c_hash should only be added if the response_type is a code |
| 61 | + ->set("cnf", array( |
| 62 | + "jwk" => $jwks['keys'][0] |
| 63 | + )) |
| 64 | + ->withHeader('kid', $jwks['keys'][0]['kid']) |
| 65 | + ->sign($signer, $keychain->getPrivateKey($privateKey)) |
| 66 | + ->getToken(); |
| 67 | + return $token->__toString(); |
| 68 | + } |
| 69 | + |
| 70 | + public function respondToRegistration($registration, $privateKey) { |
| 71 | + /* |
| 72 | + Expects in $registration: |
| 73 | + client_id |
| 74 | + client_id_issued_at |
| 75 | + redirect_uris |
| 76 | + registration_client_uri |
| 77 | + */ |
| 78 | + $registration_access_token = $this->generateRegistrationAccessToken($registration['client_id'], $privateKey); |
| 79 | + |
| 80 | + $registrationBase = array( |
| 81 | + 'response_types' => array("id_token token"), |
| 82 | + 'grant_types' => array("implicit"), |
| 83 | + 'application_type' => 'web', |
| 84 | + 'id_token_signed_response_alg' => "RS256", |
| 85 | + 'token_endpoint_auth_method' => 'client_secret_basic', |
| 86 | + 'registration_access_token' => $registration_access_token, |
| 87 | + ); |
| 88 | + |
| 89 | + return array_merge($registrationBase, $registration); |
| 90 | + } |
| 91 | + |
| 92 | + public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $privateKey) { |
| 93 | + if ($response->hasHeader("Location")) { |
| 94 | + $value = $response->getHeaderLine("Location"); |
| 95 | + if (preg_match("/#access_token=(.*?)&/", $value, $matches)) { |
| 96 | + $idToken = $this->generateIdToken( |
| 97 | + $matches[1], |
| 98 | + $clientId, |
| 99 | + $subject, |
| 100 | + $nonce, |
| 101 | + $privateKey |
| 102 | + ); |
| 103 | + $value = preg_replace("/#access_token=(.*?)&/", "#access_token=\$1&id_token=$idToken&", $value); |
| 104 | + $response = $response->withHeader("Location", $value); |
| 105 | + } else if (preg_match("/code=(.*?)&/", $value, $matches)) { |
| 106 | + $idToken = $this->generateIdToken( |
| 107 | + $matches[1], |
| 108 | + $clientId, |
| 109 | + $subject, |
| 110 | + $nonce, |
| 111 | + $privateKey |
| 112 | + ); |
| 113 | + $value = preg_replace("/code=(.*?)&/", "code=\$1&id_token=$idToken&", $value); |
| 114 | + $response = $response->withHeader("Location", value); |
| 115 | + } |
| 116 | + } |
| 117 | + return $response; |
| 118 | + } |
| 119 | + ///////////////////////////// HELPER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 120 | + |
| 121 | + private function generateJti() { |
| 122 | + return substr(md5((string)time()), 12); // FIXME: generate unique jti values |
| 123 | + } |
| 124 | + |
| 125 | + private function generateTokenHash($accessToken) { |
| 126 | + $atHash = hash('sha256', $accessToken); |
| 127 | + $atHash = substr($atHash, 0, 32); |
| 128 | + $atHash = hex2bin($atHash); |
| 129 | + $atHash = base64_encode($atHash); |
| 130 | + $atHash = rtrim($atHash, '='); |
| 131 | + $atHash = str_replace('/', '_', $atHash); |
| 132 | + $atHash = str_replace('+', '-', $atHash); |
| 133 | + |
| 134 | + return $atHash; |
| 135 | + } |
| 136 | + |
| 137 | + private function getJwks() { |
| 138 | + $key = $this->config->getKeys()->getPublicKey(); |
| 139 | + $jwks = new Jwks($key); |
| 140 | + return json_decode($jwks->__toString(), true); |
| 141 | + } |
| 142 | +} |
0 commit comments