Do not require "me" parameter in connection callback
[shpub.git] / src / shpub / Command / Connect.php
index 748e905f73ee64a9dba2ee8e83bf77b8fe835b58..b39665b1c729394335abeb749444d081fd578456 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
 {
@@ -28,6 +33,17 @@ class Command_Connect
                 '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',
             [
@@ -51,7 +67,7 @@ class Command_Connect
         );
     }
 
-    public function run($server, $user, $newKey, $force)
+    public function run($server, $user, $newKey, $force, $scope)
     {
         $server = Validator::url($server, 'server');
         if ($user === null) {
@@ -75,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);
@@ -142,9 +158,9 @@ 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 ($res->getHeader('content-type') == 'application/json') {
+        } elseif (Util::getMimeType($res) == 'application/json') {
             $tokenParams = json_decode($res->getBody(), true);
         } else {
             Log::err('Wrong content type in auth verification response');
@@ -159,14 +175,16 @@ class Command_Connect
         return $accessToken;
     }
 
-    protected function getBrowserAuthUrl($host, $user, $redirect_uri, $state)
+    protected function getBrowserAuthUrl($host, $user, $redirect_uri, $state, $scope)
     {
+        $sep = strpos($host->endpoints->authorization, '?') === false
+            ? '?' : '&';
         return $host->endpoints->authorization
-            . '?me=' . urlencode($user)
+            . $sep . 'me=' . urlencode($user)
             . '&client_id=' . urlencode(static::$client_id)
             . '&redirect_uri=' . urlencode($redirect_uri)
-            . '&state=' . $state
-            . '&scope=post'
+            . '&state=' . urlencode($state)
+            . '&scope=' . urlencode($scope)
             . '&response_type=code';
     }
 
@@ -261,7 +279,6 @@ class Command_Connect
                     parse_str($parts['query'], $query);
                     if (isset($query['code'])
                         && isset($query['state'])
-                        && isset($query['me'])
                     ) {
                         fwrite($sock, $responseOk);
                         fclose($sock);