This commit is contained in:
2020-05-05 23:07:00 -04:00
parent f2309463a4
commit 4d51a4ac9e
26 changed files with 476 additions and 189 deletions

View File

@ -1,12 +1,14 @@
'use strict';
const {User} = require('./user');
const {Token, AuthToken} = require('./token');
Auth = {}
var Auth = {}
Auth.errors = {}
Auth.errors.login = function(){
let error = new Error('PamLoginFailed');
error.name = 'PamLoginFailed';
let error = new Error('LDAPLoginFailed');
error.name = 'LDAPLoginFailed';
error.message = `Invalid Credentials, login failed.`;
error.status = 401;

32
nodejs/models/email.js Normal file
View File

@ -0,0 +1,32 @@
'use strict';
const sgMail = require('@sendgrid/mail');
const mustache = require('mustache');
const conf = require('../app').conf;
sgMail.setApiKey(conf.SENDGRID_API_KEY);
var Mail = {};
Mail.send = async function(to, subject, message, from){
await sgMail.send({
to: to,
from: from || 'Theta 42 Accounts <accounts@no-reply.theta42.com>',
subject: subject,
text: message,
html: message,
});
};
Mail.sendTemplate = async function(to, template, context, from){
template = require(`../views/email_templates/${template}`);
await Mail.send(
to,
mustache.render(template.subject, context),
mustache.render(template.message, context),
from || (template.from && mustache.render(template.message, context))
)
};
module.exports = {Mail};

View File

@ -4,7 +4,7 @@ const { Client, Attribute, Change } = require('ldapts');
const conf = require('../app').conf.ldap;
const client = new Client({
url: conf.url,
url: conf.url,
});
async function getGroups(client){
@ -25,21 +25,21 @@ async function getGroups(client){
}
async function addGroup(client, data){
try{
try{
await client.add(`cn=${data.name},${conf.groupBase}`, {
cn: data.name,
member: data.owner,
description: data.description,
owner: data.owner,
objectclass: [ 'groupOfNames', 'top' ]
});
await client.add(`cn=${data.name},${conf.groupBase}`, {
cn: data.name,
member: data.owner,
description: data.description,
owner: data.owner,
objectclass: [ 'groupOfNames', 'top' ]
});
return data;
return data;
}catch(error){
throw error;
}
}catch(error){
throw error;
}
}
async function addMember(client, group, user){
@ -61,18 +61,18 @@ async function addMember(client, group, user){
async function removeMember(client, group, user){
try{
await client.modify(group.dn, [
new Change({
operation: 'delete',
modification: new Attribute({
type: 'member',
values: [user.dn]
})}),
]);
}catch(error){
if(error = "TypeOrValueExistsError")return ;
throw error;
}
await client.modify(group.dn, [
new Change({
operation: 'delete',
modification: new Attribute({
type: 'member',
values: [user.dn]
})}),
]);
}catch(error){
if(error = "TypeOrValueExistsError")return ;
throw error;
}
}
@ -134,4 +134,19 @@ Group.get = async function(data){
}
}
Group.add = async function(data){
try{
await client.bind(conf.bindDN, conf.bindPassword);
await addGroup(client, data);
await client.unbind();
return this.get(data);
}catch(error){
}
}
module.exports = {Group};

View File

@ -29,7 +29,9 @@ Token.check = async function(data){
var InviteToken = Object.create(Token({
name: 'invite',
keyMap:{
claimed_by: {default:"__NONE__", isRequired: false, type: 'string',}
claimed_by: {default:"__NONE__", isRequired: false, type: 'string',},
mail: {default:"__NONE__", isRequired: false, type: 'string',},
mail_token: {default: UUID, type: 'string', min: 36, max: 36},
}
}));
@ -53,7 +55,7 @@ var AuthToken = Object.create(Token({
}));
AuthToken.add = async function(data){
data.created_by = data.username;
data.created_by = data.uid;
return AuthToken.__proto__.add(data);
};

View File

@ -3,6 +3,7 @@
const { Client, Attribute, Change } = require('ldapts');
const crypto = require('crypto');
const {Mail} = require('./email');
const {Token, InviteToken} = require('./token');
const conf = require('../app').conf.ldap;
@ -47,7 +48,9 @@ async function addPosixAccount(client, data){
uid: data.uid,
uidNumber: data.uidNumber,
gidNumber: data.gidNumber,
givenName: data.givenName,
mail: data.mail,
mobile: data.mobile,
loginShell: data.loginShell,
homeDirectory: data.homeDirectory,
userPassword: data.userPassword,
@ -81,6 +84,14 @@ async function addLdapUser(client, data){
}
}
async function deleteLdapUser(client, data){
try{
await client.del(`cn=${data.cn},${conf.groupBase}`);
await client.del(data.dn);
}catch(error){
throw error;
}
}
async function changeLdapPassword(client, data){
try{
@ -100,14 +111,8 @@ async function changeLdapPassword(client, data){
const user_parse = function(data){
if(data[conf.userNameAttribute]){
data.username = data[conf.userNameAttribute]
// delete data[conf.userNameAttribute];
}
// if(data.uidNumber){
// data.uid = data.uidNumber;
// delete data.uidNumber;
// }
return data;
}
@ -115,11 +120,6 @@ var User = {}
User.backing = "LDAP";
User.keyMap = {
'username': {isRequired: true, type: 'string', min: 3, max: 500},
'password': {isRequired: true, type: 'string', min: 3, max: 500},
}
User.list = async function(){
try{
await client.bind(conf.bindDN, conf.bindPassword);
@ -170,14 +170,14 @@ User.listDetail = async function(){
User.get = async function(data){
try{
if(typeof data !== 'object'){
let username = data;
let uid = data;
data = {};
data.username = username;
data.uid = uid;
}
await client.bind(conf.bindDN, conf.bindPassword);
let filter = `(&${conf.userFilter}(${conf.userNameAttribute}=${data.username}))`;
let filter = `(&${conf.userFilter}(${conf.userNameAttribute}=${data.uid}))`;
const res = await client.search(conf.userBase, {
scope: 'sub',
@ -225,7 +225,18 @@ User.add = async function(data) {
await client.unbind();
return this.get(data.uid);
let user = await this.get(data.uid);
await Mail.sendTemplate(
user.mail,
'welcome',
{
user: user
}
)
return user;
}catch(error){
if(error.message.includes('exists')){
@ -244,7 +255,7 @@ User.addByInvite = async function(data){
try{
let token = await InviteToken.get(data.token);
if(!token.is_valid){
if(!token.is_valid && data.mailToken !== token.mail_token){
let error = new Error('Token Invalid');
error.name = 'Token Invalid';
error.message = `Token is not valid or as allready been used. ${data.token}`;
@ -252,6 +263,8 @@ User.addByInvite = async function(data){
throw error;
}
data.mail = token.mail;
let user = await this.add(data);
if(user){
@ -265,13 +278,37 @@ User.addByInvite = async function(data){
};
// User.remove = async function(data){
// try{
// return await linuxUser.removeUser(this.username);
// }catch(error){
// throw error;
// }
// };
User.verifyEmail = async function(data){
try{
let token = await InviteToken.get(data.token);
await token.update({mail: data.mail})
await Mail.sendTemplate(
data.mail,
'validate_link',
{
link:`${data.url}/login/invite/${token.token}/${token.mail_token}`
}
)
}catch(error){
throw error;
}
};
User.remove = async function(data){
try{
await client.bind(conf.bindDN, conf.bindPassword);
await deleteLdapUser(client, this);
await client.unbind();
return true
}catch(error){
throw error;
}
};
// User.setPassword = async function(data){
// try{