|  | 
|  | 1 | +<?php declare(strict_types=1); | 
|  | 2 | + | 
|  | 3 | +namespace Pdsinterop\Solid\Controller; | 
|  | 4 | + | 
|  | 5 | +use Psr\Http\Message\ResponseInterface; | 
|  | 6 | +use Psr\Http\Message\ServerRequestInterface; | 
|  | 7 | + | 
|  | 8 | +class OpenidController extends AbstractController | 
|  | 9 | +{     | 
|  | 10 | +    private $keys; | 
|  | 11 | +	 | 
|  | 12 | +    public function __construct(){ | 
|  | 13 | +        // parent::__construct(); | 
|  | 14 | +        require_once(__DIR__.'/../../vendor/autoload.php'); | 
|  | 15 | + | 
|  | 16 | +        $this->keys = $this->getKeys(); | 
|  | 17 | +    } | 
|  | 18 | +    private function linkToRoute($route) { | 
|  | 19 | +        return "/$route"; | 
|  | 20 | +    } | 
|  | 21 | +    private function getBaseUrl($httpHost) { | 
|  | 22 | +        return "https://$httpHost"; | 
|  | 23 | +    } | 
|  | 24 | +    private function getAbsoluteUrl($relativeUrl, $baseUrl) { | 
|  | 25 | +        return "$baseUrl$relativeUrl"; | 
|  | 26 | +    } | 
|  | 27 | +    private function getOpenIdConfiguration($baseUrl) { | 
|  | 28 | +				return array( | 
|  | 29 | +					'issuer' => $baseUrl, | 
|  | 30 | +					'authorization_endpoint' => $this->getAbsoluteUrl($this->linkToRoute("authorize"), $baseUrl), | 
|  | 31 | +					'jwks_uri' => $this->getAbsoluteUrl($this->linkToRoute("jwks"), $baseUrl), | 
|  | 32 | +					"response_types_supported" => array("code","code token","code id_token","id_token code","id_token","id_token token","code id_token token","none"), | 
|  | 33 | +					"token_types_supported" => array("legacyPop","dpop"), | 
|  | 34 | +					"response_modes_supported" => array("query","fragment"), | 
|  | 35 | +					"grant_types_supported" => array("authorization_code","implicit","refresh_token","client_credentials"), | 
|  | 36 | +					"subject_types_supported" => ["public"], | 
|  | 37 | +					"id_token_signing_alg_values_supported" => ["RS256"], | 
|  | 38 | +					"token_endpoint_auth_methods_supported" => "client_secret_basic", | 
|  | 39 | +					"token_endpoint_auth_signing_alg_values_supported" => ["RS256"], | 
|  | 40 | +					"display_values_supported" => [], | 
|  | 41 | +					"claim_types_supported" => ["normal"], | 
|  | 42 | +					"claims_supported" => [], | 
|  | 43 | +					"claims_parameter_supported" => false, | 
|  | 44 | +					"request_parameter_supported" => true, | 
|  | 45 | +					"request_uri_parameter_supported" => false, | 
|  | 46 | +					"require_request_uri_registration" => false, | 
|  | 47 | +					"check_session_iframe" => $this->getAbsoluteUrl($this->linkToRoute("session"), $baseUrl), | 
|  | 48 | +					"end_session_endpoint" => $this->getAbsoluteUrl($this->linkToRoute("logout"), $baseUrl), | 
|  | 49 | +					"token_endpoint" => $this->getAbsoluteUrl($this->linkToRoute("token"), $baseUrl), | 
|  | 50 | +					"userinfo_endpoint" => $this->getAbsoluteUrl($this->linkToRoute("userinfo"), $baseUrl), | 
|  | 51 | +					"registration_endpoint" => $this->getAbsoluteUrl($this->linkToRoute("register"), $baseUrl), | 
|  | 52 | +			//		"sharing_endpoint" => $this->getAbsoluteUrl($this->linkToRoute("sharing"), $baseUrl) | 
|  | 53 | +				); | 
|  | 54 | +    } | 
|  | 55 | +    private function getKeys() { | 
|  | 56 | +				// FIXME: read these from the solid config in nextcloud; | 
|  | 57 | +				$encryptionKey = 'P76gcBVeXsVzrHiYp4IIwore5rQz4cotdZ2j9GV5V04='; | 
|  | 58 | +				$privateKey = <<<EOF | 
|  | 59 | +-----BEGIN RSA PRIVATE KEY----- | 
|  | 60 | +MIIEpAIBAAKCAQEAvqb0htUFZaZ+z5rn7cHWg0VzsSoVnusbtJvwWtHfD0T0s6Hb | 
|  | 61 | +OqzE5h2fgdGbB49HRtc21SNHx6jeEStGv03yyqYkLUKrJJSg+ksrL+pT3Nd0h25q | 
|  | 62 | +sx7YUoPPxnm6sbd3XTg5efCb2yyV2dOoAegUPjK46Ra6PqUvmICQWDsjnv0VJIx+ | 
|  | 63 | +TdDWmKY2xElk0T6CVNMD08OZVTHPwJgpGdRZyCK/SSmrvmAZ6K3ocKyS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
|  | 78 | +wFWH8SXHKHhKyTgmvBjmal1xVFyJu0WzYX+TbjcykoI0IZFSw4ilxdw1L67G88yM | 
|  | 79 | +1M7NLKtLuCpKgpOspZjOmCvTAoGAGji6KswYCt2SaNkmIx/jpUTInSR8xpnEtD7H | 
|  | 80 | +QOmeEPKxmFwON/eKMIUXcaoRsNAEIvOxb4MT4YiLHJIIC0XuxxS6xF/XP0hBBloW | 
|  | 81 | +s1jxC/cgLJixKa5uoNcHN1OxwMBQECgvo+GTDnwkWw4QA9kgwAOroxQ4EvMxrqHS | 
|  | 82 | +O9Pvn4ECgYA7xr/3Sz8n+BhgOdABW0m91P144rK9QDYiaClSxAha1KiFunmAy3pB | 
|  | 83 | +Uxdl4yTCTA9yKIH7X3bShDXnj+RmEZ+SkwzpPuKvAE8ZkZQuXv41anFrZYkR2PZy | 
|  | 84 | +oYiERqXgH5yS/mkDeXRFx1nWsVxjoLWfd/Vi7Lr43cuYFy4UjqXZdg== | 
|  | 85 | +-----END RSA PRIVATE KEY----- | 
|  | 86 | +EOF; | 
|  | 87 | + | 
|  | 88 | +				$key = openssl_pkey_get_private($privateKey); | 
|  | 89 | +				$publicKey = openssl_pkey_get_details($key)['key']; | 
|  | 90 | +				 | 
|  | 91 | +				return array( | 
|  | 92 | +					"encryptionKey" => $encryptionKey, | 
|  | 93 | +					"privateKey" => $privateKey, | 
|  | 94 | +					"publicKey" => $publicKey | 
|  | 95 | +				); | 
|  | 96 | +    } | 
|  | 97 | +    private function getClientId() { | 
|  | 98 | +        return "coolApp"; | 
|  | 99 | +    } | 
|  | 100 | +    private function getClient($clientId, $baseUrl) { | 
|  | 101 | +        if (!$clientId) { | 
|  | 102 | +            $clientId = $this->getClientId(); // FIXME: only continue if a clientId is set; | 
|  | 103 | +        } | 
|  | 104 | +         | 
|  | 105 | +        if ($clientId) { // FIXME: and check that we know this client and get the client secret/client name for this client; | 
|  | 106 | +            $clientSecret = "super-secret-secret-squirrel"; | 
|  | 107 | +             | 
|  | 108 | +            // FIXME: use the redirect URIs as indicated by the client; | 
|  | 109 | +            $clientRedirectUris = array( | 
|  | 110 | +                $this->getAbsoluteURL($this->linkToRoute("token"), $baseUrl), | 
|  | 111 | +                'https://solid.community/.well-known/solid/login', | 
|  | 112 | +                'http://localhost:3001/redirect' | 
|  | 113 | +            ); | 
|  | 114 | +            $clientName = "Nextcloud"; | 
|  | 115 | + | 
|  | 116 | +            return new \Pdsinterop\Solid\Auth\Config\Client( | 
|  | 117 | +                $clientId, | 
|  | 118 | +                $clientSecret, | 
|  | 119 | +                $clientRedirectUris, | 
|  | 120 | +                $clientName | 
|  | 121 | +            ); | 
|  | 122 | +        } else { | 
|  | 123 | +            return new \Pdsinterop\Solid\Auth\Config\Client('','',array(),''); | 
|  | 124 | +        } | 
|  | 125 | +    } | 
|  | 126 | + | 
|  | 127 | +	  private function createConfig($baseUrl) { | 
|  | 128 | +				// if (isset($_GET['client_id'])) { | 
|  | 129 | +				$clientId = $_GET['client_id']; | 
|  | 130 | +				$client = $this->getClient($clientId, $baseUrl); | 
|  | 131 | +				// } | 
|  | 132 | +				try { | 
|  | 133 | +						$config = (new \Pdsinterop\Solid\Auth\Factory\ConfigFactory( | 
|  | 134 | +								$client, | 
|  | 135 | +								$this->keys['encryptionKey'], | 
|  | 136 | +								$this->keys['privateKey'], | 
|  | 137 | +								$this->keys['publicKey'], | 
|  | 138 | +								$this->openIdConfiguration | 
|  | 139 | +						))->create(); | 
|  | 140 | +				} catch(\Throwable $e) { | 
|  | 141 | +						var_dump($e); | 
|  | 142 | +				} | 
|  | 143 | +				return $config; | 
|  | 144 | +		} | 
|  | 145 | + | 
|  | 146 | +    final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface | 
|  | 147 | +    { | 
|  | 148 | +				$httpHost = $request->getServerParams()['HTTP_HOST']; | 
|  | 149 | +				$baseUrl = $this->getBaseUrl($httpHost); | 
|  | 150 | +				$this->openIdConfiguration = $this->getOpenIdConfiguration($baseUrl); | 
|  | 151 | +				 | 
|  | 152 | +				$this->authServerConfig = $this->createConfig($baseUrl); | 
|  | 153 | +				$this->authServerFactory = (new \Pdsinterop\Solid\Auth\Factory\AuthorizationServerFactory($this->authServerConfig))->create(); | 
|  | 154 | + | 
|  | 155 | +        $response = $this->getResponse(); | 
|  | 156 | +				$server	= new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response); | 
|  | 157 | +				return $server->respondToOpenIdMetadataRequest(); | 
|  | 158 | +    } | 
|  | 159 | +} | 
0 commit comments