Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Authentication tokens for admin users to use with REST and GraphQL API's, username sanitization
givanz0bc6e9d8cdc8
diff --git a/admin/controller/admin/auth-token.php b/admin/controller/admin/auth-token.phpnew file mode 100644index 0000000..485884c--- /dev/null+++ b/admin/controller/admin/auth-token.php@@ -0,0 +1,84 @@+<?php++/**+ * Vvveb+ *+ * Copyright (C) 2022 Ziadin Givan+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++namespace Vvveb\Controller\Admin;++use function Vvveb\__;+use Vvveb\Controller\Listing;+use function Vvveb\humanReadable;+use Vvveb\Sql\Admin_Auth_TokenSQL;+use function Vvveb\url;+///use Vvveb\System\Traits\Crud as CrudTrait;++class AuthToken extends Listing {+ /*+ use CrudTrait {+ CrudTrait::delete as get;+ }*/++ protected $type = 'admin_auth_token';++ protected $list = 'admin_auth_token';++ protected $listController = 'auth_token';++ protected $module = 'admin';++ function index() {+ $this->options['admin_id'] = $this->request->get['admin_id'] ?? false;+ $this->options['limit'] =1000;++ if (! $this->options['admin_id']) {+ return $this->notFound(true, __('User not found!'));+ }++ $this->view->user_url = url(['module' => 'admin/user', 'admin_id' => $this->options['admin_id']]);+ parent::index();+ }++ function save() {+ $admin_auth_token = $this->request->post['admin_auth_token'] ?? [];+ $admin_id = $this->request->get['admin_id'] ?? false;++ if ($admin_auth_token && $admin_id) {+ foreach ($admin_auth_token as $key => $token) {+ if ($key == '#') {+ unset($admin_auth_token[$key]);+ }++ if (! $token['admin_auth_token_id']) {+ unset($admin_auth_token[$key]['admin_auth_token_id']);+ }+ }++ $adminAuthToken = new Admin_Auth_TokenSQL();++ if (($result = $adminAuthToken->update(['admin_auth_token' => $admin_auth_token, 'admin_id' => $admin_id] + $this->global))/* && $result['admin_auth_token']*/) {+ $this->view->success['get'] =humanReadable(__($this->type)) . __(' saved!');+ } else {+ $this->view->errors[] = __('Error saving!');+ }+ }++ $this->index();+ }+}diff --git a/admin/controller/admin/user.php b/admin/controller/admin/user.phpindex 1fdbea0..dbce6f9 100755--- a/admin/controller/admin/user.php+++ b/admin/controller/admin/user.php@@ -34,13 +34,14 @@ class User extends UserBase {$roles = new \Vvveb\Sql\RoleSQL();$options = [- 'type' => 'admin', //$this->type,+ 'type' => 'admin', //$this->type,'limit'=> 100,] + $this->global;$roles = $roles->getAll($options);$sites = new SiteSQL();+ $this->view->admin_auth_token_url = \Vvveb\url(['module' => 'admin/auth-token', 'admin_id' => $this->request->get['admin_id'] ?? '']);$this->view->sitesList = $sites->getAll()['site'] ?? [];$this->view->roles = $roles ? $roles['role'] : [];}diff --git a/admin/sql/mysqli/admin_auth_token.sql b/admin/sql/mysqli/admin_auth_token.sqlnew file mode 100644index 0000000..ca62eb0--- /dev/null+++ b/admin/sql/mysqli/admin_auth_token.sql@@ -0,0 +1,192 @@+-- admin_auth_token++ -- get all entries++ CREATE PROCEDURE getAll(+ IN start INT,+ IN limit INT,+ IN admin_id INT,+ IN updated_at CHAR,++ -- return array of admin_auth_token+ OUT fetch_all,+ -- return admin_auth_token count for count query+ OUT fetch_one+ )+ BEGIN++ SELECT * FROM admin_auth_token WHERE 1 = 1++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND admin_auth_token.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND admin_auth_token.updated_at = :updated_at+ END @IF++ ORDER BY admin_auth_token.admin_id, admin_auth_token.updated_at++ -- limit+ @IF isset(:limit)+ THEN+ @SQL_LIMIT(:start, :limit)+ END @IF;++ -- SELECT FOUND_ROWS() as count;+ SELECT count(*) FROM (++ @SQL_COUNT(admin_auth_token.admin_id, admin) -- this takes previous query removes limit and replaces select columns with parameter admin_id++ ) as count;++ END++ -- get admin information++ CREATE PROCEDURE get(+ IN admin_id INT,+ IN updated_at CHAR,+ IN username CHAR,+ IN email CHAR,+ IN status INT,+ IN role_id INT,++ OUT fetch_row+ )+ BEGIN++ SELECT _.* FROM admin_auth_token AS _+ LEFT JOIN admin ON (admin.admin_id = _.admin_id)++ WHERE 1 = 1+++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND _.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND _.updated_at = :updated_at+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:status)+ THEN+ AND admin.status = :status+ END @IF++ @IF isset(:role_id)+ THEN+ AND admin.role_id = :role_id+ END @IF++ LIMIT 1;++ END++ -- Add new admin_auth_token++ CREATE PROCEDURE add(+ IN admin_auth_token ARRAY,+ OUT insert_id+ )+ BEGIN++ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ INSERT INTO admin_auth_token++ ( @KEYS(:admin_auth_token) )++ VALUES ( :admin_auth_token )+ END+++ -- Update admin_auth_token++ CREATE PROCEDURE edit(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT affected_rows+ )+ BEGIN+ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ UPDATE admin_auth_token++ SET @LIST(:admin_auth_token)++ WHERE admin_id = :admin_id;++ END++ CREATE PROCEDURE update(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT affected_rows+ )+ BEGIN++ @EACH(:admin_auth_token)+ INSERT INTO admin_auth_token++ ( @KEYS(:each), admin_id )++ VALUES ( :each, :admin_id)+ ON DUPLICATE KEY UPDATE token = :each.token, description = :each.description;++ END++ -- delete admin_auth_token++ PROCEDURE delete(+ IN admin_id INT,+ IN admin_auth_token_id ARRAY,+ IN updated_at CHAR,+ IN count INT,++ OUT affected_rows+ )+ BEGIN++ DELETE admin_auth_token FROM admin_auth_token+ INNER JOIN admin ON admin.admin_id = admin_auth_token.admin_id+ WHERE 1 = 1++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND admin.admin_id = :admin_id+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:admin_auth_token_id)+ THEN+ AND admin_auth_token.admin_auth_token_id IN (:admin_auth_token_id)+ END @IF++ ENDdiff --git a/admin/sql/pgsql/admin_auth_token.sql b/admin/sql/pgsql/admin_auth_token.sqlnew file mode 100644index 0000000..4e14469--- /dev/null+++ b/admin/sql/pgsql/admin_auth_token.sql@@ -0,0 +1,193 @@+-- admin_auth_token++ -- get all entries++ CREATE PROCEDURE getAll(+ IN start INT,+ IN limit INT,+ IN admin_id INT,+ IN updated_at CHAR,++ -- return array of admin_auth_token+ OUT fetch_all,+ -- return admin_auth_token count for count query+ OUT fetch_one+ )+ BEGIN++ SELECT * FROM admin_auth_token WHERE 1 = 1++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND admin_auth_token.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND admin_auth_token.updated_at = :updated_at+ END @IF++ ORDER BY admin_auth_token.admin_id, admin_auth_token.updated_at++ -- limit+ @IF isset(:limit)+ THEN+ @SQL_LIMIT(:start, :limit)+ END @IF;++ -- SELECT FOUND_ROWS() as count;+ SELECT count(*) FROM (++ @SQL_COUNT(admin_auth_token.admin_id, admin) -- this takes previous query removes limit and replaces select columns with parameter admin_id++ ) as count;++ END++ -- get admin information++ CREATE PROCEDURE get(+ IN admin_id INT,+ IN updated_at CHAR,+ IN username CHAR,+ IN email CHAR,+ IN status INT,+ IN role_id INT,++ OUT fetch_row+ )+ BEGIN++ SELECT _.* FROM admin_auth_token AS _+ LEFT JOIN admin ON (admin.admin_id = _.admin_id)++ WHERE 1 = 1+++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND _.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND _.updated_at = :updated_at+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:status)+ THEN+ AND admin.status = :status+ END @IF++ @IF isset(:role_id)+ THEN+ AND admin.role_id = :role_id+ END @IF++ LIMIT 1;++ END++ -- Add new admin_auth_token++ CREATE PROCEDURE add(+ IN admin_auth_token ARRAY,+ OUT insert_id+ )+ BEGIN++ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ INSERT INTO admin_auth_token++ ( @KEYS(:admin_auth_token) )++ VALUES ( :admin_auth_token )+ END+++ -- Update admin_auth_token++ CREATE PROCEDURE edit(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT fetch_one+ )+ BEGIN+ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ UPDATE admin_auth_token++ SET @LIST(:admin_auth_token)++ WHERE admin_id = :admin_id;++ END++ CREATE PROCEDURE update(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT fetch_one+ )+ BEGIN++ @EACH(:admin_auth_token)+ INSERT INTO admin_auth_token++ ( @KEYS(:each), admin_id )++ VALUES ( :each, :admin_id)+ ON CONFLICT ("admin_auth_token_id") DO UPDATE SET "token" = :each.token, "description" = :each.description;++ END++ -- delete admin_auth_token++ PROCEDURE delete(+ IN admin_id INT,+ IN admin_auth_token_id ARRAY,+ IN updated_at CHAR,+ IN count INT,++ OUT affected_rows+ )+ BEGIN++ DELETE FROM admin_auth_token USING admin+ WHERE++ admin.admin_id = admin_auth_token.admin_id++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND admin.admin_id = :admin_id+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:admin_auth_token_id)+ THEN+ AND admin_auth_token.admin_auth_token_id IN (:admin_auth_token_id)+ END @IF++ ENDdiff --git a/admin/sql/sqlite/admin_auth_token.sql b/admin/sql/sqlite/admin_auth_token.sqlnew file mode 100644index 0000000..9178dc4--- /dev/null+++ b/admin/sql/sqlite/admin_auth_token.sql@@ -0,0 +1,191 @@+-- admin_auth_token++ -- get all entries++ CREATE PROCEDURE getAll(+ IN start INT,+ IN limit INT,+ IN admin_id INT,+ IN updated_at CHAR,++ -- return array of admin_auth_token+ OUT fetch_all,+ -- return admin_auth_token count for count query+ OUT fetch_one+ )+ BEGIN++ SELECT * FROM admin_auth_token WHERE 1 = 1++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND admin_auth_token.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND admin_auth_token.updated_at = :updated_at+ END @IF++ ORDER BY admin_auth_token.admin_id, admin_auth_token.updated_at++ -- limit+ @IF isset(:limit)+ THEN+ @SQL_LIMIT(:start, :limit)+ END @IF;++ -- SELECT FOUND_ROWS() as count;+ SELECT count(*) FROM (++ @SQL_COUNT(admin_auth_token.admin_id, admin) -- this takes previous query removes limit and replaces select columns with parameter admin_id++ ) as count;++ END++ -- get admin information++ CREATE PROCEDURE get(+ IN admin_id INT,+ IN updated_at CHAR,+ IN username CHAR,+ IN email CHAR,+ IN status INT,+ IN role_id INT,++ OUT fetch_row+ )+ BEGIN++ SELECT _.* FROM admin_auth_token AS _+ LEFT JOIN admin ON (admin.admin_id = _.admin_id)++ WHERE 1 = 1+++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ AND _.admin_id = :admin_id+ END @IF++ @IF isset(:updated_at) AND !empty(:updated_at)+ THEN+ AND _.updated_at = :updated_at+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:status)+ THEN+ AND admin.status = :status+ END @IF++ @IF isset(:role_id)+ THEN+ AND admin.role_id = :role_id+ END @IF++ LIMIT 1;++ END++ -- Add new admin_auth_token++ CREATE PROCEDURE add(+ IN admin_auth_token ARRAY,+ OUT insert_id+ )+ BEGIN++ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ INSERT INTO admin_auth_token++ ( @KEYS(:admin_auth_token) )++ VALUES ( :admin_auth_token )+ END+++ -- Update admin_auth_token++ CREATE PROCEDURE edit(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT affected_rows+ )+ BEGIN+ -- allow only table fields and set defaults for missing values+ @FILTER(:admin_auth_token, admin_auth_token)++ UPDATE admin_auth_token++ SET @LIST(:admin_auth_token)++ WHERE admin_id = :admin_id;++ END++ CREATE PROCEDURE update(+ IN admin_id INT,+ IN admin_auth_token ARRAY,+ OUT affected_rows+ )+ BEGIN++ @EACH(:admin_auth_token)+ INSERT INTO admin_auth_token++ ( @KEYS(:each), admin_id )++ VALUES ( :each, :admin_id)+ ON CONFLICT(`admin_auth_token_id`) DO UPDATE SET `token` = :each.token,`description` = :each.description;+ END++ -- delete admin_auth_token++ PROCEDURE delete(+ IN admin_id INT,+ IN admin_auth_token_id ARRAY,+ IN updated_at CHAR,+ IN count INT,++ OUT affected_rows+ )+ BEGIN++ DELETE FROM admin_auth_token+ --INNER JOIN admin ON admin.admin_id = admin_auth_token.admin_id+ WHERE 1 = 1++ @IF isset(:admin_id) AND !empty(:admin_id)+ THEN+ -- AND admin.admin_id = :admin_id+ END @IF++ @IF isset(:username)+ THEN+ AND admin.username = :username+ END @IF++ @IF isset(:email)+ THEN+ AND admin.email = :email+ END @IF++ @IF isset(:admin_auth_token_id)+ THEN+ AND admin_auth_token.admin_auth_token_id IN (:admin_auth_token_id)+ END @IF++ ENDdiff --git a/admin/template/admin/auth-token.tpl b/admin/template/admin/auth-token.tplnew file mode 100644index 0000000..5975a4c--- /dev/null+++ b/admin/template/admin/auth-token.tpl@@ -0,0 +1,7 @@+import(listing.tpl, {"type":"admin_auth_token", "list": "admin_auth_tokens"})++[data-v-admin_auth_token] [type="text"][data-v-admin_auth_token-*]|name = <?php echo 'admin_auth_token[' . ($admin_auth_token['admin_auth_token_id'] ?? '#') . '][@@__data-v-admin_auth_token-(*)__@@]';?>+[data-v-admin_auth_token] [type="hidden"][data-v-admin_auth_token-*]|name = <?php echo 'admin_auth_token[' . ($admin_auth_token['admin_auth_token_id'] ?? '#') . '][@@__data-v-admin_auth_token-(*)__@@]';?>+[data-v-admin_auth_token] [type="password"][data-v-admin_auth_token-*]|name = <?php echo 'admin_auth_token[' . ($admin_auth_token['admin_auth_token_id'] ?? '#') . '][@@__data-v-admin_auth_token-(*)__@@]';?>++a[data-v-user-url]|href = $this->user_url\ No newline at end of filediff --git a/config/graphql.php b/config/graphql.phpindex 1836257..570b833 100644--- a/config/graphql.php+++ b/config/graphql.php@@ -22,7 +22,7 @@return ['auth' => [- 'mode' => 'http',+ 'mode' => 'token', //token = admin added token, http = basic auth using admin user and password],'session' => ['driver' => 'request',@@ -32,6 +32,11 @@],'cache' => ['driver' => 'file',+ 'servers' => [+ ],+ 'options' => [+ ],],- 'key' => 'Q8qJ6CVpUW4msSM8pHmdGwyXIkSvT6Lo'+ 'key' => 'NRtV7fInOn2RaX4piJ0aT1t8G2MkFClJ',+ 'cronkey' => 'SkLEcmLlETUC3dzoOEOU0r7jZwKi8SzO',];diff --git a/config/rest.php b/config/rest.phpindex 0fd61c4..53dd576 100644--- a/config/rest.php+++ b/config/rest.php@@ -22,16 +22,21 @@return ['auth' => [- 'mode' => 'http',+ 'mode' => 'token',//token = admin added token, http = basic auth using admin user and password],'session' => [- 'driver' => 'request',//stateless, handle session externally if needed. Change to php for session management if php session cookie can be sent with the request.+ 'driver' => 'request',],'email' => ['driver' => 'mail',],'cache' => ['driver' => 'file',+ 'servers' => [+ ],+ 'options' => [+ ],],- 'key' => 'Q8qJ6CVpUW4msSM8pHmdGwyXIkSvT6Lo'+ 'key' => 'FM4qmc4bDEldhU4Aafdu2HAhoH3lvZiW',+ 'cronkey' => 'eNRkZ894TTDxxOaKREsh5GnhAscYlHt5',];diff --git a/graphql/controller/base.php b/graphql/controller/base.phpindex ddffd0c..04a2829 100644--- a/graphql/controller/base.php+++ b/graphql/controller/base.php@@ -52,16 +52,67 @@ class Base {$password = $_SERVER['PHP_AUTH_PW'] ?? '';$loginData = compact('user', 'password');- if ($userInfo = Admin::login($loginData)) {+ if ($userInfo = Admin::login($loginData, [], $feedback)) {} else {$this->response->addHeader('WWW-Authenticate', 'Basic realm="REST Api"');$this->response->addHeader('HTTP/1.0 401 Unauthorized');- FrontController::notFound(false, __('Auth failed!'), 403);+ $message = __('Auth failed!');++ if (DEBUG) {+ $message .= ' ' . $feedback['message'] ?? [];+ $message .= " auth.mode = $authMode";+ }+ //FrontController::notFound(false, $message, 403);+ $this->notFound($message, 403);}} else {$this->response->addHeader('WWW-Authenticate', 'Basic realm="REST Api"');$this->response->addHeader('HTTP/1.0 401 Unauthorized');- FrontController::notFound(false, __('Auth failed!'), 403);+ $message = __('Auth failed!');++ if (DEBUG) {+ $message .= __('No credentials!');+ $message .= " auth.mode = $authMode";+ }+ //FrontController::notFound(false, $message, 403);+ $this->notFound($message, 403);+ }+ } else {+ if ($authMode == 'token') {+ $headers = getallheaders();+ $token = $headers['Bearer'] ?? $_SERVER['HTTP_BEARER'] ?? null;++ if (! $token) {+ $token = $headers['Authorization'] ?? $_SERVER['HTTP_AUTHORIZATION'] ?? '';+ $token = trim(substr($token, 6)) ?: $this->request->get['_token'] ?? false;+ }++ if ($token) {+ if ($userInfo = Admin::auth($token, [], $feedback)) {+ } else {+ $this->response->addHeader('HTTP/1.0 401 Unauthorized');+ $message = __('Auth failed!');++ if (DEBUG) {+ $message .= ' ' . $feedback['message'] ?? [];+ $message .= " auth.mode = $authMode";+ $message .= " token = $token";+ }+ //FrontController::notFound(false, $message, 403);+ $this->notFound($message, 403);+ }+ } else {+ $this->response->addHeader('HTTP/1.0 401 Unauthorized');+ $message = __('Auth failed!');++ if (DEBUG) {+ $message .= __('No credentials!');+ $message .= " auth.mode = $authMode";+ $message .= " token = $token";+ }+ //FrontController::notFound(false, $message, 403);+ $this->notFound($message, 403);+ }}}}@@ -80,7 +131,7 @@ class Base {$this->response->setType('json');if (! GRAPHQL) {- die($this->notFound(__('GRAPHQL is disabled!'), 404));+ $this->notFound(__('GRAPHQL is disabled!'), 404);}$this->auth();@@ -130,6 +181,8 @@ class Base {$response = Response::getInstance();http_response_code($statusCode);- return $response->output(['errors' => is_array($message) ? $message : ['message' => $message]]);+ $response->output(['errors' => is_array($message) ? $message : ['message' => $message]]);++ die();}}diff --git a/install/sql/mysqli/schema/admin/admin_auth_token.sql b/install/sql/mysqli/schema/admin/admin_auth_token.sqlnew file mode 100644index 0000000..88da8c6--- /dev/null+++ b/install/sql/mysqli/schema/admin/admin_auth_token.sql@@ -0,0 +1,12 @@+DROP TABLE IF EXISTS `admin_auth_token`;++CREATE TABLE `admin_auth_token` (+ `admin_auth_token_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,+ `admin_id` INT UNSIGNED NOT NULL,+ `token` varchar(191) NOT NULL,+ `description` varchar(191) NOT NULL DEFAULT '',+ `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,+ `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,+ PRIMARY KEY (`admin_auth_token_id`),+ KEY `token` (`token`, `admin_id`)+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;diff --git a/install/sql/pgsql/schema/admin/admin_auth_token.sql b/install/sql/pgsql/schema/admin/admin_auth_token.sqlnew file mode 100644index 0000000..55760ef--- /dev/null+++ b/install/sql/pgsql/schema/admin/admin_auth_token.sql@@ -0,0 +1,16 @@+DROP TABLE IF EXISTS "admin_auth_token";++-- DROP SEQUENCE IF EXISTS admin_admin_id_seq;+-- CREATE SEQUENCE admin_admin_id_seq;++CREATE TABLE "admin_auth_token" (+ "admin_auth_token_id" SERIAL PRIMARY KEY,+ "admin_id" int check ("admin_id" > 0) NOT NULL,+ "token" varchar(191) NOT NULL DEFAULT '',+ "description" varchar(191) NOT NULL DEFAULT '',+ "created_at" timestamp(0) NOT NULL DEFAULT now(),+ "updated_at" timestamp(0) NOT NULL DEFAULT now()+);++CREATE INDEX "admin_auth_token_token" ON admin ("token", "admin_id");+diff --git a/install/sql/sqlite/schema/admin/admin_auth_token.sql b/install/sql/sqlite/schema/admin/admin_auth_token.sqlnew file mode 100644index 0000000..2116b92--- /dev/null+++ b/install/sql/sqlite/schema/admin/admin_auth_token.sql@@ -0,0 +1,12 @@+DROP TABLE IF EXISTS `admin_auth_token`;++CREATE TABLE `admin_auth_token` (+ `admin_auth_token_id` INTEGER PRIMARY KEY AUTOINCREMENT,+ `admin_id` INT UNSIGNED NOT NULL,+ `token` TEXT NOT NULL,+ `description` TEXT NOT NULL DEFAULT '',+ `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,+ `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP+);++CREATE INDEX `admin_auth_token_token` ON `admin` (`token`,`admin_id`);\ No newline at end of filediff --git a/system/user/admin.php b/system/user/admin.phpindex ab603ed..6973db8 100755--- a/system/user/admin.php+++ b/system/user/admin.php@@ -22,6 +22,7 @@namespace Vvveb\System\User;+use function Vvveb\__;use function Vvveb\session as sess;use Vvveb\Sql\AdminSQL;use Vvveb\System\PageCache;@@ -53,6 +54,8 @@ class Admin extends Auth {return false;}+ self::sanitize($data);+//check if email or username is already registered$check = ['email'=> $data['email']];@@ -109,6 +112,7 @@ class Admin extends Auth {$admin = new AdminSQL();self::setUserData($data);+ self::sanitize($data);return $admin->edit(array_merge([self :: $namespace => $data], $condition));}@@ -117,32 +121,10 @@ class Admin extends Auth {$loginInfo = []; //['status' => 1];$adminInfo = false;- if (isset($data['email'])) {- $loginInfo['email'] = $data['email'];- }-- if (isset($data['user'])) {- $loginInfo['username'] = $data['user'];- }-- if (isset($data['username'])) {- $loginInfo['username'] = $data['username'];- }-- if (isset($data['role_id'])) {- $loginInfo['role_id'] = $data['role_id'];- }-- if (isset($data['user_id'])) {- $loginInfo['user_id'] = $data['user_id'];- }-- if (isset($data['token'])) {- $loginInfo['token'] = $data['token'];- }-- if (isset($data['status'])) {- $loginInfo['status'] = $data['status'];+ foreach (['email', 'user', 'username', 'role_id', 'user_id', 'token', 'admin_auth_token', 'admin_auth_token', 'status'] as $key) {+ if (isset($data[$key])) {+ $loginInfo[$key] = $data[$key];+ }}if ($loginInfo) {@@ -165,13 +147,63 @@ class Admin extends Auth {return $adminInfo;}- public static function login($data, $additionalInfo = []) {+ public static function auth($admin_auth_token, $additionalInfo = [], &$feedback = []) {+ //check admin email and that status is active+ $data['status'] = 1;+ $data['admin_auth_token'] = $admin_auth_token;+ $adminInfo = self::get($data);+ $userExists = ($adminInfo && isset($adminInfo['password']));++ if ($userExists) {+ $passwordCorrect = $data['admin_auth_token'] == $admin_auth_token;+ }++ if (! $userExists || ! $passwordCorrect) {+ if ($userExists) {+ $message = __('Token incorrect!');+ $code = 0;+ } else {+ $message = __('User not found or has status inactive!');+ $code = 1;+ }++ $feedback = ['message' => $message, 'code' => $code];++ return false;+ }++ $session = Session :: getInstance();+ $session->regenerateId(true);+ unset($adminInfo['password']);+ $session->set(self :: $namespace, $adminInfo + $additionalInfo);++ PageCache::disable('user');++ return $adminInfo;+ }++ public static function login($data, $additionalInfo = [], &$feedback = []) {//check admin email and that status is active- $data['status'] = 1;- $adminInfo = self::get($data);+ $data['status'] = 1;+ $adminInfo = self::get($data);+ $passwordCorrect = false;+ $userExists = ($adminInfo && isset($adminInfo['password']));++ if ($userExists) {+ $passwordCorrect = self::checkPassword($data['password'], $adminInfo['password']);+ }++ if (! $userExists || ! $passwordCorrect) {+ if ($userExists) {+ $message = __('Password incorrect!');+ $code = 0;+ } else {+ $message = __('User not found or has status inactive!');+ $code = 1;+ }++ $feedback = ['message' => $message, 'code' => $code];- if (! ($adminInfo && isset($adminInfo['password'])) ||- ! self::checkPassword($data['password'], $adminInfo['password'])) {return false;}diff --git a/system/user/auth.php b/system/user/auth.phpindex e127832..2820942 100755--- a/system/user/auth.php+++ b/system/user/auth.php@@ -25,6 +25,16 @@ namespace Vvveb\System\User;class Auth {static $options = ['cost' => 11];+ public static function sanitize(&$data) {+ if (isset($data['username'])) {+ $data['username'] = preg_replace('/[^\w-]/', '', $data['username']);+ }++ if (isset($data['email'])) {+ $data['email'] = filter_var($data['email'], FILTER_VALIDATE_EMAIL);+ }+ }+public static function checkPassword($password, $hash) {if (password_verify($password, $hash)) {// Check if a newer hashing algorithm is available