X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FController%2FGithub%2FCallback.php;h=bd8c0e5abe6dc7f281aa894174f42bee95c194f0;hb=8b9e6a11bae0db302dd100ce288b07ef7995e372;hp=496baf331f9973def275201755e1ad658ca51c3c;hpb=07b87ac26f62bc3c069bb16983fe7500272e19b4;p=pharext%2Fpharext.org diff --git a/app/Controller/Github/Callback.php b/app/Controller/Github/Callback.php index 496baf3..bd8c0e5 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,39 +27,49 @@ class Callback extends Github "error" => $this->app->getRequest()->getQuery("error_description") ]); } else { - $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(); + $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->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) use (&$oauth) { + list($oauth) = $result; + $this->github->setToken($oauth->access_token); + return $this->github->readAuthUser(); + })->done(function($result) use(&$oauth) { + list($user) = $result; + $this->login(...$this->persistUser($oauth, $user)); + }); + + $this->github->drain(); + } + + 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]; } }