From: Michael Wallner Date: Wed, 13 May 2015 15:37:04 +0000 (+0200) Subject: login with account cookie X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=9815b77a14832685f90d34f9a348af1cadd63576;p=pharext%2Fpharext.org login with account cookie --- diff --git a/app/Controller/Github.php b/app/Controller/Github.php index 164f320..7b7bdf6 100644 --- a/app/Controller/Github.php +++ b/app/Controller/Github.php @@ -4,11 +4,14 @@ namespace app\Controller; use app\Controller; use app\Github\API; +use app\Model\Account; +use app\Model\Owner; +use app\Model\Token; use app\Session; use app\Web; - -use http\QueryString; +use http\Cookie; use http\Header; +use http\QueryString; abstract class Github implements Controller { @@ -18,12 +21,12 @@ abstract class Github implements Controller protected $app; /** - * @var \app\Github\API + * @var API */ protected $github; /** - * @var \app\Session + * @var Session */ protected $session; @@ -47,6 +50,19 @@ abstract class Github implements Controller } } + protected function login(Account $account, Token $token, Owner $owner) { + $auth = new Cookie; + $auth->setCookie("account", $account->account->get()); + $auth->setFlags(Cookie::SECURE | Cookie::HTTPONLY); + $auth->setPath($this->app->getBaseUrl()->path); + $auth->setMaxAge(60*60*24); + $this->app->getResponse()->setCookie($auth); + + $this->github->setToken($token->token->get()); + $this->session->account = $account->account->get(); + $this->session->github = (object) $owner->export(); + } + protected function checkToken() { if ($this->github->hasToken()) { return true; diff --git a/app/Controller/Github/Callback.php b/app/Controller/Github/Callback.php index 646d782..e6429fc 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 { @@ -45,20 +46,21 @@ class Callback extends Github $this->app->display("github/callback"); } - function createUserCallback($token) { - return function($user) use($token) { + function createUserCallback($oauth) { + return function($user) use($oauth) { $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); + + 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); } - $account->updateToken("github", $token->access_token, $token); + $token = $account->updateToken("github", $oauth->access_token, $oauth); $owner = $account->updateOwner("github", $user->login, $user); $tx->commit(); - $this->session->account = $account->account->get(); - $this->session->github = (object) $owner->export(); + $this->login($account, $token, $owner); }; } } diff --git a/app/Controller/Github/Signin.php b/app/Controller/Github/Signin.php index 9fca554..d753687 100644 --- a/app/Controller/Github/Signin.php +++ b/app/Controller/Github/Signin.php @@ -3,10 +3,47 @@ namespace app\Controller\Github; use app\Controller\Github; +use app\Github\API; +use app\Model\Accounts; +use app\Session; +use app\Web; class Signin extends Github { + /** + * @var Accounts + */ + private $accounts; + + function __construct(Web $app, API $github, Session $session, Accounts $accounts) { + parent::__construct($app, $github, $session); + $this->accounts = $accounts; + } + function __invoke(array $args = null) { + if (($cookie = $this->app->getRequest()->getCookie("account"))) { + $accounts = $this->accounts->find(["account=" => $cookie]); + if (count($accounts)) { + $account = $accounts->current(); + $tokens = $account->allOf("tokens")->filter(function($token) { + return $token->authority == "github"; + }); + if (count($tokens)) { + $token = $tokens->current(); + $this->login($account, $token, + $account->allOf("owners")->filter(function($owner) { + return $owner->authority == "github"; + })->current() + ); + if (($returnto = $this->app->getRequest()->getQuery("returnto"))) { + $this->app->redirect($returnto); + } else { + $this->app->redirect($this->app->getBaseUrl()->mod("./github")); + } + return; + } + } + } $callback = $this->app->getBaseUrl()->mod("./github/callback"); $location = $this->github->getAuthUrl($callback); $this->app->redirect($location); diff --git a/app/Github/API.php b/app/Github/API.php index 1c9ff78..1337326 100644 --- a/app/Github/API.php +++ b/app/Github/API.php @@ -49,7 +49,8 @@ class API function __construct(Config $config, LoggerInterface $logger, Storage $tokens = null, Storage $cache = null) { $this->logger = $logger; $this->config = $config; - $this->client = new Client; + $this->client = new Client("curl", "github"); + $this->client->configure($config->http->configure); $this->client->attach(new ClientObserver($logger)); $this->tokens = $tokens ?: new Storage\Session; $this->cache = $cache; diff --git a/app/bootstrap/model.php b/app/bootstrap/model.php index 105125e..f9f729b 100644 --- a/app/bootstrap/model.php +++ b/app/bootstrap/model.php @@ -22,6 +22,10 @@ $injector->define(Model\Accounts::class, [ "conn" => Connection::class, ]); +\pq\Gateway\Table::$defaultResolver = function($table) use($injector) { + return $injector->make("app\\Model\\" . ucfirst($table)); +}; + //$modelconf = function($key, $injector) { // return new Table($key, $injector->make(Connection::class)); //}; diff --git a/config/app.ini b/config/app.ini index 201de74..7b9ca38 100644 --- a/config/app.ini +++ b/config/app.ini @@ -6,6 +6,8 @@ github.api.accept = application/vnd.github.v3+json github.api.call.listrepos.args.per_page = 10 github.api.call.listhooks.args.per_page = 100 +github.http.configure.pipelining = true + github.hook.url = https://pharext.org/github/hook github.hook.content_type = json github.hook.insecure_ssl = 0