friendship ended with social-app. php is my new best friend

Compare changes

Choose any two refs to compare.

Changed files
+76 -64
templates
+71 -63
index.php
···
Flight::set('frontpageFeed', FRONTPAGE_FEED);
Flight::set('defaultRelay', DEFAULT_RELAY);
Flight::set('userAuth', array_key_exists('sbs_'.SITE_DOMAIN, $_SESSION) ? $_SESSION['sbs_'.SITE_DOMAIN] : null);
+
Flight::set('userPds', array_key_exists('sbs_'.SITE_DOMAIN.'_pds', $_SESSION) ? $_SESSION['sbs_'.SITE_DOMAIN.'_pds'] : null);
+
Flight::set('userInfo', array_key_exists('sbs_'.SITE_DOMAIN.'_userinfo', $_SESSION) ? $_SESSION['sbs_'.SITE_DOMAIN.'_userinfo'] : null);
Flight::set('flight.log_errors', false);
Flight::set('flight.handle_errors', false);
Flight::set('flight.content_length', false);
···
'setTheme' => array_key_exists('sbs_theme', $_COOKIE) ? $_COOKIE['sbs_theme'] : DEFAULT_THEME,
'setFont' => array_key_exists('sbs_font', $_COOKIE) ? $_COOKIE['sbs_font'] : DEFAULT_FONT,
'userAuth' => Flight::get('userAuth'),
+
'userPds' => Flight::get('userPds'),
+
'userInfo' => Flight::Get('userInfo'),
'favFeeds' => $favoriteFeeds,
'pages' => PAGES,
'links' => LINKS,
···
});
Flight::route('/login', function(): void {
-
if (!array_key_exists('username', $_GET)) {
+
if (isset($_GET['username'])) {
+
$username = $_GET['username'];
+
$bskyToucher = new BskyToucher();
+
$userInfo = $bskyToucher->getUserInfo($username);
+
if (!$userInfo) die(1);
+
$pds = $userInfo->pds;
+
$options = new OAuthOptions([
+
'key' => 'https://'.SITE_DOMAIN.CLIENT_ID,
+
'secret' => CLIENT_SECRET,
+
'callbackURL' => 'https://'.SITE_DOMAIN.'/login',
+
'sessionStart' => true,
+
'sessionStorageVar' => 'sbs_'.SITE_DOMAIN
+
]);
+
$storage = new SessionStorage($options);
+
$connector = new React\Socket\Connector([
+
'dns' => '1.1.1.1'
+
]);
+
$http = new React\Http\Browser($connector);
+
$httpFactory = new HttpFactory();
+
$token_builder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
+
$algorithm = new Sha256();
+
$signing_key = InMemory::file(CERT_PATH);
+
$now = new DateTimeImmutable();
+
$token = $token_builder
+
->withHeader('alg', 'ES256')
+
->withHeader('typ', 'JWT')
+
->withHeader('kid', 'ocwgKj_O7H9at1sL6yWf9ZZ82NOM7D0xlN8HGIyWH6M')
+
->issuedBy('https://'.SITE_DOMAIN.CLIENT_ID)
+
->identifiedBy(uniqid())
+
->relatedTo('https://'.SITE_DOMAIN.CLIENT_ID)
+
->permittedFor($pds)
+
->issuedAt($now->modify('-5 seconds'))
+
->getToken($algorithm, $signing_key);
+
$client = new GuzzleHttp\Client([
+
'verify' => true,
+
'headers' => [
+
'User-Agent' => USER_AGENT_STR,
+
'Authorization' => 'Bearer: '.$token->toString()
+
]
+
]);
+
$provider = new BskyProvider($options, $client, $httpFactory, $httpFactory, $httpFactory);
+
$provider->setPds($pds);
+
$name = $provider->getName();
+
if (isset($_GET['login']) && $_GET['login'] === $name) {
+
$auth_url = $provider->getAuthorizationUrl([
+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
+
'client_assertion' => $token->toString()
+
]);
+
Flight::redirect($auth_url);
+
die(1);
+
} else if (isset($_GET['code'], $_GET['iss'])) {
+
$storage->storeAccessToken($_GET['code'], $name);
+
$_SESSION['sbs_'.SITE_DOMAIN.'_pds'] = $_GET['iss'];
+
$_SESSION['sbs_'.SITE_DOMAIN.'_userinfo'] = $bskyToucher->getUserInfo();
+
Flight::redirect('/');
+
die(1);
+
} else if (isset($_GET['error'])) {
+
die(1);
+
}
+
} else {
$latte = new Latte\Engine;
$latte->render('./templates/login.latte', array_merge(Flight::get('standardParams'), [
'mainClass' => 'form',
···
'ogimage' => '',
'ogurl' => 'https://'.SITE_DOMAIN.'/login'
]));
-
die(1);
}
-
$username = $_GET['username'];
-
$bskyToucher = new BskyToucher();
-
$userInfo = $bskyToucher->getUserInfo($username);
-
if (!$userInfo) die(1);
-
$pds = $userInfo->pds;
-
$options = new OAuthOptions([
-
'key' => 'https://'.SITE_DOMAIN.CLIENT_ID,
-
'secret' => CLIENT_SECRET,
-
'callbackURL' => 'https://'.SITE_DOMAIN.'/login',
-
'sessionStart' => true,
-
'sessionStorageVar' => 'sbs_'.SITE_DOMAIN
-
]);
-
$storage = new SessionStorage($options);
-
echo 'get access token:';
-
print_r($storage->getAccessToken($name));
-
$connector = new React\Socket\Connector([
-
'dns' => '1.1.1.1'
-
]);
-
$http = new React\Http\Browser($connector);
-
$httpFactory = new HttpFactory();
-
$token_builder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
-
$algorithm = new Sha256();
-
$signing_key = InMemory::file(CERT_PATH);
-
$now = new DateTimeImmutable();
-
$token = $token_builder
-
->withHeader('alg', 'ES256')
-
->withHeader('typ', 'JWT')
-
->withHeader('kid', 'ocwgKj_O7H9at1sL6yWf9ZZ82NOM7D0xlN8HGIyWH6M')
-
->issuedBy('https://'.SITE_DOMAIN.CLIENT_ID)
-
->identifiedBy(uniqid())
-
->relatedTo('https://'.SITE_DOMAIN.CLIENT_ID)
-
->permittedFor($pds)
-
->issuedAt($now->modify('-5 seconds'))
-
->getToken($algorithm, $signing_key);
-
$client = new GuzzleHttp\Client([
-
'verify' => true,
-
'headers' => [
-
'User-Agent' => USER_AGENT_STR,
-
'Authorization' => 'Bearer: '.$token->toString()
-
]
-
]);
-
$provider = new BskyProvider($options, $client, $httpFactory, $httpFactory, $httpFactory);
-
$provider->setPds($pds);
-
$name = $provider->getName();
-
-
if (isset($_GET['login']) && $_GET['login'] === $name) {
-
$auth_url = $provider->getAuthorizationUrl([
-
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
-
'client_assertion' => $token->toString()
-
]);
-
header('Location: '.$auth_url);
-
die(1);
-
} else if (isset($_GET['code'])) {
-
// https://smallbird.social/login?iss=https%3A%2F%2Fshimaenaga.veryroundbird.house&code=cod-3a80b9095e871dc4bc2391f876cc234abefcbb66ca65e8a381e99095b8899b0e
-
$token = $provider->getAccessToken($_GET['code'], null);
-
$storage->storeAccessToken($token, $name);
-
header('Location: https://'.SITE_DOMAIN);
-
die(1);
-
} else if (isset($_GET['error'])) {
-
die(1);
-
}
+
});
+
+
Flight::route('/logout', function(): void {
+
unset($_SESSION['sbs_'.SITE_DOMAIN]);
+
unset($_SESSION['sbs_'.SITE_DOMAIN.'_pds']);
+
unset($_SESSION['sbs_'.SITE_DOMAIN.'_userinfo']);
+
Flight::redirect('/');
});
Flight::route('/createaccount', function(): void {
+1 -1
templates/_partials/nav.latte
···
<nav>
<ul>
{if $userAuth}
-
<li><a href="/">profile</a></li>
+
<li><a href="/u/{$userInfo->handle}">profile</a></li>
<li><a href="/settings">settings</a></li>
<li><a href="/logout">log out</a></li>
{else}
+4
templates/layout.latte
···
data-theme="{$setTheme}"
data-font="{$setFont}"
>
+
<!--
+
{print_r($_SESSION)}
+
{print_r(PHP_SESSION_DISABLED)}
+
-->
<div id="page">
<header>
<h1><a href="/">{include '_partials/logo.latte'}{$siteTitle}</a></h1>