X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FController%2FGithub%2FCallback.php;h=2347d0e67069ca946dbea6c924af10e59b312f06;hb=f71f7713ee19492c1355efb9bef8ecb5e01f6a4c;hp=646d782f20d52c59676efdbd23a72a39ddd3c5ba;hpb=36cfa28cf2dcee3422f0231f91c6692eb28e7824;p=pharext%2Fpharext.org diff --git a/app/Controller/Github/Callback.php b/app/Controller/Github/Callback.php index 646d782..2347d0e 100644 --- a/app/Controller/Github/Callback.php +++ b/app/Controller/Github/Callback.php @@ -7,6 +7,7 @@ use app\Github\API; use app\Model\Accounts; use app\Session; use app\Web; +use http\Cookie; class Callback extends Github { @@ -22,43 +23,51 @@ class Callback extends Github function __invoke(array $args = null) { if ($this->app->getRequest()->getQuery("error")) { - $this->app->getView()->addData([ - "error" => $this->app->getRequest()->getQuery("error_description") - ]); + $this->app->getView()->addData($this->app->getRequest()->getQuery()->toArray()); } else { - $this->github->fetchToken( - $this->app->getRequest()->getQuery("code"), - $this->app->getRequest()->getQuery("state"), - function($token) { - $this->github->setToken($token->access_token); - $this->github->readAuthUser($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]; } }