mirror of
https://github.com/janishutz/libreevent.git
synced 2025-11-25 21:34:24 +00:00
fix bugs, full two fa system done
This commit is contained in:
@@ -14,6 +14,7 @@ const token = require( '../token.js' );
|
||||
class TwoFA {
|
||||
constructor () {
|
||||
this.tokenStore = {};
|
||||
this.references = {};
|
||||
}
|
||||
|
||||
registerStandardAuthentication () {
|
||||
@@ -35,6 +36,14 @@ class TwoFA {
|
||||
return { 'code': code, 'token': tok };
|
||||
}
|
||||
|
||||
storeTokenReference ( token, sessionID ) {
|
||||
this.references[ token ] = sessionID;
|
||||
}
|
||||
|
||||
getTokenReference ( token ) {
|
||||
return this.references[ token ];
|
||||
}
|
||||
|
||||
verifyEnhanced ( token, number = '' ) {
|
||||
if ( this.tokenStore[ token ]?.mode === 'standard' ) return true;
|
||||
else if ( this.tokenStore[ token ]?.mode === 'enhanced' ) {
|
||||
|
||||
@@ -19,7 +19,9 @@ const db = require( '../db/db.js' );
|
||||
module.exports.checkpassword = function checkpassword ( username, password ) {
|
||||
return new Promise( resolve => {
|
||||
db.getData( 'user', username ).then( data => {
|
||||
resolve( bcrypt.compareSync( password, data ) );
|
||||
bcrypt.compare( password, data ).then( data => {
|
||||
resolve( data );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
@@ -13,6 +13,8 @@ const auth = require( './credentials/2fa.js' );
|
||||
const twoFA = new auth();
|
||||
const path = require( 'path' );
|
||||
|
||||
let responseObjects = {};
|
||||
|
||||
module.exports = ( app, settings ) => {
|
||||
app.post( '/api/reserveTicket', ( request, response ) => {
|
||||
db.getData( 'test', request.body );
|
||||
@@ -23,11 +25,16 @@ module.exports = ( app, settings ) => {
|
||||
if ( request.body.mail && request.body.password ) {
|
||||
pwdmanager.checkpassword( request.body.mail, request.body.password ).then( data => {
|
||||
if ( data ) {
|
||||
// TODO: Send mails
|
||||
if ( settings.twoFA === 'standard' ) {
|
||||
let tok = twoFA.registerStandardAuthentication()[ 'token' ];
|
||||
request.session.token = tok;
|
||||
console.log( 'http://localhost:8081/user/2fa?token=' + tok );
|
||||
response.send( { 'status': '2fa' } );
|
||||
} else if ( settings.twoFA === 'enhanced' ) {
|
||||
let res = twoFA.registerEnhancedAuthentication();
|
||||
console.log( 'http://localhost:8081/user/2fa?token=' + res.token );
|
||||
request.session.token = res.token;
|
||||
response.send( { 'status': '2fa+', 'code': res.code } );
|
||||
} else {
|
||||
request.session.loggedInUser = true;
|
||||
@@ -47,6 +54,7 @@ module.exports = ( app, settings ) => {
|
||||
let tokType = twoFA.verifySimple( request.query.token );
|
||||
if ( tokType === 'standard' ) {
|
||||
request.session.loggedInUser = true;
|
||||
responseObjects[ request.query.token ].write( 'data: authenticated\n\n' );
|
||||
response.sendFile( path.join( __dirname + '/../ui/en/2fa/2faSimple.html' ) );
|
||||
} else if ( tokType === 'enhanced' ) {
|
||||
response.sendFile( path.join( __dirname + '/../ui/en/2fa/2faEnhanced.html' ) );
|
||||
@@ -59,7 +67,20 @@ module.exports = ( app, settings ) => {
|
||||
let verified = twoFA.verifyEnhanced( request.body.token, request.body.code );
|
||||
if ( verified ) {
|
||||
request.session.loggedInUser = true;
|
||||
responseObjects[ request.query.token ].write( 'data: authenticated\n\n' );
|
||||
response.send( 'ok' );
|
||||
} else response.send( 'wrong' );
|
||||
} );
|
||||
|
||||
app.get( '/user/2fa/check', ( request, response ) => {
|
||||
response.writeHead( 200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
} );
|
||||
response.status( 200 );
|
||||
response.flushHeaders();
|
||||
response.write( 'data: connected\n\n' );
|
||||
responseObjects[ request.session.token ] = response;
|
||||
} );
|
||||
};
|
||||
Reference in New Issue
Block a user