unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
62 lines • 2.24 kB
JavaScript
import AuthenticationRequired from '../types/authentication-required.js';
import ApiUser from '../types/api-user.js';
import { ApiTokenType } from '../types/model.js';
import { encrypt } from '../util/index.js';
function demoAuthentication(app, basePath, { userService }, { authentication, flagResolver, }) {
app.post(`${basePath}/auth/demo/login`, async (req, res) => {
let { email } = req.body;
let user;
try {
if (authentication.demoAllowAdminLogin && email === 'admin') {
user = await userService.loginDemoAuthDefaultAdmin();
}
else {
email = flagResolver.isEnabled('encryptEmails', { email })
? encrypt(email)
: email;
user = await userService.loginUserWithoutPassword(email, true);
}
req.session.user = user;
return res.status(200).json(user);
}
catch (e) {
res.status(400)
.json({ error: `Could not sign in with ${email}` })
.end();
}
});
app.use(`${basePath}/api/admin/`, (req, res, next) => {
if (req.session.user?.email || req.session.user?.username === 'admin') {
req.user = req.session.user;
}
next();
});
app.use(`${basePath}/api/client`, (req, res, next) => {
if (!authentication.enableApiToken && !req.user) {
req.user = new ApiUser({
tokenName: 'unauthed-default-client',
permissions: [],
environment: 'default',
type: ApiTokenType.CLIENT,
project: '*',
secret: 'a',
});
}
next();
});
app.use(`${basePath}/api`, (req, res, next) => {
if (req.user) {
return next();
}
return res
.status(401)
.json(new AuthenticationRequired({
path: `${basePath}/auth/demo/login`,
type: 'demo',
message: 'You have to identify yourself in order to use Unleash.',
}))
.end();
});
}
export default demoAuthentication;
//# sourceMappingURL=demo-authentication.js.map