2022-01-28 12:50:35 +01:00
|
|
|
import { Response } from 'express';
|
2022-06-23 09:40:25 +02:00
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
2022-01-28 12:50:35 +01:00
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import { IUnleashConfig } from '../../server-impl';
|
|
|
|
import UserService from '../../services/user-service';
|
|
|
|
import { IUnleashServices } from '../../types';
|
|
|
|
import { NONE } from '../../types/permissions';
|
|
|
|
import Controller from '../controller';
|
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2022-06-23 09:40:25 +02:00
|
|
|
import { userSchema, UserSchema } from '../../openapi/spec/user-schema';
|
|
|
|
import { LoginSchema } from '../../openapi/spec/login-schema';
|
|
|
|
import { serializeDates } from '../../types/serialize-dates';
|
2023-07-04 10:31:54 +02:00
|
|
|
import { getStandardResponses } from '../../openapi';
|
2022-01-28 12:50:35 +01:00
|
|
|
|
2022-06-23 09:40:25 +02:00
|
|
|
export class SimplePasswordProvider extends Controller {
|
2022-01-28 12:50:35 +01:00
|
|
|
private logger: Logger;
|
|
|
|
|
2022-06-23 09:40:25 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
|
|
|
private userService: UserService;
|
|
|
|
|
2022-01-28 12:50:35 +01:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2022-06-23 09:40:25 +02:00
|
|
|
{
|
|
|
|
userService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<IUnleashServices, 'userService' | 'openApiService'>,
|
2022-01-28 12:50:35 +01:00
|
|
|
) {
|
|
|
|
super(config);
|
|
|
|
this.logger = config.getLogger('/auth/password-provider.js');
|
2022-06-23 09:40:25 +02:00
|
|
|
this.openApiService = openApiService;
|
2022-01-28 12:50:35 +01:00
|
|
|
this.userService = userService;
|
|
|
|
|
2022-06-23 09:40:25 +02:00
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/login',
|
|
|
|
handler: this.login,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Auth'],
|
2023-07-04 10:31:54 +02:00
|
|
|
summary: 'Log in',
|
|
|
|
description:
|
|
|
|
'Logs in the user and creates an active session',
|
2022-06-23 09:40:25 +02:00
|
|
|
operationId: 'login',
|
|
|
|
requestBody: createRequestSchema('loginSchema'),
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('userSchema'),
|
2023-07-04 10:31:54 +02:00
|
|
|
...getStandardResponses(401),
|
2022-06-23 09:40:25 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2022-01-28 12:50:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 09:40:25 +02:00
|
|
|
async login(
|
|
|
|
req: IAuthRequest<void, void, LoginSchema>,
|
|
|
|
res: Response<UserSchema>,
|
|
|
|
): Promise<void> {
|
2022-01-28 12:50:35 +01:00
|
|
|
const { username, password } = req.body;
|
|
|
|
|
|
|
|
const user = await this.userService.loginUser(username, password);
|
|
|
|
req.session.user = user;
|
2022-06-23 09:40:25 +02:00
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
userSchema.$id,
|
|
|
|
serializeDates(user),
|
|
|
|
);
|
2022-01-28 12:50:35 +01:00
|
|
|
}
|
|
|
|
}
|