Split off content type comments
[shpub.git] / src / shpub / Command / Connect.php
index 2fd6d3800c4a11343d8c8118f6046164a5bac93a..e92077779c8abce3eb16d17d13649b1d26a53bec 100644 (file)
@@ -2,8 +2,13 @@
 namespace shpub;
 
 /**
- * @link http://micropub.net/draft/
- * @link http://indieweb.org/authorization-endpoint
+ * Connect to a micropub server to get an access token.
+ *
+ * @author  Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link    http://cweiske.de/shpub.htm
+ * @link    http://micropub.net/draft/
+ * @link    http://indieweb.org/authorization-endpoint
  */
 class Command_Connect
 {
@@ -14,7 +19,55 @@ class Command_Connect
         $this->cfg = $cfg;
     }
 
-    public function run($server, $user, $newKey, $force)
+    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->addOption(
+            'scope',
+            array(
+                'short_name'  => '-s',
+                'long_name'   => '--scope',
+                'description' => 'Space-separated list of scopes to request'
+                    . ' (default: create)',
+                'action'      => 'StoreString',
+                'default'     => 'create',
+            )
+        );
+        $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, $scope)
     {
         $server = Validator::url($server, 'server');
         if ($user === null) {
@@ -38,7 +91,7 @@ class Command_Connect
         $state = time();
         Log::msg(
             "To authenticate, open the following URL:\n"
-            . $this->getBrowserAuthUrl($host, $user, $redirect_uri, $state)
+            . $this->getBrowserAuthUrl($host, $user, $redirect_uri, $state, $scope)
         );
 
         $authParams = $this->startHttpServer($socketStr);
@@ -89,6 +142,7 @@ class Command_Connect
         $req->setBody(
             http_build_query(
                 [
+                    'grant_type'   => 'authorization_code',
                     'me'           => $userUrl,
                     'code'         => $code,
                     'redirect_uri' => $redirect_uri,
@@ -104,11 +158,14 @@ class Command_Connect
             Log::err($res->getBody());
             exit(2);
         }
-        if ($res->getHeader('content-type') != 'application/x-www-form-urlencoded') {
+        if (Util::getMimeType($res) == 'application/x-www-form-urlencoded') {
+            parse_str($res->getBody(), $tokenParams);
+        } elseif (Util::getMimeType($res) == 'application/json') {
+            $tokenParams = json_decode($res->getBody(), true);
+        } else {
             Log::err('Wrong content type in auth verification response');
             exit(2);
         }
-        parse_str($res->getBody(), $tokenParams);
         if (!isset($tokenParams['access_token'])) {
             Log::err('"access_token" missing');
             exit(2);
@@ -118,14 +175,14 @@ class Command_Connect
         return $accessToken;
     }
 
-    protected function getBrowserAuthUrl($host, $user, $redirect_uri, $state)
+    protected function getBrowserAuthUrl($host, $user, $redirect_uri, $state, $scope)
     {
         return $host->endpoints->authorization
             . '?me=' . urlencode($user)
             . '&client_id=' . urlencode(static::$client_id)
             . '&redirect_uri=' . urlencode($redirect_uri)
             . '&state=' . $state
-            . '&scope=post'
+            . '&scope=' . urlencode($scope)
             . '&response_type=code';
     }