X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FController%2FGithub%2FCallback.php;h=baa6c4d47389f0d4aba92eede281c180f2e742f0;hb=2198c781a021d85ee845f2f4b2f5c322f73e6bd5;hp=d6d11920c6bd14ab3ecd465356536b1b49e94175;hpb=26494fb5cdfb9cf103904d10b6bda564bcf2d0bd;p=pharext%2Fpharext.org diff --git a/app/Controller/Github/Callback.php b/app/Controller/Github/Callback.php index d6d1192..baa6c4d 100644 --- a/app/Controller/Github/Callback.php +++ b/app/Controller/Github/Callback.php @@ -4,14 +4,13 @@ namespace app\Controller\Github; use app\Controller\Github; use app\Github\API; -use app\Github\Exception; use app\Model\Accounts; use app\Session; use app\Web; +use http\Cookie; class Callback extends Github { - /** * @var Accounts */ @@ -28,43 +27,51 @@ class Callback extends Github "error" => $this->app->getRequest()->getQuery("error_description") ]); } else { - try { - $this->github->fetchToken( - $this->app->getRequest()->getQuery("code"), - $this->app->getRequest()->getQuery("state"), - function($token) { - $this->github->setToken($token->access_token); - $this->github->fetchUser($this->createUserCallback($token)); - })->send(); - if (isset($this->session->returnto)) { - $returnto = $this->session->returnto; - unset($this->session->returnto); - $this->app->redirect($returnto); - } else { - $this->app->redirect( - $this->app->getBaseUrl()->mod("./github")); - } - } catch (Exception $exception) { - $this->app->getView()->addData(compact("exception")); + $this->validateUser(); + + if (isset($this->session->returnto)) { + $returnto = $this->session->returnto; + unset($this->session->returnto); + $this->app->redirect($returnto); + } else { + $this->app->redirect( + $this->app->getBaseUrl()->mod("./github")); } } $this->app->display("github/callback"); } - function createUserCallback($token) { - return function($user) use($token) { - $tx = $this->accounts->getConnection()->startTransaction(); - - if (!($account = $this->accounts->byOAuth("github", $token->access_token, $user->login))) { - $account = $this->accounts->createOAuthAccount("github", $token->access_token, $user->login); - } - $account->updateToken("github", $token->access_token, $token); - $owner = $account->updateOwner("github", $user->login, $user); - - $tx->commit(); - - $this->session->account = $account->account->get(); - $this->session->github = (object) $owner->export(); - }; + private function validateUser() { + $this->github->fetchToken( + $this->app->getRequest()->getQuery("code"), + $this->app->getRequest()->getQuery("state") + )->then(function($result) { + list($oauth) = $result; + $this->github->setToken($oauth->access_token); + return $this->github->readAuthUser()->then(function($result) use($oauth) { + list($user) = $result; + return $this->persistUser($oauth, $user); + }); + })->done(function($result) { + $this->login(...$result); + }); + + $this->github->getClient()->send(); + } + + private function persistUser($oauth, $user) { + $tx = $this->accounts->getConnection()->startTransaction(); + + if (($cookie = $this->app->getRequest()->getCookie("account"))) { + $account = $this->accounts->find(["account=" => $cookie])->current(); + } elseif (!($account = $this->accounts->byOAuth("github", $oauth->access_token, $user->login))) { + $account = $this->accounts->createOAuthAccount("github", $oauth->access_token, $user->login); + } + $token = $account->updateToken("github", $oauth->access_token, $oauth); + $owner = $account->updateOwner("github", $user->login, $user); + + $tx->commit(); + + return [$account, $token, $owner]; } }