UNPKG

@nephele/authenticator-nymph

Version:

Nymph.js authenticator for the Nephele WebDAV server.

46 lines 1.65 kB
import basicAuth from 'basic-auth'; import { User as UserClass, enforceTilmeld, } from '@nymphjs/tilmeld'; import { UnauthorizedError } from 'nephele'; export default class Authenticator { constructor(config) { this.realm = config.realm || 'Nephele WebDAV Service'; this.nymph = config.nymph; } 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.'); } enforceTilmeld(this.nymph); const User = this.nymph.getEntityClass(UserClass); const user = await User.factoryUsername(username); if (user.guid == null || user.username == null || !user.enabled || !user.$checkPassword(password)) { throw new UnauthorizedError('Username or password is invalid.'); } return user; } catch (e) { if (e instanceof UnauthorizedError) { response.set('WWW-Authenticate', `Basic realm="${this.realm}", charset="UTF-8"`); } throw e; } } async cleanAuthentication(_request, _response) { return; } } //# sourceMappingURL=Authenticator.js.map