add description for all commands
[shpub.git] / src / shpub / Command / Connect.php
index bd1606eb849c96f6d64b99d616ac37c4907d41a2..66342670235bb155ba459c1746b641ed2f74f58a 100644 (file)
@@ -14,8 +14,53 @@ class Command_Connect
         $this->cfg = $cfg;
     }
 
         $this->cfg = $cfg;
     }
 
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('connect');
+        $cmd->description = 'Obtain access token from a micropub server';
+        $cmd->addOption(
+            'force',
+            array(
+                'short_name'  => '-f',
+                'long_name'   => '--force-update',
+                'description' => 'Force token update if token already available',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
+        $cmd->addArgument(
+            'server',
+            [
+                'optional'    => false,
+                'description' => 'Server URL',
+            ]
+        );
+        $cmd->addArgument(
+            'user',
+            [
+                'optional'    => true,
+                'description' => 'User URL',
+            ]
+        );
+        $cmd->addArgument(
+            'key',
+            [
+                'optional'    => true,
+                'description' => 'Short name (key)',
+            ]
+        );
+    }
+
     public function run($server, $user, $newKey, $force)
     {
     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
         $host = $this->getHost($newKey != '' ? $newKey : $server, $force);
         if ($host === null) {
             //already taken
@@ -28,9 +73,10 @@ class Command_Connect
 
         list($redirect_uri, $socketStr) = $this->getHttpServerData();
         $state = time();
 
         list($redirect_uri, $socketStr) = $this->getHttpServerData();
         $state = time();
-        echo "To authenticate, open the following URL:\n"
+        Log::msg(
+            "To authenticate, open the following URL:\n"
             . $this->getBrowserAuthUrl($host, $user, $redirect_uri, $state)
             . $this->getBrowserAuthUrl($host, $user, $redirect_uri, $state)
-            . "\n";
+        );
 
         $authParams = $this->startHttpServer($socketStr);
         if ($authParams['state'] != $state) {
 
         $authParams = $this->startHttpServer($socketStr);
         if ($authParams['state'] != $state) {
@@ -39,7 +85,6 @@ class Command_Connect
         }
         $code    = $authParams['code'];
         $userUrl = $authParams['me'];
         }
         $code    = $authParams['code'];
         $userUrl = $authParams['me'];
-        $this->verifyAuthCode($host, $code, $state, $redirect_uri, $userUrl);
 
         $accessToken = $this->fetchAccessToken(
             $host, $userUrl, $code, $redirect_uri, $state
 
         $accessToken = $this->fetchAccessToken(
             $host, $userUrl, $code, $redirect_uri, $state
@@ -65,12 +110,18 @@ class Command_Connect
         }
         $this->cfg->hosts[$hostKey] = $host;
         $this->cfg->save();
         }
         $this->cfg->hosts[$hostKey] = $host;
         $this->cfg->save();
+        Log::info("Server configuration $hostKey saved successfully.");
     }
 
     protected function fetchAccessToken(
         $host, $userUrl, $code, $redirect_uri, $state
     ) {
         $req = new \HTTP_Request2($host->endpoints->token, 'POST');
     }
 
     protected function fetchAccessToken(
         $host, $userUrl, $code, $redirect_uri, $state
     ) {
         $req = new \HTTP_Request2($host->endpoints->token, '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(
         $req->setHeader('Content-Type: application/x-www-form-urlencoded');
         $req->setBody(
             http_build_query(
@@ -84,6 +135,12 @@ class Command_Connect
             )
         );
         $res = $req->send();
             )
         );
         $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);
         if ($res->getHeader('content-type') != 'application/x-www-form-urlencoded') {
             Log::err('Wrong content type in auth verification response');
             exit(2);
@@ -125,40 +182,23 @@ class Command_Connect
 
     protected function getHttpServerData()
     {
 
     protected function getHttpServerData()
     {
-        //FIXME: get IP from SSH_CONNECTION
         $ip   = '127.0.0.1';
         $port = 12345;
         $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');
-        $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)
     }
 
     protected function startHttpServer($socketStr)