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