login with account cookie
authorMichael Wallner <mike@php.net>
Wed, 13 May 2015 15:37:04 +0000 (17:37 +0200)
committerMichael Wallner <mike@php.net>
Wed, 13 May 2015 15:37:04 +0000 (17:37 +0200)
app/Controller/Github.php
app/Controller/Github/Callback.php
app/Controller/Github/Signin.php
app/Github/API.php
app/bootstrap/model.php
config/app.ini

index 164f3205134070d8464ca6927382151f9f050594..7b7bdf64d976547cee05bd7331439cfe6c102a5f 100644 (file)
@@ -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;
index 646d782f20d52c59676efdbd23a72a39ddd3c5ba..e6429fcf7de18f57ef2b313fa2247224b75b7cc4 100644 (file)
@@ -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);
                };
        }
 }
index 9fca55481886af5321fe0157f4206cd55bb5a611..d753687a3b7928ed739bc4b0bc66dbc54dbbad8b 100644 (file)
@@ -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);
index 1c9ff7834e772a92cc8fb758bb8701bfaae6bf88..13373269ac86c30d144eff3d78addb2f8f49bcce 100644 (file)
@@ -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;
index 105125e64e34e5a021fac34b20e68b91d6b3102f..f9f729b6a5ae2a9cff158d5f9202f839a151440f 100644 (file)
@@ -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));
 //};
index 201de74a4ca65f5d6c5fd871bc615d17d20ece93..7b9ca3871b302575fd91693e9e0c838157a28876 100644 (file)
@@ -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