do not use state parameter on auth code verification
[indieauth-openid.git] / www / index.php
index 2e6b4c8c4bb0671977816a67320e415994146020..724c97c2b62498e739a963d8135bf58209d5a1c9 100644 (file)
@@ -29,7 +29,22 @@ require_once 'OpenID/Exception.php';
 
 function loadDb()
 {
-    $db = new PDO('sqlite:' . __DIR__ . '/../data/tokens.sq3');
+    $pharFile = \Phar::running();
+    if ($pharFile == '') {
+        $dsn = 'sqlite:' . __DIR__ . '/../data/tokens.sq3';
+        $cfgFilePath = __DIR__ . '/config.php';
+    } else {
+        //remove phar:// from the path
+        $dir = dirname(substr($pharFile, 7)) . '/';
+        $dsn = 'sqlite:' . $dir . '/tokens.sq3';
+        $cfgFilePath = substr($pharFile, 7) . '.config.php';
+    }
+    //allow overriding DSN
+    if (file_exists($cfgFilePath)) {
+        include $cfgFilePath;
+    }
+
+    $db = new PDO($dsn);
     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $db->exec("CREATE TABLE IF NOT EXISTS authtokens(
 code TEXT,
@@ -66,7 +81,7 @@ function create_token($me, $redirect_uri, $client_id, $state)
     return $code;
 }
 
-function validate_token($code, $redirect_uri, $client_id, $state)
+function validate_token($code, $redirect_uri, $client_id)
 {
     $db = loadDb();
     $stmt = $db->prepare(
@@ -74,7 +89,6 @@ function validate_token($code, $redirect_uri, $client_id, $state)
         . ' code = :code'
         . ' AND redirect_uri = :redirect_uri'
         . ' AND client_id = :client_id'
-        . ' AND state = :state'
         . ' AND created >= :created'
     );
     $stmt->execute(
@@ -82,7 +96,6 @@ function validate_token($code, $redirect_uri, $client_id, $state)
             ':code'         => $code,
             ':redirect_uri' => $redirect_uri,
             ':client_id'    => $client_id,
-            ':state'        => (string) $state,
             ':created'      => date('c', time() - 60)
         )
     );
@@ -175,6 +188,8 @@ if (isset($_GET['openid_mode']) && $_GET['openid_mode'] != '') {
         }
     } catch (OpenID_Exception $e) {
         error('Error verifying OpenID login: ' . $e->getMessage());
+    } catch (Exception $e) {
+        error(get_class($e) . ': ' . $e->getMessage());
     }
 }
 
@@ -212,24 +227,20 @@ if ($_SERVER['REQUEST_METHOD'] == 'GET') {
         exit(0);
     } catch (OpenID_Exception $e) {
         error('OpenID error: ' . $e->getMessage());
+    } catch (Exception $e) {
+        error(get_class($e) . ': ' . $e->getMessage());
     }
 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
     $client_id    = verifyUrlParameter($_POST, 'client_id');
-    $state        = null;
-    if (isset($_POST['state'])) {
-        $state = $_POST['state'];
-    }
     if (!isset($_POST['code'])) {
         error('"code" parameter missing');
     }
     $token = $_POST['code'];
 
-    $me = validate_token($token, $redirect_uri, $client_id, $state);
+    $me = validate_token($token, $redirect_uri, $client_id);
     if ($me === false) {
-        header('HTTP/1.0 400 Bad Request');
-        echo "Validating token failed\n";
-        exit(1);
+        error('Validating token failed');
     }
     header('Content-type: application/x-www-form-urlencoded');
     echo 'me=' . urlencode($me);