From bb3e4fa27c8e6b5aa3f901ce834834c53c77e70c Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Thu, 10 Mar 2022 16:37:02 +0100 Subject: [PATCH] Login and Token System implemented --- data/admin | Bin 11 -> 12 bytes data/s/bundle.1727d.js | 2 ++ data/s/bundle.f9ad8.js | 2 -- data/s/index.html | 2 +- src/AdminAuth.cpp | 59 +++++++++++++++++++++++++++++++ src/AdminAuth.h | 77 +++++++++++++++++++++++++++++++++++++++++ src/UserDb.h | 7 ++-- src/WebConsole.cpp | 66 +++++++++++++++++++++++++++++++++-- src/WebConsole.h | 4 +++ 9 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 data/s/bundle.1727d.js delete mode 100644 data/s/bundle.f9ad8.js create mode 100644 src/AdminAuth.cpp create mode 100644 src/AdminAuth.h diff --git a/data/admin b/data/admin index d083dff760c6ef67628ab8fd1df3727e1f1f09d6..002dce9ae6e7644c5aa16d2f9e06d7d7304d1aca 100644 GIT binary patch literal 12 PcmYdH$<54TNCcAr983fg literal 11 PcmYdH$<55GNd%Dq8zBUb diff --git a/data/s/bundle.1727d.js b/data/s/bundle.1727d.js new file mode 100644 index 0000000..012e897 --- /dev/null +++ b/data/s/bundle.1727d.js @@ -0,0 +1,2 @@ +!function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var o in e)t.d(r,o,function(t){return e[t]}.bind(null,o));return r},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="/",t(t.s="/ODT")}({"/ODT":function(e,t,n){"use strict";function r(e,t){for(var n in t)e[n]=t[n];return e}function o(e){var t=e.parentNode;t&&t.removeChild(e)}function i(e,t,n){var r,o,i,l={};for(i in t)"key"==i?r=t[i]:"ref"==i?o=t[i]:l[i]=t[i];if(arguments.length>2&&(l.children=arguments.length>3?P.call(arguments,2):n),"function"==typeof e&&null!=e.defaultProps)for(i in e.defaultProps)void 0===l[i]&&(l[i]=e.defaultProps[i]);return u(e,l,r,o,null)}function u(e,t,n,r,o){var i={type:e,props:t,key:n,ref:r,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==o?++C:o};return null==o&&null!=x.vnode&&x.vnode(i),i}function l(e){return e.children}function c(e,t){this.props=e,this.context=t}function a(e,t){if(null==t)return e.__?a(e.__,e.__.__k.indexOf(e)+1):null;for(var n;t0?u(v.type,v.props,v.key,null,v.__v):v)){if(v.__=n,v.__b=n.__b+1,null===(y=S[p])||y&&v.key==y.key&&v.type===y.type)S[p]=void 0;else for(b=0;b=n.__.length&&n.__.push({}),n.__[e]}function o(e){return y=1,i(f,e)}function i(e,t,n){var o=r(p++,2);return o.t=e,o.__c||(o.__=[n?n(t):f(void 0,t),function(e){var t=o.t(o.__[0],e);o.__[0]!==t&&(o.__=[t,o.__[1]],o.__c.setState({}))}],o.__c=h),o.__}function u(e,t){var n=r(p++,3);!b.options.__s&&_(n.__H,t)&&(n.__=e,n.__H=t,h.__H.__h.push(n))}function l(e){var t=h.context[e.__c],n=r(p++,9);return n.c=e,t?(null==n.__&&(n.__=!0,t.sub(h)),t.props.value):e.__}function c(){for(var e;e=v.shift();)if(e.__P)try{e.__H.__h.forEach(a),e.__H.__h.forEach(s),e.__H.__h=[]}catch(t){e.__H.__h=[],b.options.__e(t,e.__v)}}function a(e){var t=h,n=e.__c;"function"==typeof n&&(e.__c=void 0,n()),h=t}function s(e){var t=h;e.__c=e.__(),h=t}function _(e,t){return!e||e.length!==t.length||t.some((function(t,n){return t!==e[n]}))}function f(e,t){return"function"==typeof t?t(e):t}n.d(t,"d",(function(){return o})),n.d(t,"c",(function(){return i})),n.d(t,"b",(function(){return u})),n.d(t,"a",(function(){return l}));var p,h,d,b=n("hosL"),y=0,v=[],m=b.options.__b,g=b.options.__r,O=b.options.diffed,j=b.options.__c,k=b.options.unmount;b.options.__b=function(e){h=null,m&&m(e)},b.options.__r=function(e){g&&g(e),p=0;var t=(h=e.__c).__H;t&&(t.__h.forEach(a),t.__h.forEach(s),t.__h=[])},b.options.diffed=function(e){O&&O(e);var t=e.__c;t&&t.__H&&t.__H.__h.length&&(1!==v.push(t)&&d===b.options.requestAnimationFrame||((d=b.options.requestAnimationFrame)||function(e){var t,n=function(){clearTimeout(r),w&&cancelAnimationFrame(t),setTimeout(e)},r=setTimeout(n,100);w&&(t=requestAnimationFrame(n))})(c)),h=null},b.options.__c=function(e,t){t.some((function(e){try{e.__h.forEach(a),e.__h=e.__h.filter((function(e){return!e.__||s(e)}))}catch(n){t.some((function(e){e.__h&&(e.__h=[])})),t=[],b.options.__e(n,e.__v)}})),j&&j(e,t)},b.options.unmount=function(e){k&&k(e);var t,n=e.__c;n&&n.__H&&(n.__H.__.forEach((function(e){try{a(e)}catch(e){t=e}})),t&&b.options.__e(t,n.__v))};var w="function"==typeof requestAnimationFrame},QfWi:function(e,t,n){"use strict";function r(e,t){for(var n in t)e[n]=t[n];return e}function o(e,t,n){var r,o=/(?:\?([^#]*))?(#.*)?$/,i=e.match(o),u={};if(i&&i[1])for(var c=i[1].split("&"),a=0;at.rank?-1:e.index-t.index}function u(e,t){return e.index=t,e.rank=function(e){return e.props.default?0:(t=e.props.path,l(t).map(c).join(""));var t}(e),e.props}function l(e){return e.replace(/(^\/+|\/+$)/g,"").split("/")}function c(e){return":"==e.charAt(0)?1+"*+?".indexOf(e.charAt(e.length-1))||4:5}function a(){var e;return""+((e=ne&&ne.location?ne.location:ne&&ne.getCurrentLocation?ne.getCurrentLocation():"undefined"!=typeof location?location:ie).pathname||"")+(e.search||"")}function s(e,t){return void 0===t&&(t=!1),"string"!=typeof e&&e.url&&(t=e.replace,e=e.url),function(e){for(var t=re.length;t--;)if(re[t].canRoute(e))return!0;return!1}(e)&&function(e,t){void 0===t&&(t="push"),ne&&ne[t]?ne[t](e):"undefined"!=typeof history&&history[t+"State"]&&history[t+"State"](null,null,e)}(e,t?"replace":"push"),_(e)}function _(e){for(var t=!1,n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n0&&void 0!==arguments[0]?arguments[0]:"azif7eqCl5",t=yield fetch("".concat(_e,"/api/userdb"),{method:"GET",mode:"cors",headers:{Authentification:e}}).then((function(e){return e.text()})).then((function(e){return w(e)}));return t})),k.apply(this,arguments)}function w(e){var t=e.split("\n"),n=[];return t.map((function(e,t){var r=v(e.split([";"]),6);n.push({line:t,uid:r[0],first_name:r[1],last_name:r[2],rfid_uid:r[3],user_pin:r[4],enabled:"1"===r[5][0]})})),n}function S(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function A(e){for(var t=1;te.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n0},t.prototype.routeTo=function(e){this.setState({url:e});var t=this.canRoute(e);return this.updating||this.forceUpdate(),t},t.prototype.componentWillMount=function(){re.push(this),this.updating=!0},t.prototype.componentDidMount=function(){var e=this;ne&&(this.unlisten=ne.listen((function(t){e.routeTo(""+(t.pathname||"")+(t.search||""))}))),this.updating=!1},t.prototype.componentWillUnmount=function(){"function"==typeof this.unlisten&&this.unlisten(),re.splice(re.indexOf(this),1)},t.prototype.componentWillUpdate=function(){this.updating=!0},t.prototype.componentDidUpdate=function(){this.updating=!1},t.prototype.getMatchingChildren=function(e,t,n){return e.filter(u).sort(i).map((function(e){var i=o(t,e.props.path,e.props);if(i){if(!1!==n){var u={url:t,matches:i};return r(u,i),delete u.ref,delete u.key,Object(ee.cloneElement)(e,u)}return e}})).filter(Boolean)},t.prototype.render=function(e,t){var n=e.children,r=e.onChange,o=t.url,i=this.getMatchingChildren(Object(ee.toChildArray)(n),o,!0),u=i[0]||null,l=this.previousUrl;return o!==l&&(this.previousUrl=o,"function"==typeof r&&r({router:this,url:o,previous:l,active:i,current:u})),u},t}(ee.Component),ce=function(e){return Object(ee.createElement)("a",r({onClick:p},e))};le.subscribers=oe,le.getCurrentUrl=a,le.route=s,le.Router=le,le.Route=function(e){return Object(ee.createElement)(e.component,e)},le.Link=ce,le.exec=o;var ae=n("QRet"),se=function(){return Object(ee.h)("div",{class:"container"},Object(ee.h)("h1",null,"Home"),Object(ee.h)("p",null,"This is the Home component."))},_e="http://192.168.4.22",fe={login:function(){return j.apply(this,arguments)},logout:function(){return"DUMMYTOKEN"},checkAuth:function(e){return"DUMMYTOKEN"===e},parsedb:w,fetchdb:function(){return k.apply(this,arguments)}},pe=function(e){for(var t=1;t2&&(l.children=arguments.length>3?U.call(arguments,2):n),"function"==typeof e&&null!=e.defaultProps)for(i in e.defaultProps)void 0===l[i]&&(l[i]=e.defaultProps[i]);return u(e,l,r,o,null)}function u(e,t,n,r,o){var i={type:e,props:t,key:n,ref:r,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==o?++T:o};return null==o&&null!=I.vnode&&I.vnode(i),i}function l(){return{current:null}}function c(e){return e.children}function a(e,t){this.props=e,this.context=t}function s(e,t){if(null==t)return e.__?s(e.__,e.__.__k.indexOf(e)+1):null;for(var n;t0?u(v.type,v.props,v.key,null,v.__v):v)){if(v.__=n,v.__b=n.__b+1,null===(b=k[p])||b&&v.key==b.key&&v.type===b.type)k[p]=void 0;else for(h=0;h2&&(c.children=arguments.length>3?U.call(arguments,2):n),u(e.type,c,o||e.key,i||e.ref,null)}function D(e,t){var n={__c:t="__cC"+H++,__:e,Consumer:function(e,t){return e.children(t)},Provider:function(e){var n,r;return this.getChildContext||(n=[],(r={})[t]=this,this.getChildContext=function(){return r},this.shouldComponentUpdate=function(e){this.props.value!==e.value&&n.some(f)},this.sub=function(e){n.push(e);var t=e.componentWillUnmount;e.componentWillUnmount=function(){n.splice(n.indexOf(e),1),t&&t.call(e)}}),e.children}};return n.Provider.__=n.Consumer.contextType=n}n.r(t),n.d(t,"render",(function(){return x})),n.d(t,"hydrate",(function(){return C})),n.d(t,"createElement",(function(){return i})),n.d(t,"h",(function(){return i})),n.d(t,"Fragment",(function(){return c})),n.d(t,"createRef",(function(){return l})),n.d(t,"isValidElement",(function(){return N})),n.d(t,"Component",(function(){return a})),n.d(t,"cloneElement",(function(){return E})),n.d(t,"createContext",(function(){return D})),n.d(t,"toChildArray",(function(){return b})),n.d(t,"options",(function(){return I}));var U,I,T,N,M,L,R,H,W={},F=[],$=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;U=F.slice,I={__e:function(e,t){for(var n,r,o;t=t.__;)if((n=t.__c)&&!n.__)try{if((r=n.constructor)&&null!=r.getDerivedStateFromError&&(n.setState(r.getDerivedStateFromError(e)),o=n.__d),null!=n.componentDidCatch&&(n.componentDidCatch(e),o=n.__d),o)return n.__E=n}catch(t){e=t}throw e}},T=0,N=function(e){return null!=e&&void 0===e.constructor},a.prototype.setState=function(e,t){var n;n=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=r({},this.state),"function"==typeof e&&(e=e(r({},n),this.props)),e&&r(n,e),null!=e&&this.__v&&(t&&this.__h.push(t),f(this))},a.prototype.forceUpdate=function(e){this.__v&&(this.__e=!0,e&&this.__h.push(e),f(this))},a.prototype.render=c,M=[],L="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,p.__r=0,H=0},z8Av:function(e,t,n){"use strict";(function(e){function r(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,o,i=[],u=!0,l=!1;try{for(n=n.call(e);!(u=(r=n.next()).done)&&(i.push(r.value),!t||i.length!==t);u=!0);}catch(e){l=!0,o=e}finally{try{u||null==n.return||n.return()}finally{if(l)throw o}}return i}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return o(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return o(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n2&&(l.children=arguments.length>3?x.call(arguments,2):n),"function"==typeof t&&null!=t.defaultProps)for(u in t.defaultProps)void 0===l[u]&&(l[u]=t.defaultProps[u]);return i(t,l,r,o,null)}function i(t,e,n,r,o){var u={type:t,props:e,key:n,ref:r,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==o?++P:o};return null==o&&null!=C.vnode&&C.vnode(u),u}function l(t){return t.children}function c(t,e){this.props=t,this.context=e}function a(t,e){if(null==e)return t.__?a(t.__,t.__.__k.indexOf(t)+1):null;for(var n;e0?i(b.type,b.props,b.key,null,b.__v):b)){if(b.__=n,b.__b=n.__b+1,null===(v=S[p])||v&&b.key==v.key&&b.type===v.type)S[p]=void 0;else for(y=0;y=n.__.length&&n.__.push({}),n.__[t]}function o(t){return v=1,u(f,t)}function u(t,e,n){var o=r(p++,2);return o.t=t,o.__c||(o.__=[n?n(e):f(void 0,e),function(t){var e=o.t(o.__[0],t);o.__[0]!==e&&(o.__=[e,o.__[1]],o.__c.setState({}))}],o.__c=h),o.__}function i(t,e){var n=r(p++,3);!y.options.__s&&s(n.__H,e)&&(n.__=t,n.__H=e,h.__H.__h.push(n))}function l(t){var e=h.context[t.__c],n=r(p++,9);return n.c=t,e?(null==n.__&&(n.__=!0,e.sub(h)),e.props.value):t.__}function c(){for(var t;t=b.shift();)if(t.__P)try{t.__H.__h.forEach(a),t.__H.__h.forEach(_),t.__H.__h=[]}catch(e){t.__H.__h=[],y.options.__e(e,t.__v)}}function a(t){var e=h,n=t.__c;"function"==typeof n&&(t.__c=void 0,n()),h=e}function _(t){var e=h;t.__c=t.__(),h=e}function s(t,e){return!t||t.length!==e.length||e.some((function(e,n){return e!==t[n]}))}function f(t,e){return"function"==typeof e?e(t):e}n.d(e,"d",(function(){return o})),n.d(e,"c",(function(){return u})),n.d(e,"b",(function(){return i})),n.d(e,"a",(function(){return l}));var p,h,d,y=n("hosL"),v=0,b=[],m=y.options.__b,g=y.options.__r,O=y.options.diffed,j=y.options.__c,k=y.options.unmount;y.options.__b=function(t){h=null,m&&m(t)},y.options.__r=function(t){g&&g(t),p=0;var e=(h=t.__c).__H;e&&(e.__h.forEach(a),e.__h.forEach(_),e.__h=[])},y.options.diffed=function(t){O&&O(t);var e=t.__c;e&&e.__H&&e.__H.__h.length&&(1!==b.push(e)&&d===y.options.requestAnimationFrame||((d=y.options.requestAnimationFrame)||function(t){var e,n=function(){clearTimeout(r),w&&cancelAnimationFrame(e),setTimeout(t)},r=setTimeout(n,100);w&&(e=requestAnimationFrame(n))})(c)),h=null},y.options.__c=function(t,e){e.some((function(t){try{t.__h.forEach(a),t.__h=t.__h.filter((function(t){return!t.__||_(t)}))}catch(n){e.some((function(t){t.__h&&(t.__h=[])})),e=[],y.options.__e(n,t.__v)}})),j&&j(t,e)},y.options.unmount=function(t){k&&k(t);var e,n=t.__c;n&&n.__H&&(n.__H.__.forEach((function(t){try{a(t)}catch(t){e=t}})),e&&y.options.__e(e,n.__v))};var w="function"==typeof requestAnimationFrame},QfWi:function(t,e,n){"use strict";function r(t,e){for(var n in e)t[n]=e[n];return t}function o(t,e,n){var r,o=/(?:\?([^#]*))?(#.*)?$/,u=t.match(o),i={};if(u&&u[1])for(var c=u[1].split("&"),a=0;ae.rank?-1:t.index-e.index}function i(t,e){return t.index=e,t.rank=function(t){return t.props.default?0:(e=t.props.path,l(e).map(c).join(""));var e}(t),t.props}function l(t){return t.replace(/(^\/+|\/+$)/g,"").split("/")}function c(t){return":"==t.charAt(0)?1+"*+?".indexOf(t.charAt(t.length-1))||4:5}function a(){var t;return""+((t=E&&E.location?E.location:E&&E.getCurrentLocation?E.getCurrentLocation():"undefined"!=typeof location?location:T).pathname||"")+(t.search||"")}function _(t,e){return void 0===e&&(e=!1),"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),function(t){for(var e=U.length;e--;)if(U[e].canRoute(t))return!0;return!1}(t)&&function(t,e){void 0===e&&(e="push"),E&&E[e]?E[e](t):"undefined"!=typeof history&&history[e+"State"]&&history[e+"State"](null,null,t)}(t,e?"replace":"push"),s(t)}function s(t){for(var e=!1,n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);nt.length)&&(e=t.length);for(var n=0,r=new Array(e);nt.length)&&(e=t.length);for(var n=0,r=new Array(e);nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n0},e.prototype.routeTo=function(t){this.setState({url:t});var e=this.canRoute(t);return this.updating||this.forceUpdate(),e},e.prototype.componentWillMount=function(){U.push(this),this.updating=!0},e.prototype.componentDidMount=function(){var t=this;E&&(this.unlisten=E.listen((function(e){t.routeTo(""+(e.pathname||"")+(e.search||""))}))),this.updating=!1},e.prototype.componentWillUnmount=function(){"function"==typeof this.unlisten&&this.unlisten(),U.splice(U.indexOf(this),1)},e.prototype.componentWillUpdate=function(){this.updating=!0},e.prototype.componentDidUpdate=function(){this.updating=!1},e.prototype.getMatchingChildren=function(t,e,n){return t.filter(i).sort(u).map((function(t){var u=o(e,t.props.path,t.props);if(u){if(!1!==n){var i={url:e,matches:u};return r(i,u),delete i.ref,delete i.key,Object(C.cloneElement)(t,i)}return t}})).filter(Boolean)},e.prototype.render=function(t,e){var n=t.children,r=t.onChange,o=e.url,u=this.getMatchingChildren(Object(C.toChildArray)(n),o,!0),i=u[0]||null,l=this.previousUrl;return o!==l&&(this.previousUrl=o,"function"==typeof r&&r({router:this,url:o,previous:l,active:u,current:i})),i},e}(C.Component),R=function(t){return Object(C.createElement)("a",r({onClick:p},t))};I.subscribers=D,I.getCurrentUrl=a,I.route=_,I.Router=I,I.Route=function(t){return Object(C.createElement)(t.component,t)},I.Link=R,I.exec=o;var N=function(t,e){switch(e){case"show":return!0;case"hide":return!1;case"toggle":return!t;default:throw new Error("menustate unknown to reducer")}},M=function(t,e){switch(e.type){case"start":return{active:!0,token:e.token,username:e.username};case"end":return{active:!1,token:null,username:null,exiry:null};default:throw new Error("action type unknown to session reducer")}},H=n("QRet"),W=function(){return Object(C.h)("div",{class:"container"},Object(C.h)("h1",null,"Home"),Object(C.h)("p",null,"This is the Home component."))},F=n("L7jf"),B=function(t){var e=t.user,n=y(Object(H.d)(Date.now()),2),r=n[0],o=n[1],u=y(Object(H.d)(10),2),i=u[0],l=u[1];return Object(H.b)((function(){var t=setInterval((function(){return o(Date.now())}),1e3);return function(){return clearInterval(t)}}),[]),Object(C.h)("div",{className:"container"},Object(C.h)("div",null,Object(C.h)("h1",null,"Profile: ",e),Object(C.h)("p",null,"This is the user profile for a user named ",e,"."),Object(C.h)("div",null,"Current time: ",new Date(r).toLocaleString()),Object(C.h)("p",null,Object(C.h)("button",{onClick:function(){return l((function(t){return t+1}))}},"Click Me")," ","Clicked ",i," times.")))},$=n("YRIq"),q=function(t){if(t.items)return Object(C.h)("div",{class:"breadcrumb"},Object(C.h)("ul",null,t.items.map((function(t,e){return Object(C.h)("li",{key:e},Object(C.h)("a",{href:"#"},t)," ")}))))},z=function(){var t=O(Object(H.a)($.b).session,2),e=t[0],n=t[1],r=O(Object(H.d)({username:"",password:"",error:null}),2),o=r[0],u=r[1];return e.active&&_("/",!0),Object(C.h)("div",{class:"container"},Object(C.h)(q,{items:["Login"]}),Object(C.h)("form",{id:"login_form",onSubmit:function(t){if(t.preventDefault(),"admin"===o.username){n({type:"start",username:"affe",token:"dsf4w3qr"})}else u(m(m({},o),{},{error:"user"}));u({username:"",password:""})}},Object(C.h)("p",null,"Bitte melden Sie sich mit ihren Nutzerdaten an."),null!==o.error&&Object(C.h)("span",{style:"color: red"},"Fehler: Ungültige Anmeldedaten."),Object(C.h)("div",{class:"input-box"},Object(C.h)("input",{id:"name",type:"text",placeholder:"Username",onInput:function(t){return u(m(m({},o),{},{username:t.target.value}))},value:o.username}),Object(C.h)("label",{for:"name"},"Benutzername")),Object(C.h)("div",{class:"input-box"},Object(C.h)("input",{id:"pass",type:"password",placeholder:"Passwort",onInput:function(t){return u(m(m({},o),{},{password:t.target.value}))},value:o.password}),Object(C.h)("label",{for:"pass"},"Password")),Object(C.h)("input",{type:"submit",value:"Submit"})))},K=function(){var t=k(Object(H.a)($.b).session,2),e=t[0],n=t[1];return this.shouldComponentUpdate=function(){console.log("functional component vs closures")},e.active&&n({type:"end"}),Object(C.h)("div",{class:"container"},Object(C.h)(q,{items:["Logout"]}),Object(C.h)("p",null,"Erfolgreich abgemeldet.",Object(C.h)(R,{href:"/login"},"Erneut Anmelden")))},Q=function(){var t=Object(H.c)(N,!1),e=Object(H.c)(M,{active:!0});return Object(H.b)((function(){console.log("Use Session")})),this.menu_items=[{text:"Übersicht",path:"/"},{text:"Benutzer",path:"/users"},{text:"System",path:"/system"},{text:"Profil",path:"/profile"},{text:"Abmelden",path:"/logout"}],this.handleRoute=function(){var t,n=(t=function*(){e[0].active||_("/login",!0)},function(){var e=this,n=arguments;return new Promise((function(r,o){function u(t){S(l,r,o,u,i,"next",t)}function i(t){S(l,r,o,u,i,"throw",t)}var l=t.apply(e,n);u(void 0)}))});return function(t){return n.apply(this,arguments)}}(),Object(C.h)($.a,{value:{menu:t,session:e}},Object(C.h)("div",{id:"wrapper"},Object(C.h)(V.a,null),Object(C.h)("div",{class:"page"},Object(C.h)(G,{items:this.menu_items}),!t[0]&&Object(C.h)(I,{onChange:this.handleRoute},Object(C.h)(W,{path:"/",user:"me"}),Object(C.h)(z,{path:"login"}),Object(C.h)(B,{path:"/profile"}),Object(C.h)(K,{path:"/logout"}),Object(C.h)(F.a,{path:"/users"}),Object(C.h)("div",{class:"container",default:!0},"Error 404"))),Object(C.h)("footer",null,Object(C.h)("div",{className:"container",style:"text-align: center; align-items: center"},Object(C.h)("span",null,"© Jean Jacques Avril 2022 ")))))},V=(n("ab6c"),n("z8Av")),G=(n("hNgA"),function(t){var e=t.items,n=A(Object(H.a)($.b).menu,2),r=n[1],o=[{text:"Übersicht",path:"/"},{text:"Benutzer",path:"/users"},{text:"System",path:"/system"},{text:"Backup",path:"/backup"},{text:"Abmelden",path:"/logout"}],u=function(t){t.preventDefault(),r("hide")};if(e&&(o=e),n[0])return Object(C.h)("div",{class:"container"},Object(C.h)("nav",{className:"menu"},Object(C.h)("ul",null,o.map((function(t,e){return Object(C.h)("li",{key:e},Object(C.h)(R,{href:t.path,onClick:u},t.text))})))))}),J=Q;Object(C.render)(Object(C.h)(J,null),document.body)},YRIq:function(t,e,n){"use strict";n.d(e,"a",(function(){return u}));var r=n("hosL"),o=Object(r.createContext)({}),u=o.Provider;e.b=o},ab6c:function(t,e,n){"use strict";function r(){return r=Object.assign||function(t){for(var e=1;e"))),Object(o.h)("div",{class:"page-nav-bar"},Object(o.h)("ul",null,e))}},hNgA:function(t,e,n){"use strict";(function(t,r){function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function u(t,e){for(var n=0;n2&&(l.children=arguments.length>3?D.call(arguments,2):n),"function"==typeof t&&null!=t.defaultProps)for(u in t.defaultProps)void 0===l[u]&&(l[u]=t.defaultProps[u]);return i(t,l,r,o,null)}function i(t,e,n,r,o){var u={type:t,props:e,key:n,ref:r,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==o?++L:o};return null==o&&null!=T.vnode&&T.vnode(u),u}function l(){return{current:null}}function c(t){return t.children}function a(t,e){this.props=t,this.context=e}function _(t,e){if(null==e)return t.__?_(t.__,t.__.__k.indexOf(t)+1):null;for(var n;e0?i(b.type,b.props,b.key,null,b.__v):b)){if(b.__=n,b.__b=n.__b+1,null===(y=k[p])||y&&b.key==y.key&&b.type===y.type)k[p]=void 0;else for(h=0;h2&&(c.children=arguments.length>3?D.call(arguments,2):n),i(t.type,c,o||t.key,u||t.ref,null)}function U(t,e){var n={__c:e="__cC"+H++,__:t,Consumer:function(t,e){return t.children(e)},Provider:function(t){var n,r;return this.getChildContext||(n=[],(r={})[e]=this,this.getChildContext=function(){return r},this.shouldComponentUpdate=function(t){this.props.value!==t.value&&n.some(f)},this.sub=function(t){n.push(t);var e=t.componentWillUnmount;t.componentWillUnmount=function(){n.splice(n.indexOf(t),1),e&&e.call(t)}}),t.children}};return n.Provider.__=n.Consumer.contextType=n}n.r(e),n.d(e,"render",(function(){return C})),n.d(e,"hydrate",(function(){return P})),n.d(e,"createElement",(function(){return u})),n.d(e,"h",(function(){return u})),n.d(e,"Fragment",(function(){return c})),n.d(e,"createRef",(function(){return l})),n.d(e,"isValidElement",(function(){return I})),n.d(e,"Component",(function(){return a})),n.d(e,"cloneElement",(function(){return E})),n.d(e,"createContext",(function(){return U})),n.d(e,"toChildArray",(function(){return y})),n.d(e,"options",(function(){return T}));var D,T,L,I,R,N,M,H,W={},F=[],B=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;D=F.slice,T={__e:function(t,e){for(var n,r,o;e=e.__;)if((n=e.__c)&&!n.__)try{if((r=n.constructor)&&null!=r.getDerivedStateFromError&&(n.setState(r.getDerivedStateFromError(t)),o=n.__d),null!=n.componentDidCatch&&(n.componentDidCatch(t),o=n.__d),o)return n.__E=n}catch(e){t=e}throw t}},L=0,I=function(t){return null!=t&&void 0===t.constructor},a.prototype.setState=function(t,e){var n;n=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=r({},this.state),"function"==typeof t&&(t=t(r({},n),this.props)),t&&r(n,t),null!=t&&this.__v&&(e&&this.__h.push(e),f(this))},a.prototype.forceUpdate=function(t){this.__v&&(this.__e=!0,t&&this.__h.push(t),f(this))},a.prototype.render=c,R=[],N="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,p.__r=0,H=0},z8Av:function(t,e,n){"use strict";(function(t){function r(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null==n)return;var r,o,u=[],i=!0,l=!1;try{for(n=n.call(t);!(i=(r=n.next()).done)&&(u.push(r.value),!e||u.length!==e);i=!0);}catch(t){l=!0,o=t}finally{try{i||null==n.return||n.return()}finally{if(l)throw o}}return u}(t,e)||function(t,e){if(!t)return;if("string"==typeof t)return o(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return o(t,e)}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);ndoorlock_pwa \ No newline at end of file +doorlock_pwa \ No newline at end of file diff --git a/src/AdminAuth.cpp b/src/AdminAuth.cpp new file mode 100644 index 0000000..64ba351 --- /dev/null +++ b/src/AdminAuth.cpp @@ -0,0 +1,59 @@ +#include "AdminAuth.h" +using namespace webconsole; +bool AdminAuth::isAuth(const char *token) +{ + return tokenbuffer->exists(token) != -1; +} +bool AdminAuth::logout(const char *token) +{ + int16_t tid = tokenbuffer->exists(token); + tokenbuffer->setnull(tid); + return tid != -1; +} +char *AdminAuth::login(const char *username, const char *password) +{ + char *res = nullptr; + File adminfile = LittleFS.open("admin", "r"); + bool current_field = false; + uint16_t current_pos = 0; + Serial.print('-'); + while (adminfile.available()) + { + char current = adminfile.read(); + Serial.print(current); + Serial.print('-'); + if (current == 0x00) + { + if (current_field) + { + if (password[current_pos] == 0x00) + res = tokenbuffer->newToken(); + break; + } + else + { + if (username[current_pos] != 0x00) + break; + current_pos = 0; + current_field = true; + } + } + else if (!current_field) + { + Serial.print(String(username[current_pos])); + if (username[current_pos] == 0x00 || username[current_pos] != current) + break; + current_pos++; + } + else + { + Serial.print(String(password[current_pos])); + if (password[current_pos] == 0x00 || password[current_pos] != current) + break; + current_pos++; + } + Serial.print(','); + } + adminfile.close(); + return res; +} \ No newline at end of file diff --git a/src/AdminAuth.h b/src/AdminAuth.h new file mode 100644 index 0000000..e160b5d --- /dev/null +++ b/src/AdminAuth.h @@ -0,0 +1,77 @@ +#pragma once +#include "LittleFS.h" +#define TOKENBUFFERCAPACITY 3 +#define TOKENLENGHT 10 +namespace webconsole +{ + + struct TokenBuffer + { + + TokenBuffer() + { + for (uint16_t j = 0; j < TOKENBUFFERCAPACITY; j++) + setnull(j); + } + char *newToken() + { + if (next >= TOKENBUFFERCAPACITY) + next = 0; + for (uint16_t i = 0; i < TOKENLENGHT; i++) + token[next][i] = randomChar(); + token[next][TOKENLENGHT] = 0x00; + + return &token[next++][0]; + } + int16_t exists(const char *cmp) + { + for (uint16_t j = 0; j < TOKENBUFFERCAPACITY; j++) + { + bool match = true; + for (uint16_t i = 0; match && i < TOKENLENGHT; i++) + if (token[j][i] != cmp[i]) + match = false; + if (match) + return j; + } + return -1; + } + void setnull(uint16_t index) + { + if (index >= 0 && index < TOKENBUFFERCAPACITY) + for (uint16_t i = 0; i < TOKENLENGHT; i++) + token[index][i] = 0x00; // initialize with nullbytes + } + + private: + uint16_t next = 0; + char token[TOKENBUFFERCAPACITY][TOKENLENGHT + 1]; + char randomChar() + { + auto charrype = random(0, 3); + switch (charrype) + { + case 0: + return random(0x30, 0x3A); // Numbers + break; + case 1: + return random(0x41, 0x5B); // Capital Letters + break; + case 2: + return random(0x61, 0x7B); // Small Letters + break; + } + return 0x21; + } + }; + class AdminAuth + { + public: + char *login(const char *username, const char *password); + bool isAuth(const char *token); + bool logout(const char *token); + + private: + TokenBuffer *tokenbuffer = new TokenBuffer(); + }; +} \ No newline at end of file diff --git a/src/UserDb.h b/src/UserDb.h index d6e8a98..77d7b23 100644 --- a/src/UserDb.h +++ b/src/UserDb.h @@ -46,6 +46,8 @@ namespace userdb } }; + + class UserDb { private: @@ -148,7 +150,8 @@ namespace userdb { close_file(); } - void close_file(){ + void close_file() + { db_file.close(); } bool has_next() @@ -164,7 +167,7 @@ namespace userdb if (matchline == line) { current = read_csv_line(current, db_file, line, match, filter_attr); - current.match=true; + current.match = true; } } else diff --git a/src/WebConsole.cpp b/src/WebConsole.cpp index b8eaf3c..0a33f96 100644 --- a/src/WebConsole.cpp +++ b/src/WebConsole.cpp @@ -2,7 +2,9 @@ using namespace webconsole; -WebConsole::WebConsole() {} +WebConsole::WebConsole() +{ +} WebConsole::~WebConsole() { _server->close(); @@ -12,7 +14,11 @@ bool WebConsole::init(userdb::UserDb *userdb) { _server = new ESP8266WebServer(80); this->userdb = userdb; + const char *headerkeys[] = {"Authentification"}; + size_t headerkeyssize = sizeof(headerkeys) / sizeof(char *); + _server->collectHeaders(headerkeys, headerkeyssize); _server->begin(); + _server->on("/api/auth", HTTPMethod::HTTP_POST, std::bind(&WebConsole::_auth, this)); _server->on("/api/userdb", HTTPMethod::HTTP_DELETE, std::bind(&WebConsole::_dropUserDb, this)); _server->on("/api/userdb", HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUserDb, this)); _server->on("/api/rfid", std::bind(&WebConsole::_catchRFID, this)); @@ -45,6 +51,48 @@ bool WebConsole::isInterceptingRfid() { return catch_rfid; } +bool WebConsole::_isAuth() +{ + if (!_server->hasHeader("Authentification")) + { + _server->send(401, "text/plain", "Error 401: Unauthorized (missing auth token)"); + return false; + } + const char *token = _server->header("Authentification").c_str(); + bool res = auth.isAuth(token); + if (!res) + _server->send(401, "text/plain", "Error 401: Unauthorized (missing auth token)"); + return res; +} +void WebConsole::_auth() +{ + _sendCORS(); + String action = _server->arg("action"); + if (action.equals("check")) + { + const char *token = _server->arg("token").c_str(); + bool res = auth.isAuth(token); + _server->send(200, "text/plain", res ? "valid" : "invalid"); + } + else if (action.equals("login")) + { + const char *username = _server->arg("username").c_str(); + const char *password = _server->arg("password").c_str(); + char *token = auth.login(username, password); + if (token == nullptr) + _server->send(401, "text/plain", "failed!"); + else + _server->send(200, "text/plain", token); + } + else if (action.equals("logout")) + { + const char *token = _server->arg("token").c_str(); + bool res = auth.logout(token); + _server->send(200, "text/plain", res ? "success" : "failed"); + } + else + _server->send(404, "text/plain", "unknown action"); +} void WebConsole::_sendCORS() { _server->sendHeader("Access-Control-Allow-Origin", "*"); @@ -54,9 +102,9 @@ void WebConsole::_sendCORS() } void WebConsole::_handleUnknown() { + _sendCORS(); if (_server->method() == HTTP_OPTIONS) { - _sendCORS(); _server->send(204); } else @@ -70,6 +118,8 @@ void WebConsole::_handleUnknown() void WebConsole::_getUserDb() { _sendCORS(); + if (!_isAuth()) + return; File src = LittleFS.open("userdb.csv", "r"); if (src) { @@ -80,6 +130,8 @@ void WebConsole::_getUserDb() void WebConsole::_deleteUser() { _sendCORS(); + if (!_isAuth()) + return; if (userdb == nullptr) { _server->send(500, "text/json", "{\"error\":\"UserDb not initialized\"}"); @@ -101,6 +153,8 @@ void WebConsole::_deleteUser() void WebConsole::_getUser() { _sendCORS(); + if (!_isAuth()) + return; if (userdb == nullptr) { _server->send(500, "text/json", "{\"error\":\"UserDb not initialized\"}"); @@ -120,6 +174,8 @@ void WebConsole::_getUser() void WebConsole::_updateUser() { _sendCORS(); + if (!_isAuth()) + return; userdb::User updated; String body = _server->arg("plain"); const int capacity = 256; @@ -172,6 +228,8 @@ void WebConsole::_updateUser() void WebConsole::_createUser() { _sendCORS(); + if (!_isAuth()) + return; userdb::User created; String body = _server->arg("plain"); const int capacity = 1024; @@ -207,6 +265,8 @@ void WebConsole::_createUser() void WebConsole::_dropUserDb() { _sendCORS(); + if (!_isAuth()) + return; if (userdb->drop()) _server->send(500, "text/json", "{\"ok\":\"UserDb dropped.\"}"); else @@ -215,6 +275,8 @@ void WebConsole::_dropUserDb() void WebConsole::_catchRFID() { _sendCORS(); + if (!_isAuth()) + return; if (rfid == nullptr) { _server->send(500, "text/json", "{\"error\":\"RFID not attached.\"}"); diff --git a/src/WebConsole.h b/src/WebConsole.h index 2371ab5..9ef1d74 100644 --- a/src/WebConsole.h +++ b/src/WebConsole.h @@ -7,6 +7,7 @@ #include "ArduinoJson.h" #include "UserDb.h" #include "Rfid.h" +#include "AdminAuth.h" namespace webconsole { static const char path_prefix[] PROGMEM = "/s"; @@ -24,6 +25,8 @@ namespace webconsole private: void _sendCORS(); + void _auth(); + bool _isAuth(); void _handleUnknown(); void _getUserDb(); void _deleteUser(); @@ -49,6 +52,7 @@ namespace webconsole bool catch_rfid_updated = false; String rfid_buffer; Rfid *rfid = nullptr; + AdminAuth auth; }; } \ No newline at end of file