X-Git-Url: https://git.cweiske.de/indieauth-openid.git/blobdiff_plain/c773a629c4d2cc6afecc514358d2e36b9eb3d4f2..93ca0e9f11d88b3d9b7bd92537c22a27714d6361:/www/index.php diff --git a/www/index.php b/www/index.php index 496d7d3..ca89855 100644 --- a/www/index.php +++ b/www/index.php @@ -13,15 +13,38 @@ * @link http://indiewebcamp.com/auth-brainstorming * @link https://indieauth.com/developers */ +header('IndieAuth: authorization_endpoint'); +if (($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') + && count($_GET) == 0 +) { + include 'about.php'; + exit(); +} require_once 'Net/URL2.php'; +require_once 'OpenID.php'; require_once 'OpenID/RelyingParty.php'; require_once 'OpenID/Message.php'; 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, @@ -124,7 +147,6 @@ function getBaseUrl() . $file; } -header('IndieAuth: authorization_endpoint'); session_start(); $returnTo = getBaseUrl(); $realm = getBaseUrl(); @@ -139,7 +161,7 @@ if (isset($_GET['openid_mode']) && $_GET['openid_mode'] != '') { $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP); $id = $message->get('openid.claimed_id'); - if ($id != $_SESSION['me']) { + if (OpenID::normalizeIdentifier($id) != OpenID::normalizeIdentifier($_SESSION['me'])) { error( sprintf( 'Given identity URL "%s" and claimed OpenID "%s" do not match', @@ -168,6 +190,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()); } } @@ -194,12 +218,19 @@ if ($_SERVER['REQUEST_METHOD'] == 'GET') { try { $o = new \OpenID_RelyingParty($returnTo, $realm, $me); + //if you get timeouts (errors like + // OpenID error: Request timed out after 3 second(s) + //) then uncomment the following line which disables + // all timeouts: + //$o->setRequestOptions(array('follow_redirects' => true)); $authRequest = $o->prepare(); $url = $authRequest->getAuthorizeURL(); header("Location: $url"); 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'); @@ -215,9 +246,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'GET') { $me = validate_token($token, $redirect_uri, $client_id, $state); 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);