From 40df3b2fd6a8f7f30119672fb6ef2cde3ac7742e Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 5 Jun 2023 19:48:33 +0300 Subject: [PATCH] v0.0.10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Добавлена авторизация как отдельная страница + Небольшие исправления в JS при ответе от сервера --- js/authorization.js | 48 +++++++++++++++++++++++ js/script.js | 40 ++++++++++++++----- public/authorization.css | 64 +++++++++++++++++++++++++++++++ source/daster.d | 68 ++++++++++++++++++++++----------- source/requests/authorization.d | 47 +++++++++++++++++++++++ source/structures.d | 9 +++++ source/version_.d | 2 +- views/authorization.dt | 26 +++++++++++++ 8 files changed, 271 insertions(+), 33 deletions(-) create mode 100644 js/authorization.js create mode 100644 public/authorization.css create mode 100644 source/requests/authorization.d create mode 100644 views/authorization.dt diff --git a/js/authorization.js b/js/authorization.js new file mode 100644 index 0000000..4edab3f --- /dev/null +++ b/js/authorization.js @@ -0,0 +1,48 @@ +$(document).ready(function () { + noticer = new Noticer; + + $("#authorization").button({ icon: "ui-icon-home" }); + + $("#login, #password").on('keypress',function(e) { + if(e.which == 13) { + authorization() + } + }); + + $("#authorization").click(() => { + authorization() + }); + + $("body").fadeTo(500, 1); +}); + +function authorization() { + request().then((data) => { + data.error ? noticer.error(data.message) : (window.location.href = "."); + }).catch((e) => { + noticer.error(e.message); + }); +} + +async function request() { + let login = $("#login").val(); + let password = $("#password").val(); + + let response = await fetch('.', { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8' + }, + body: JSON.stringify({ + login: login, + password: password, + query: "login" + }) + }); + + if (!response.ok) + throw new Error(`Произошла неизвестаня ошибка: ${response.status}`); + + const data = await response.json(); + return data; +} diff --git a/js/script.js b/js/script.js index 583147f..7a00ef9 100644 --- a/js/script.js +++ b/js/script.js @@ -58,6 +58,14 @@ $(document).ready(function () { e.key == "Escape" && ($(this).val(""), lists[$("#tabs").tabs( "option", "active" )]()) }); + $("#logout").click(() => { + request('logout', 'json').then(data => { + data.error ? noticer.error(data.message) : (window.location.href = "."); + }).catch(error => { + noticer.error(error.message); + }); + }); + loadNumbers(); loadSMS(); loadUSSD(); @@ -129,7 +137,10 @@ function pEmpty(text) { function loadNumbers() { request('listnumbergroups', 'text').then(data => { - data.error ? noticer.error(data.message) : generateListNumberGroups(data); + if (isJSON(data) && JSON.parse(data).error) + noticer.error(JSON.parse(data).message); + else + generateListNumberGroups(data); }).catch(error => { noticer.error(error.message); }); @@ -161,8 +172,8 @@ function generateListGroupNumbers(panel) { } request('listgroupnumbers', 'json', { group: panel.data("group-name") }).then(data => { - if (isJSON(data) && JSON.parse(data).error) - noticer.error(JSON.parse(data).message); + if (data.error) + noticer.error(data.message); else { numbers = data; showListNumbers(panel); @@ -347,7 +358,10 @@ function delNumber(panel, currentWindow) { function loadSMS() { request('listsmsgroups', 'text').then(data => { - data.error ? noticer.error(data.message) : generateListSMSGroups(data); + if (isJSON(data) && JSON.parse(data).error) + noticer.error(JSON.parse(data).message); + else + generateListSMSGroups(data); }).catch(error => { noticer.error(error.message); }); @@ -379,8 +393,8 @@ function generateListGroupSMS(panel) { } request('listgroupsms', 'json', { to: panel.data("to") }).then(data => { - if (isJSON(data) && JSON.parse(data).error) - noticer.error(JSON.parse(data).message); + if (data.error) + noticer.error(data.message); else { sms = data; showListSMS(panel); @@ -483,7 +497,10 @@ function delSMS(panel, currentWindow) { function loadUSSD() { request('listussdgroups', 'text').then(data => { - data.error ? noticer.error(data.message) : generateListUSSDGroups(data); + if (isJSON(data) && JSON.parse(data).error) + noticer.error(JSON.parse(data).message); + else + generateListUSSDGroups(data); }).catch(error => { noticer.error(error.message); }); @@ -515,8 +532,8 @@ function generateListGroupUSSD(panel) { } request('listgroupussd', 'json', { to: panel.data("to") }).then(data => { - if (isJSON(data) && JSON.parse(data).error) - noticer.error(JSON.parse(data).message); + if (data.error) + noticer.error(data.message); else { ussd = data; showListUSSD(panel); @@ -618,7 +635,10 @@ function delUSSD(panel, currentWindow) { function loadServerInfo() { request('serverinfo', 'text').then(data => { - data.error ? noticer.error(data.message) : showServerInfo(data); + if (isJSON(data) && JSON.parse(data).error) + noticer.error(JSON.parse(data).message); + else + showServerInfo(data); }).catch(error => { noticer.error(error.message); }); diff --git a/public/authorization.css b/public/authorization.css new file mode 100644 index 0000000..2b1cf94 --- /dev/null +++ b/public/authorization.css @@ -0,0 +1,64 @@ +@font-face { + font-family: Scada; + src: url(Scada-Regular.ttf); +} + +body { + display: flex; + align-items: center; + justify-content: center; + height: 100vh; + overflow: hidden; + margin-top: -5%; + color: #333; + opacity: 0; + font-family: Scada; +} + +div.form { + display: flex; + flex-direction: column; + align-items: center; +} + +.div-button { + display: flex; + justify-content: center; + margin-top: 10px; +} + +input { + text-align: center; + color: #333; + border: 1px solid#c5c5c5; + height: 30px; +} + +input:hover { + border: 1px solid #999; + box-shadow: 1px 1px 10px 1px #ccc; +} + +.input-focus:focus { + outline: none; + box-shadow: 1px 1px 10px 1px #666; + border: 1px solid #555; +} + +.logo { + background-image: url("favicon.png"); + min-width: 128px; + min-height: 128px; + background-size: contain; + margin-bottom: 20px; +} + +.title { + margin: 30px; + color:#333333 +} + +.label { + text-align: right; + padding-right: 10px; +} diff --git a/source/daster.d b/source/daster.d index 01789f6..53a37bb 100644 --- a/source/daster.d +++ b/source/daster.d @@ -15,13 +15,16 @@ import std.array; import verinfo; import pgdb; import structures; +import response; import requests.numbers; import requests.sms; import requests.ussd; import requests.server; +import requests.authorization; static ServerInfo serverInfo; +static AuthData serverAuthData; private void showVersion() { writefln("daster версия %s, собрано %s", getDasterVersion(), __DATE__); @@ -88,6 +91,7 @@ int main(string[] args) { if (webHost.logfile.length) log.file(webHost.logfile); rcAsteriskDB(); + rcAuth(); auto router = new URLRouter; router.post("/", &postReq); @@ -144,16 +148,15 @@ void startWebServer(WebHost wh, HTTPServerSettings http, HTTPServerSettings http } void getReq(HTTPServerRequest req, HTTPServerResponse res) { - // if (req.session) { - // auto user = req.session.get!UserData("userData"); - // if (user.loggedIn) { - // renderMainPage(req, res); - // return; - // } - // } + if (req.session) { + auto user = req.session.get!UserData("user"); + if (user.login) { + renderMainPage(req, res); + return; + } + } - // render!("index.dt", serverInfo)(res); - renderMainPage(req, res); + render!("authorization.dt", serverInfo)(res); } void renderMainPage(HTTPServerRequest req, HTTPServerResponse res) { @@ -168,23 +171,23 @@ void postReq(HTTPServerRequest req, HTTPServerResponse res) { if (query.empty) return; - // if (query != "authorization" && !checkAuth(req)) { - // res.send( - // true, - // "Сессия не существует. Перезагрузите страницу" - // ); - // return; - // } + if (query != "login" && !checkAuth(req)) { + res.send( + true, + "Сессия не существует. Перезагрузите страницу" + ); + return; + } log.d("json request: " ~ jsr.to!string); switch (query) { - // case "authorization": - // authorization(req, res); - // break; - // case "logout": - // logout(req, res); - // break; + case "login": + login(req, res, serverAuthData); + break; + case "logout": + logout(req, res); + break; case "listnumbergroups": getListNumberGroups(req, res); break; @@ -441,3 +444,24 @@ WebHost rcWebHost() { return wh; } + +void rcAuth() { + ConfigSection auth; + + try { + auth = rc[]["auth"]; + } catch (Exception e) { + log.c("В конфигурационном файле не верны настройки auth"); + exit(1); + } + + if (auth["login"].empty) + log.w("Логин не был установлен - auth.login"); + else + serverAuthData.login = auth["login"]; + + if (auth["password"].empty) + log.w("Пароль не был установлен - auth.password"); + else + serverAuthData.password = auth["password"]; +} diff --git a/source/requests/authorization.d b/source/requests/authorization.d new file mode 100644 index 0000000..e046886 --- /dev/null +++ b/source/requests/authorization.d @@ -0,0 +1,47 @@ +module requests.authorization; + +import vibe.vibe; +import response; +import structures; +import singlog; + +void login(HTTPServerRequest req, HTTPServerResponse res, AuthData serverAuthData) { + auto userAuthData = deserializeJson!AuthData(req.json); + + if (!(serverAuthData.login == userAuthData.login && + serverAuthData.password == userAuthData.password)) { + log.i(("Данные авторизации не верны: %s").format(req.json)); + res.send( + true, + "Данные авторизации не верны" + ); + return; + } + + auto user = UserData(true); + + req.session = res.startSession(); + req.session.set!UserData("user", user); + + log.i("Авторизация успешно пройдена"); + + res.send(); +} + +void logout(HTTPServerRequest req, HTTPServerResponse res) { + req.session.set!UserData("user", UserData.init); + res.terminateSession(); + + log.i("Выход из системы"); + + res.send(); +} + +bool checkAuth(HTTPServerRequest req) { + if (req.session) + return req.session.get!UserData("user").login; + + log.d("Отсутствует авторизация"); + + return false; +} diff --git a/source/structures.d b/source/structures.d index 04c2a95..b38058d 100644 --- a/source/structures.d +++ b/source/structures.d @@ -4,6 +4,15 @@ struct ServerInfo { string name; } +struct AuthData { + string login; + string password; +} + +struct UserData { + bool login = false; +} + struct WebHost { string[] addresses; ushort http = 0; diff --git a/source/version_.d b/source/version_.d index 0492867..e8028ea 100644 --- a/source/version_.d +++ b/source/version_.d @@ -1,3 +1,3 @@ module version_; -enum dasterVersion = "v0.0.9"; +enum dasterVersion = "v0.0.10"; diff --git a/views/authorization.dt b/views/authorization.dt new file mode 100644 index 0000000..6eb8540 --- /dev/null +++ b/views/authorization.dt @@ -0,0 +1,26 @@ +doctype html +head + title #{serverInfo.name}: авторизация + link(rel='icon', type='image/png', sizes='128x128', href='favicon.png') + link(rel='stylesheet', type='text/css', href='jquery-ui.min.css') + link(rel='stylesheet', type='text/css', href='authorization.css') + script(src='jquery-3.7.0.min.js') + script(src='jquery-ui.min.js') + script(src='noticer.min.js') + script(src='authorization.js') +body + div.form + div.logo + div.title #{serverInfo.name} + table + tbody + tr + td.label Логин: + td + input.input-focus#login(type='text') + tr + td.label Пароль: + td + input.input-focus#password(type='password') + div.div-button + button#authorization Войти