First commit
This commit is contained in:
309
application/user/signin/user.signin.js
Normal file
309
application/user/signin/user.signin.js
Normal file
@@ -0,0 +1,309 @@
|
||||
|
||||
import user from '/user/user.js';
|
||||
|
||||
import label from '/elements/label.js';
|
||||
|
||||
import username from './signin.username.js';
|
||||
|
||||
import password from './signin.password.js';
|
||||
|
||||
import signinButton from './signin.button.js';
|
||||
|
||||
import userManager from '/server/userManager.js';
|
||||
|
||||
import document from '/unify/document.js';
|
||||
|
||||
import unify from '/unify/unify.js';
|
||||
|
||||
|
||||
import groups from '/user/group/user.group.permission.js';
|
||||
|
||||
import querySQL from '/unify/querySQL.js';
|
||||
|
||||
import header from '/elements/header.js';
|
||||
|
||||
import cancelButton from './signin.cancel.button.js';
|
||||
|
||||
import page from '/elements/page.js';
|
||||
|
||||
import collection from "/unify/collection.js";
|
||||
|
||||
import IS from '/unify/sql/IS.js';
|
||||
|
||||
#ifdef SERVER
|
||||
|
||||
import crypto from "node:crypto";
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class tussenObject{
|
||||
|
||||
password = new password();
|
||||
|
||||
width = "100%"
|
||||
}
|
||||
|
||||
|
||||
|
||||
class buttons{
|
||||
|
||||
#ifdef ANDROID
|
||||
|
||||
width = "100vw";
|
||||
|
||||
position = "absolute"
|
||||
|
||||
bottom = "0";
|
||||
|
||||
#endif
|
||||
|
||||
cancelButton = new cancelButton();
|
||||
|
||||
signinButton = new signinButton();
|
||||
|
||||
}
|
||||
|
||||
class hint{
|
||||
|
||||
text = "Hint: You can sign in with username: admin, password: admin."
|
||||
|
||||
color = "white"
|
||||
|
||||
marginLeft = 22;
|
||||
|
||||
fontWeight = "bold"
|
||||
|
||||
marginTop = 20;
|
||||
|
||||
padding = 20;
|
||||
|
||||
background = "#221f1f4f";
|
||||
|
||||
borderRadius = 4;
|
||||
}
|
||||
|
||||
|
||||
export default class signin extends user, page {
|
||||
|
||||
|
||||
_hint = new hint();
|
||||
|
||||
padding = 10;
|
||||
|
||||
paddingTop = 20;
|
||||
|
||||
color = "black";
|
||||
|
||||
//paddingTop = "0";
|
||||
|
||||
flexDirection = "column";
|
||||
|
||||
height = "400px";
|
||||
|
||||
|
||||
layers = 1;
|
||||
|
||||
debug = true;
|
||||
|
||||
customElement = document.createElement("form");
|
||||
|
||||
useCustomElement = true;
|
||||
|
||||
display = "flex";
|
||||
|
||||
|
||||
|
||||
autocomplete = false;
|
||||
|
||||
_header = new header("Signin");
|
||||
|
||||
//userLabel = new label("Username");
|
||||
username = new username();
|
||||
|
||||
//passwordLabel = new label("Password");
|
||||
tussenObject = new tussenObject();
|
||||
|
||||
|
||||
buttons = new buttons();
|
||||
|
||||
boxMargin;
|
||||
|
||||
|
||||
enableREAD() {
|
||||
|
||||
this.hide();
|
||||
|
||||
}
|
||||
|
||||
|
||||
createVisitor( client ) {
|
||||
|
||||
var table = new user();
|
||||
|
||||
table.username.value = "Visitor";
|
||||
table.id = 0;
|
||||
table.permissionObjects = userManager.getPermissions( table, client );
|
||||
|
||||
return table;
|
||||
|
||||
}
|
||||
|
||||
node async signin( username, password, sessionKey ) {
|
||||
|
||||
var client = this.client;
|
||||
|
||||
client.user = this.createVisitor( client );
|
||||
client.user.error = false;
|
||||
|
||||
if( username ) {
|
||||
|
||||
var users = new collection( user );
|
||||
|
||||
var filter = users.getFilter();
|
||||
|
||||
filter.search = IS( filter.username, username );
|
||||
|
||||
users.sync();
|
||||
|
||||
|
||||
var currentUser = users.get();
|
||||
|
||||
|
||||
if( currentUser ) {
|
||||
|
||||
var hash = currentUser.hash.value;
|
||||
|
||||
|
||||
|
||||
console.log("password", password);
|
||||
|
||||
console.log("try to sign in hash", hash);
|
||||
|
||||
if( !password ) {
|
||||
|
||||
client.user.error = "password required";
|
||||
|
||||
}
|
||||
|
||||
if( !hash ) {
|
||||
|
||||
client.user.error = "user does not have an hash";
|
||||
|
||||
}
|
||||
|
||||
var signedIn = false;
|
||||
|
||||
var loginMethod = false;
|
||||
|
||||
console.log(sessionKey);
|
||||
|
||||
if( sessionKey ) {
|
||||
|
||||
signedIn = sessionKey === currentUser.sessionKey.value;
|
||||
|
||||
loginMethod = "session"
|
||||
|
||||
console.log( "signin with sessionKey:", sessionKey, currentUser.sessionKey.value, signedIn );
|
||||
|
||||
}
|
||||
|
||||
if( password ) {
|
||||
|
||||
var encryptHash = await crypto.pbkdf2Sync( password, currentUser.salt.value, 100, 64,'SHA256' ).toString('hex');
|
||||
|
||||
|
||||
if( !signedIn ) {
|
||||
|
||||
signedIn = encryptHash === hash;
|
||||
|
||||
loginMethod = "password"
|
||||
|
||||
console.log( "signin with Password:", encryptHash, hash, signedIn );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if( signedIn ) {
|
||||
|
||||
if( loginMethod != "session" ) {
|
||||
|
||||
currentUser.sessionKey.value = crypto.randomBytes( 32 ).toString('base64');
|
||||
|
||||
currentUser.save();
|
||||
|
||||
currentUser.sessionKey.scope = "public"
|
||||
|
||||
}
|
||||
|
||||
currentUser.permissionObjects = userManager.getPermissions( currentUser, client );
|
||||
|
||||
currentUser.error = false;
|
||||
|
||||
|
||||
client.user = currentUser;
|
||||
|
||||
client.user.status = "success";
|
||||
|
||||
} else {
|
||||
|
||||
client.user.status = "failed";
|
||||
client.user.error = "password";
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
client.user.status = "failed";
|
||||
client.user.error = "username";
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
client.user.error = "password";
|
||||
|
||||
}
|
||||
|
||||
global.core.setUserObjects( client.user, client );
|
||||
|
||||
global.core.parse( client.user, client );
|
||||
|
||||
return client.user;
|
||||
|
||||
}
|
||||
|
||||
async afterLoad() {
|
||||
|
||||
var username = localStorage.getItem( "username" );
|
||||
|
||||
var sessionKey = localStorage.getItem( "sessionKey" );
|
||||
|
||||
var user = await this.signin( username, false, sessionKey );
|
||||
|
||||
console.log("signin ",user);
|
||||
|
||||
var core = this.getCore();
|
||||
|
||||
core.updatePermissions( user.permissionObjects );
|
||||
|
||||
core.setUser( user );
|
||||
|
||||
}
|
||||
|
||||
|
||||
permission() {
|
||||
|
||||
this.allow( groups.visitor , "PROCESS" );
|
||||
this.allow( groups.member , "PROCESS" );
|
||||
this.allow( groups.admin , "PROCESS" );
|
||||
|
||||
this.allow( groups.member , "READ" );
|
||||
this.allow( groups.admin , "READ" );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user