@nephele/authenticator-pam
Version:
PAM based authenticator (local system users) for the Nephele WebDAV server.
44 lines • 1.61 kB
JavaScript
import basicAuth from 'basic-auth';
import { UnauthorizedError } from 'nephele';
import User from './User.js';
export default class Authenticator {
constructor({ realm = 'Nephele WebDAV Service', unauthorizedAccess = false, allowedUIDs = '500-59999', } = {}) {
this.realm = realm;
this.unauthorizedAccess = unauthorizedAccess;
this.allowedUIDs = allowedUIDs.split(',').map((range) => range.trim());
}
async authenticate(request, response) {
const authorization = request.get('Authorization');
let username = '';
let password = '';
if (authorization) {
const auth = basicAuth.parse(authorization);
if (auth) {
username = auth.name;
password = auth.pass;
}
}
try {
if (username.trim() === '') {
throw new UnauthorizedError('Authentication is required to use this server.');
}
const user = new User({ username });
await user.authenticate(password, request.ip);
await user.checkUID(this.allowedUIDs);
return user;
}
catch (e) {
if (e instanceof UnauthorizedError) {
response.set('WWW-Authenticate', `Basic realm="${this.realm}", charset="UTF-8"`);
}
if (this.unauthorizedAccess) {
return new User({ username: 'nobody' });
}
throw e;
}
}
async cleanAuthentication(_request, _response) {
return;
}
}
//# sourceMappingURL=Authenticator.js.map