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
{
protected $app;
/**
- * @var \app\Github\API
+ * @var API
*/
protected $github;
/**
- * @var \app\Session
+ * @var Session
*/
protected $session;
}
}
+ 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;
use app\Model\Accounts;
use app\Session;
use app\Web;
+use http\Cookie;
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);
};
}
}
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);
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;
"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));
//};
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