2021-03-29 19:58:11 +02:00
|
|
|
import { Response } from 'express';
|
|
|
|
|
|
|
|
import Controller from '../controller';
|
|
|
|
import {
|
|
|
|
ADMIN,
|
|
|
|
CREATE_API_TOKEN,
|
|
|
|
DELETE_API_TOKEN,
|
2022-04-26 10:24:34 +02:00
|
|
|
READ_API_TOKEN,
|
2021-03-29 19:58:11 +02:00
|
|
|
UPDATE_API_TOKEN,
|
2021-05-02 20:58:02 +02:00
|
|
|
} from '../../types/permissions';
|
2021-03-29 19:58:11 +02:00
|
|
|
import { ApiTokenService } from '../../services/api-token-service';
|
2021-04-12 20:25:03 +02:00
|
|
|
import { Logger } from '../../logger';
|
2021-03-29 19:58:11 +02:00
|
|
|
import { AccessService } from '../../services/access-service';
|
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2021-04-22 23:40:52 +02:00
|
|
|
import User from '../../types/user';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
2022-04-26 10:24:34 +02:00
|
|
|
import { ApiTokenType, IApiToken } from '../../types/models/api-token';
|
2021-09-15 20:28:10 +02:00
|
|
|
import { createApiToken } from '../../schema/api-token-schema';
|
2021-03-29 19:58:11 +02:00
|
|
|
|
|
|
|
interface IServices {
|
|
|
|
apiTokenService: ApiTokenService;
|
|
|
|
accessService: AccessService;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ApiTokenController extends Controller {
|
|
|
|
private apiTokenService: ApiTokenService;
|
|
|
|
|
|
|
|
private accessService: AccessService;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-04-09 13:46:53 +02:00
|
|
|
constructor(config: IUnleashConfig, services: IServices) {
|
2021-03-29 19:58:11 +02:00
|
|
|
super(config);
|
|
|
|
this.apiTokenService = services.apiTokenService;
|
|
|
|
this.accessService = services.accessService;
|
|
|
|
this.logger = config.getLogger('api-token-controller.js');
|
|
|
|
|
2022-04-26 10:24:34 +02:00
|
|
|
this.get('/', this.getAllApiTokens, READ_API_TOKEN);
|
2021-03-29 19:58:11 +02:00
|
|
|
this.post('/', this.createApiToken, CREATE_API_TOKEN);
|
|
|
|
this.put('/:token', this.updateApiToken, UPDATE_API_TOKEN);
|
|
|
|
this.delete('/:token', this.deleteApiToken, DELETE_API_TOKEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllApiTokens(req: IAuthRequest, res: Response): Promise<void> {
|
|
|
|
const { user } = req;
|
2022-04-26 10:24:34 +02:00
|
|
|
const tokens = await this.accessibleTokens(user);
|
|
|
|
res.json({ tokens });
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async createApiToken(req: IAuthRequest, res: Response): Promise<any> {
|
2021-09-15 20:28:10 +02:00
|
|
|
const createToken = await createApiToken.validateAsync(req.body);
|
|
|
|
const token = await this.apiTokenService.createApiToken(createToken);
|
|
|
|
return res.status(201).json(token);
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteApiToken(req: IAuthRequest, res: Response): Promise<void> {
|
|
|
|
const { token } = req.params;
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
await this.apiTokenService.delete(token);
|
|
|
|
res.status(200).end();
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateApiToken(req: IAuthRequest, res: Response): Promise<any> {
|
|
|
|
const { token } = req.params;
|
|
|
|
const { expiresAt } = req.body;
|
|
|
|
|
|
|
|
if (!expiresAt) {
|
|
|
|
this.logger.error(req.body);
|
|
|
|
return res.status(400).send();
|
|
|
|
}
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
await this.apiTokenService.updateExpiry(token, expiresAt);
|
|
|
|
return res.status(200).end();
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
2022-04-26 10:24:34 +02:00
|
|
|
|
|
|
|
private async accessibleTokens(user: User): Promise<IApiToken[]> {
|
|
|
|
const allTokens = await this.apiTokenService.getAllTokens();
|
|
|
|
|
|
|
|
if (user.isAPI && user.permissions.includes(ADMIN)) {
|
|
|
|
return allTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (await this.accessService.hasPermission(user, UPDATE_API_TOKEN)) {
|
|
|
|
return allTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allTokens.filter((t) => t.type !== ApiTokenType.ADMIN);
|
|
|
|
}
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ApiTokenController;
|
|
|
|
export default ApiTokenController;
|