2020-04-25 16:13:07 +02:00
|
|
|
import ApplicationComponent from "wms-core/ApplicationComponent";
|
2020-07-20 16:34:35 +02:00
|
|
|
import {Express} from "express";
|
|
|
|
import ldap, {InvalidCredentialsError, Server} from "ldapjs";
|
2020-04-25 16:13:07 +02:00
|
|
|
import Logger from "wms-core/Logger";
|
2020-07-25 18:24:12 +02:00
|
|
|
import UserPasswordComponent from "./models/UserPasswordComponent";
|
2020-04-25 16:13:07 +02:00
|
|
|
import Throttler from "wms-core/Throttler";
|
2020-07-25 18:24:12 +02:00
|
|
|
import User from "wms-core/auth/models/User";
|
2020-04-25 16:13:07 +02:00
|
|
|
|
|
|
|
export default class LDAPServerComponent extends ApplicationComponent<void> {
|
|
|
|
private server?: Server;
|
|
|
|
|
2020-07-20 16:34:35 +02:00
|
|
|
public async start(app: Express): Promise<void> {
|
2020-04-25 16:13:07 +02:00
|
|
|
this.server = ldap.createServer({
|
|
|
|
log: console
|
|
|
|
});
|
|
|
|
this.server.bind('ou=users,dc=toot,dc=party', async (req: any, res: any, next: any) => {
|
|
|
|
const rdns = req.dn.toString().split(', ').map((rdn: string) => rdn.split('='));
|
|
|
|
let username;
|
|
|
|
let email;
|
|
|
|
for (const rdn of rdns) {
|
|
|
|
if (rdn[0] === 'cn') {
|
|
|
|
username = rdn[1];
|
|
|
|
} else if (rdn[0] === 'email') {
|
|
|
|
email = rdn[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug('Matrix authentication attempt:', username, email);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Throttler.throttle('ldap_auth', 3, 30 * 1000, username);
|
|
|
|
} catch (e) {
|
|
|
|
Logger.debug('Too many auth requests');
|
|
|
|
next(new InvalidCredentialsError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-25 18:24:12 +02:00
|
|
|
const user = await User.select().where('name', username).first();
|
2020-04-25 16:13:07 +02:00
|
|
|
if (user) {
|
2020-07-20 16:34:35 +02:00
|
|
|
const email = await user.mainEmail.get();
|
2020-04-25 16:13:07 +02:00
|
|
|
if (email) {
|
2020-07-25 18:24:12 +02:00
|
|
|
if (await user.as(UserPasswordComponent).verifyPassword(req.credentials)) {
|
2020-04-25 16:13:07 +02:00
|
|
|
Logger.debug('Success');
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug('Fail');
|
|
|
|
next(new InvalidCredentialsError());
|
|
|
|
});
|
|
|
|
this.server.unbind((req: any, res: any, next: any) => {
|
|
|
|
Logger.debug('Unbind', req);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
this.server.listen(8389, '127.0.0.1', () => {
|
|
|
|
Logger.info(`LDAP server listening on ${this.server!.url}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|