add "published" option to notes
[shpub.git] / src / shpub / Command / Connect.php
index 0c86a8bac811e503bc26c9956220435203d7388a..668d7e90419a30c8001fe9f105fbfe7fd55889ee 100644 (file)
@@ -16,6 +16,14 @@ class Command_Connect
 
     public function run($server, $user, $newKey, $force)
     {
+        $server = Validator::url($server, 'server');
+        if ($user === null) {
+            //indieweb: homepage is your identity
+            $user = $server;
+        } else {
+            $user = Validator::url($user, 'user');
+        }
+
         $host = $this->getHost($newKey != '' ? $newKey : $server, $force);
         if ($host === null) {
             //already taken
@@ -39,7 +47,6 @@ class Command_Connect
         }
         $code    = $authParams['code'];
         $userUrl = $authParams['me'];
-        $this->verifyAuthCode($host, $code, $state, $redirect_uri, $userUrl);
 
         $accessToken = $this->fetchAccessToken(
             $host, $userUrl, $code, $redirect_uri, $state
@@ -65,6 +72,7 @@ class Command_Connect
         }
         $this->cfg->hosts[$hostKey] = $host;
         $this->cfg->save();
+        echo "Server configuration $hostKey saved successfully.\n";
     }
 
     protected function fetchAccessToken(
@@ -89,6 +97,12 @@ class Command_Connect
             )
         );
         $res = $req->send();
+        if (intval($res->getStatus() / 100) !== 2) {
+            Log::err('Failed to fetch access token');
+            Log::err('Server responded with HTTP status code ' . $res->getStatus());
+            Log::err($res->getBody());
+            exit(2);
+        }
         if ($res->getHeader('content-type') != 'application/x-www-form-urlencoded') {
             Log::err('Wrong content type in auth verification response');
             exit(2);
@@ -130,45 +144,23 @@ class Command_Connect
 
     protected function getHttpServerData()
     {
-        //FIXME: get IP from SSH_CONNECTION
         $ip   = '127.0.0.1';
         $port = 12345;
-        $redirect_uri = 'http://' . $ip . ':' . $port . '/callback';
-        $socketStr    = 'tcp://' . $ip . ':' . $port;
-        return [$redirect_uri, $socketStr];
-    }
 
-    protected function verifyAuthCode($host, $code, $state, $redirect_uri, $me)
-    {
-        $req = new \HTTP_Request2($host->endpoints->authorization, 'POST');
-        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
-            //correct ssl validation on php 5.5 is a pain, so disable
-            $req->setConfig('ssl_verify_host', false);
-            $req->setConfig('ssl_verify_peer', false);
-        }
-        $req->setHeader('Content-Type: application/x-www-form-urlencoded');
-        $req->setBody(
-            http_build_query(
-                [
-                    'code'         => $code,
-                    'state'        => $state,
-                    'client_id'    => static::$client_id,
-                    'redirect_uri' => $redirect_uri,
-                ]
-            )
-        );
-        $res = $req->send();
-        if ($res->getHeader('content-type') != 'application/x-www-form-urlencoded') {
-            Log::err('Wrong content type in auth verification response');
-            exit(2);
+        if (isset($_SERVER['SSH_CONNECTION'])) {
+            $parts = explode(' ', $_SERVER['SSH_CONNECTION']);
+            if (count($parts) >= 3) {
+                $ip = $parts[2];
+            }
         }
-        parse_str($res->getBody(), $verifiedParams);
-        if (!isset($verifiedParams['me'])
-            || $verifiedParams['me'] !== $me
-        ) {
-            Log::err('Non-matching "me" values');
-            exit(2);
+        if (strpos($ip, ':') !== false) {
+            //ipv6
+            $ip = '[' . $ip . ']';
         }
+
+        $redirect_uri = 'http://' . $ip . ':' . $port . '/callback';
+        $socketStr    = 'tcp://' . $ip . ':' . $port;
+        return [$redirect_uri, $socketStr];
     }
 
     protected function startHttpServer($socketStr)