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
··· 68 68 Flight::set('frontpageFeed', FRONTPAGE_FEED); 69 69 Flight::set('defaultRelay', DEFAULT_RELAY); 70 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); 71 73 Flight::set('flight.log_errors', false); 72 74 Flight::set('flight.handle_errors', false); 73 75 Flight::set('flight.content_length', false); ··· 79 81 'setTheme' => array_key_exists('sbs_theme', $_COOKIE) ? $_COOKIE['sbs_theme'] : DEFAULT_THEME, 80 82 'setFont' => array_key_exists('sbs_font', $_COOKIE) ? $_COOKIE['sbs_font'] : DEFAULT_FONT, 81 83 'userAuth' => Flight::get('userAuth'), 84 + 'userPds' => Flight::get('userPds'), 85 + 'userInfo' => Flight::Get('userInfo'), 82 86 'favFeeds' => $favoriteFeeds, 83 87 'pages' => PAGES, 84 88 'links' => LINKS, ··· 178 182 }); 179 183 180 184 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 197 + ]); 198 + $storage = new SessionStorage($options); 199 + $connector = new React\Socket\Connector([ 200 + 'dns' => '1.1.1.1' 201 + ]); 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([ 219 + 'verify' => true, 220 + 'headers' => [ 221 + 'User-Agent' => USER_AGENT_STR, 222 + 'Authorization' => 'Bearer: '.$token->toString() 223 + ] 224 + ]); 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() 232 + ]); 233 + Flight::redirect($auth_url); 234 + die(1); 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('/'); 240 + die(1); 241 + } else if (isset($_GET['error'])) { 242 + die(1); 243 + } 244 + } else { 182 245 $latte = new Latte\Engine; 183 246 $latte->render('./templates/login.latte', array_merge(Flight::get('standardParams'), [ 184 247 'mainClass' => 'form', ··· 187 250 'ogimage' => '', 188 251 'ogurl' => 'https://'.SITE_DOMAIN.'/login' 189 252 ])); 190 - die(1); 191 253 } 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 203 - ]); 204 - $storage = new SessionStorage($options); 205 - echo 'get access token:'; 206 - print_r($storage->getAccessToken($name)); 207 - $connector = new React\Socket\Connector([ 208 - 'dns' => '1.1.1.1' 209 - ]); 210 - $http = new React\Http\Browser($connector); 211 - $httpFactory = new HttpFactory(); 212 - $token_builder = Builder::new(new JoseEncoder(), ChainedFormatter::default()); 213 - $algorithm = new Sha256(); 214 - $signing_key = InMemory::file(CERT_PATH); 215 - $now = new DateTimeImmutable(); 216 - $token = $token_builder 217 - ->withHeader('alg', 'ES256') 218 - ->withHeader('typ', 'JWT') 219 - ->withHeader('kid', 'ocwgKj_O7H9at1sL6yWf9ZZ82NOM7D0xlN8HGIyWH6M') 220 - ->issuedBy('https://'.SITE_DOMAIN.CLIENT_ID) 221 - ->identifiedBy(uniqid()) 222 - ->relatedTo('https://'.SITE_DOMAIN.CLIENT_ID) 223 - ->permittedFor($pds) 224 - ->issuedAt($now->modify('-5 seconds')) 225 - ->getToken($algorithm, $signing_key); 226 - $client = new GuzzleHttp\Client([ 227 - 'verify' => true, 228 - 'headers' => [ 229 - 'User-Agent' => USER_AGENT_STR, 230 - 'Authorization' => 'Bearer: '.$token->toString() 231 - ] 232 - ]); 233 - $provider = new BskyProvider($options, $client, $httpFactory, $httpFactory, $httpFactory); 234 - $provider->setPds($pds); 235 - $name = $provider->getName(); 236 - 237 - if (isset($_GET['login']) && $_GET['login'] === $name) { 238 - $auth_url = $provider->getAuthorizationUrl([ 239 - 'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer', 240 - 'client_assertion' => $token->toString() 241 - ]); 242 - header('Location: '.$auth_url); 243 - die(1); 244 - } else if (isset($_GET['code'])) { 245 - // https://smallbird.social/login?iss=https%3A%2F%2Fshimaenaga.veryroundbird.house&code=cod-3a80b9095e871dc4bc2391f876cc234abefcbb66ca65e8a381e99095b8899b0e 246 - $token = $provider->getAccessToken($_GET['code'], null); 247 - $storage->storeAccessToken($token, $name); 248 - header('Location: https://'.SITE_DOMAIN); 249 - die(1); 250 - } else if (isset($_GET['error'])) { 251 - die(1); 252 - } 254 + }); 255 + 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('/'); 253 261 }); 254 262 255 263 Flight::route('/createaccount', function(): void {
+1 -1
templates/_partials/nav.latte
··· 1 1 <nav> 2 2 <ul> 3 3 {if $userAuth} 4 - <li><a href="/">profile</a></li> 4 + <li><a href="/u/{$userInfo->handle}">profile</a></li> 5 5 <li><a href="/settings">settings</a></li> 6 6 <li><a href="/logout">log out</a></li> 7 7 {else}
+4
templates/layout.latte
··· 24 24 data-theme="{$setTheme}" 25 25 data-font="{$setFont}" 26 26 > 27 + <!-- 28 + {print_r($_SESSION)} 29 + {print_r(PHP_SESSION_DISABLED)} 30 + --> 27 31 <div id="page"> 28 32 <header> 29 33 <h1><a href="/">{include '_partials/logo.latte'}{$siteTitle}</a></h1>