···
Flight::set('publicApi', PUBLIC_API);
Flight::set('frontpageFeed', FRONTPAGE_FEED);
Flight::set('defaultRelay', DEFAULT_RELAY);
70
-
Flight::set('userAuth', $_SESSION['sbs_'.SITE_DOMAIN]);
70
+
Flight::set('userAuth', array_key_exists('sbs_'.SITE_DOMAIN, $_SESSION) ? $_SESSION['sbs_'.SITE_DOMAIN] : null);
71
+
Flight::set('userPds', array_key_exists('sbs_'.SITE_DOMAIN.'_pds', $_SESSION) ? $_SESSION['sbs_'.SITE_DOMAIN.'_pds'] : null);
72
+
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'),
84
+
'userPds' => Flight::get('userPds'),
85
+
'userInfo' => Flight::Get('userInfo'),
'favFeeds' => $favoriteFeeds,
···
Flight::route('/login', function(): void {
181
-
if (!array_key_exists('username', $_GET)) {
185
+
if (isset($_GET['username'])) {
186
+
$username = $_GET['username'];
187
+
$bskyToucher = new BskyToucher();
188
+
$userInfo = $bskyToucher->getUserInfo($username);
189
+
if (!$userInfo) die(1);
190
+
$pds = $userInfo->pds;
191
+
$options = new OAuthOptions([
192
+
'key' => 'https://'.SITE_DOMAIN.CLIENT_ID,
193
+
'secret' => CLIENT_SECRET,
194
+
'callbackURL' => 'https://'.SITE_DOMAIN.'/login',
195
+
'sessionStart' => true,
196
+
'sessionStorageVar' => 'sbs_'.SITE_DOMAIN
198
+
$storage = new SessionStorage($options);
199
+
$connector = new React\Socket\Connector([
202
+
$http = new React\Http\Browser($connector);
203
+
$httpFactory = new HttpFactory();
204
+
$token_builder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
205
+
$algorithm = new Sha256();
206
+
$signing_key = InMemory::file(CERT_PATH);
207
+
$now = new DateTimeImmutable();
208
+
$token = $token_builder
209
+
->withHeader('alg', 'ES256')
210
+
->withHeader('typ', 'JWT')
211
+
->withHeader('kid', 'ocwgKj_O7H9at1sL6yWf9ZZ82NOM7D0xlN8HGIyWH6M')
212
+
->issuedBy('https://'.SITE_DOMAIN.CLIENT_ID)
213
+
->identifiedBy(uniqid())
214
+
->relatedTo('https://'.SITE_DOMAIN.CLIENT_ID)
215
+
->permittedFor($pds)
216
+
->issuedAt($now->modify('-5 seconds'))
217
+
->getToken($algorithm, $signing_key);
218
+
$client = new GuzzleHttp\Client([
221
+
'User-Agent' => USER_AGENT_STR,
222
+
'Authorization' => 'Bearer: '.$token->toString()
225
+
$provider = new BskyProvider($options, $client, $httpFactory, $httpFactory, $httpFactory);
226
+
$provider->setPds($pds);
227
+
$name = $provider->getName();
228
+
if (isset($_GET['login']) && $_GET['login'] === $name) {
229
+
$auth_url = $provider->getAuthorizationUrl([
230
+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
231
+
'client_assertion' => $token->toString()
233
+
Flight::redirect($auth_url);
235
+
} else if (isset($_GET['code'], $_GET['iss'])) {
236
+
$storage->storeAccessToken($_GET['code'], $name);
237
+
$_SESSION['sbs_'.SITE_DOMAIN.'_pds'] = $_GET['iss'];
238
+
$_SESSION['sbs_'.SITE_DOMAIN.'_userinfo'] = $bskyToucher->getUserInfo();
239
+
Flight::redirect('/');
241
+
} else if (isset($_GET['error'])) {
$latte = new Latte\Engine;
$latte->render('./templates/login.latte', array_merge(Flight::get('standardParams'), [
···
'ogurl' => 'https://'.SITE_DOMAIN.'/login'
192
-
$username = $_GET['username'];
193
-
$bskyToucher = new BskyToucher();
194
-
$userInfo = $bskyToucher->getUserInfo($username);
195
-
if (!$userInfo) die(1);
196
-
$pds = $userInfo->pds;
197
-
$options = new OAuthOptions([
198
-
'key' => 'https://'.SITE_DOMAIN.CLIENT_ID,
199
-
'secret' => CLIENT_SECRET,
200
-
'callbackURL' => 'https://'.SITE_DOMAIN.'/login',
201
-
'sessionStart' => true,
202
-
'sessionStorageVar' => 'sbs_'.SITE_DOMAIN
204
-
$connector = new React\Socket\Connector([
207
-
$http = new React\Http\Browser($connector);
208
-
$httpFactory = new HttpFactory();
209
-
$token_builder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
210
-
$algorithm = new Sha256();
211
-
$signing_key = InMemory::file(CERT_PATH);
212
-
$now = new DateTimeImmutable();
213
-
$token = $token_builder
214
-
->withHeader('alg', 'ES256')
215
-
->withHeader('typ', 'JWT')
216
-
->withHeader('kid', 'ocwgKj_O7H9at1sL6yWf9ZZ82NOM7D0xlN8HGIyWH6M')
217
-
->issuedBy('https://'.SITE_DOMAIN.CLIENT_ID)
218
-
->identifiedBy(uniqid())
219
-
->relatedTo('https://'.SITE_DOMAIN.CLIENT_ID)
220
-
->permittedFor($pds)
221
-
->issuedAt($now->modify('-5 seconds'))
222
-
->getToken($algorithm, $signing_key);
223
-
$client = new GuzzleHttp\Client([
226
-
'User-Agent' => USER_AGENT_STR,
227
-
'Authorization' => 'Bearer: '.$token->toString()
230
-
$provider = new BskyProvider($options, $client, $httpFactory, $httpFactory, $httpFactory);
231
-
$provider->setPds($pds);
232
-
$name = $provider->getName();
234
-
if (isset($_GET['login']) && $_GET['login'] === $name) {
235
-
$auth_url = $provider->getAuthorizationUrl([
236
-
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
237
-
'client_assertion' => $token->toString()
239
-
header('Location: '.$auth_url);
241
-
} else if (isset($_GET['code'], $_GET['state'])) {
242
-
$token = $provider->getAccessToken($_GET['code'], $_GET['state']);
243
-
$storage = new SessionStorage($options);
244
-
$storage->storeAccessToken($token, $name);
245
-
header('Location: ?granted='.$name);
247
-
} else if (isset($_GET['granted']) && $_GET['granted'] === $name) {
248
-
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();
249
-
header('Location: https://'.SITE_DOMAIN);
251
-
} else if (isset($_GET['error'])) {
254
-
$latte = new Latte\Engine;
255
-
$latte->render('./templates/login.latte', array_merge(Flight::get('standardParams'), [
256
-
'mainClass' => 'form',
257
-
'ogtitle' => SITE_TITLE." | login",
258
-
'ogdesc' => SITE_DESC,
260
-
'ogurl' => 'https://'.SITE_DOMAIN.'/login'
256
+
Flight::route('/logout', function(): void {
257
+
unset($_SESSION['sbs_'.SITE_DOMAIN]);
258
+
unset($_SESSION['sbs_'.SITE_DOMAIN.'_pds']);
259
+
unset($_SESSION['sbs_'.SITE_DOMAIN.'_userinfo']);
260
+
Flight::redirect('/');
Flight::route('/createaccount', function(): void {