]> source.dussan.org Git - nextcloud-server.git/commitdiff
attempt at reusing access tokens
authorMichiel de Jong <michiel@unhosted.org>
Wed, 9 May 2012 13:17:01 +0000 (15:17 +0200)
committerMichiel de Jong <michiel@unhosted.org>
Wed, 9 May 2012 13:28:30 +0000 (15:28 +0200)
apps/remoteStorage/auth.php
apps/remoteStorage/lib_remoteStorage.php

index a5cbd6aca02b6287f47b0a7ddc91e6eec35d0ab9..ac8c24232913ff7fbcf5c1eed2834d5ebd6e0116 100755 (executable)
@@ -62,6 +62,8 @@ if(count($pathParts) == 2 && $pathParts[0] == '') {
                        //TODO: check if this can be faked by editing the cookie in firebug!
                        $token=OC_remoteStorage::createCategories($appUrl, $categories);
                        header('Location: '.$_GET['redirect_uri'].'#access_token='.$token.'&token_type=bearer');
+               } else if($existingToken = OC_remoteStorage::getTokenFor($appUrl, $categories)) {
+                       header('Location: '.$_GET['redirect_uri'].'#access_token='.$existingToken.'&token_type=bearer');
                } else {
 ?>
 <!DOCTYPE html>
@@ -82,25 +84,25 @@ if(count($pathParts) == 2 && $pathParts[0] == '') {
                </header>
                <section id="main">
                <div id="oauth">
-      <h2><img src="../remoteStorage-big.png" alt="remoteStorage" /></h2>
-      <p><strong><?php $appUrlParts = explode('/', $_GET['redirect_uri']); echo htmlentities($appUrlParts[2]); ?></strong>
-      requests read &amp; write access to your 
-      <?php
-        $categories = explode(',', htmlentities($_GET['scope']));
-        if(!count($categories)) {
-          echo htmlentities($_GET['scope']);
-        } else {
-          echo '<em>'.$categories[0].'</em>';
-          if(count($categories)==2) {
-            echo ' and <em>'.$categories[1].'</em>';
-          } else if(count($categories)>2) {
-            for($i=1; $i<count($categories)-1; $i++) {
-              echo ', <em>'.$categories[$i].'</em>';
-            }
-            echo ', and <em>'.$categories[$i].'</em>';
-          }
-        }
-      ?>.
+                       <h2><img src="../remoteStorage-big.png" alt="remoteStorage" /></h2>
+                       <p><strong><?php $appUrlParts = explode('/', $_GET['redirect_uri']); echo htmlentities($appUrlParts[2]); ?></strong>
+                       requests read &amp; write access to your 
+                       <?php
+                               $categories = explode(',', htmlentities($_GET['scope']));
+                               if(!count($categories)) {
+                                       echo htmlentities($_GET['scope']);
+                               } else {
+                                       echo '<em>'.$categories[0].'</em>';
+                                       if(count($categories)==2) {
+                                               echo ' and <em>'.$categories[1].'</em>';
+                                       } else if(count($categories)>2) {
+                                               for($i=1; $i<count($categories)-1; $i++) {
+                                                       echo ', <em>'.$categories[$i].'</em>';
+                                               }
+                                               echo ', and <em>'.$categories[$i].'</em>';
+                                       }
+                               }
+                       ?>.
                        </p>
                        <form accept-charset="UTF-8" method="post">
                                <input id="allow-auth" name="allow" type="submit" value="Allow" />
index b6a7a43b7b29c0f3d685d154cff65210ed30985f..3e366ad29eaa97f1e34194ad9f9406e7ef74e373 100755 (executable)
@@ -13,6 +13,18 @@ class OC_remoteStorage {
                return $ret;
        }
 
+  public static function getTokenFor($appUrl, $categories) {
+               $user=OCP\USER::getUser();
+               $query=OCP\DB::prepare("SELECT token FROM *PREFIX*authtoken WHERE user=? AND appUrl=? AND category=? LIMIT 1");
+               $result=$query->execute(array($user, $appUrl, $categories));
+               $ret = array();
+               if($row=$result->fetchRow()) {
+      return $row['token'];
+    } else {
+      return false;
+    }
+       }
+
        public static function getAllTokens() {
                $user=OCP\USER::getUser();
                $query=OCP\DB::prepare("SELECT token,appUrl,category FROM *PREFIX*authtoken WHERE user=? LIMIT 100");