From d70cd8c1df115a721e72a00208c2a37870333cdf Mon Sep 17 00:00:00 2001 From: Christoph Burschka Date: Wed, 15 Jan 2014 20:40:07 +0100 Subject: First step toward a session-auth plugin. (See #1, and cburschka/cadence#31) --- plugins/session/BridgeSession.php | 46 +++++++++++++++++++++++++++++++++++++++ plugins/session/install.sql | 5 +++++ plugins/session/session.module | 10 +++++++++ plugins/session/www/rpc.php | 25 +++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 plugins/session/BridgeSession.php create mode 100644 plugins/session/install.sql create mode 100644 plugins/session/session.module create mode 100644 plugins/session/www/rpc.php diff --git a/plugins/session/BridgeSession.php b/plugins/session/BridgeSession.php new file mode 100644 index 0000000..700d545 --- /dev/null +++ b/plugins/session/BridgeSession.php @@ -0,0 +1,46 @@ +db = $pdo; + $this->timeout = $config['timeout']; + $this->table = mysqli::escape_string($config['mysql'['tablename']); + $this->_isuser = $this->db->prepare(sprintf('SELECT COUNT(*) FROM `%s` WHERE `username` = :user AND `created` >= :limit;', $this->table)); + $this->_auth = $this->db->prepare(sprintf('DELETE COUNT(*) FROM `%s` WHERE `username` = :user AND `secret` = :secret AND `created` >= :limit;', $this->table)); + $this->_prune = $this->db->prepare(sprintf('DELETE COUNT(*) FROM `%s` WHERE `created` < :limit;', $this->table)); + $this->_create = $this->db->prepare(sprintf('INSERT INTO `%s` (`username`, `secret`, `created`) VALUES (:user, :secret, :time);', $this->table)); + } + + function create($entry) { + $this->_create->execute([':user' => $entry['user'], ':secret' => $entry['secret'], ':time' => $entry['time']]); + } + + function prune() { + $this->_prune->execute([':limit' => time() - $this->timeout]); + } + + function isuser($username, $server) { + $this->prune(); + return $this->_isuser->execute([':user' => $username, ':limit' => time() - $this->timeout])->fetch()[0] > 0; + } + + function auth($username, $server, $password) { + $this->prune(); + return $this->_auth->execute([[':useer' => $username, ':secret' => $password, ':limit' => time() - $this->timeout])->rowCount() > 0; + } + + function setpass($username, $server, $password) { + return FALSE; + } + + function tryregister($username, $server, $password) { + return FALSE; + } + + function removeuser($username, $server) { + return FALSE; + } +} diff --git a/plugins/session/install.sql b/plugins/session/install.sql new file mode 100644 index 0000000..074e4a9 --- /dev/null +++ b/plugins/session/install.sql @@ -0,0 +1,5 @@ +CREATE TABLE `{TAB}` ( + username TEXT PRIMARY KEY, + secret VARCHAR(40) PRIMARY KEY, + created INT INDEX, +); diff --git a/plugins/session/session.module b/plugins/session/session.module new file mode 100644 index 0000000..ed1771c --- /dev/null +++ b/plugins/session/session.module @@ -0,0 +1,10 @@ + 'SET NAMES utf8']; + $pdo = new PDO($v['dsn'], $v['username'], $v['password'], $options); + + require_once __DIR__ . '/BridgeSession.php'; + return new BridgeSession($pdo, $config); +} diff --git a/plugins/session/www/rpc.php b/plugins/session/www/rpc.php new file mode 100644 index 0000000..58f3634 --- /dev/null +++ b/plugins/session/www/rpc.php @@ -0,0 +1,25 @@ + $username, 'secret' => sha1($_POST['salt'] . time() . mt_rand()), 'time' => time()]; + $bridge->create($entry); + header('Content-type: text/plain; charset=UTF-8'); + print json_encode($entry); + } + else header('HTTP/1.1 403 Forbidden'); +} -- cgit v1.1