2022-09-30 13:01:32 +02:00
|
|
|
import { Response } from 'express';
|
|
|
|
|
|
|
|
import Controller from './controller';
|
|
|
|
import { NONE } from '../types/permissions';
|
|
|
|
import { Logger } from '../logger';
|
|
|
|
import { IAuthRequest } from './unleash-types';
|
|
|
|
import { IUnleashConfig, IUnleashServices } from '../types';
|
|
|
|
import { OpenApiService } from '../services/openapi-service';
|
|
|
|
import { createRequestSchema } from '../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../openapi/util/create-response-schema';
|
|
|
|
import { serializeDates } from '../types/serialize-dates';
|
|
|
|
import {
|
|
|
|
emptyResponse,
|
|
|
|
getStandardResponses,
|
|
|
|
} from '../openapi/util/standard-responses';
|
|
|
|
import { PublicSignupTokenService } from '../services/public-signup-token-service';
|
|
|
|
import { UserSchema, userSchema } from '../openapi/spec/user-schema';
|
|
|
|
import { CreateInvitedUserSchema } from '../openapi/spec/create-invited-user-schema';
|
|
|
|
|
|
|
|
interface TokenParam {
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PublicInviteController extends Controller {
|
|
|
|
private publicSignupTokenService: PublicSignupTokenService;
|
|
|
|
|
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{
|
|
|
|
publicSignupTokenService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashServices,
|
|
|
|
'publicSignupTokenService' | 'openApiService'
|
|
|
|
>,
|
|
|
|
) {
|
|
|
|
super(config);
|
|
|
|
this.publicSignupTokenService = publicSignupTokenService;
|
|
|
|
this.openApiService = openApiService;
|
|
|
|
this.logger = config.getLogger('validate-invite-token-controller.js');
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/:token/validate',
|
|
|
|
handler: this.validate,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['Public signup tokens'],
|
|
|
|
operationId: 'validatePublicSignupToken',
|
2022-10-10 15:12:11 +02:00
|
|
|
summary: `Check whether a public sign-up token exists, has not expired and is enabled`,
|
2022-09-30 13:01:32 +02:00
|
|
|
responses: {
|
|
|
|
200: emptyResponse,
|
|
|
|
...getStandardResponses(400),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/:token/signup',
|
|
|
|
handler: this.addTokenUser,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['Public signup tokens'],
|
|
|
|
operationId: 'addPublicSignupTokenUser',
|
fix: Fix broken OpenAPI (#2379)
## What
This change removes the use of double quotes in the
'addPublicSignupTokenUser' endpoint summary. It also changes the
original summary to a description and adds a new, shorter summary.
## Why
The OpenAPI / docusaurus integration errors out (refer to [this failed
build](https://github.com/Unleash/unleash/actions/runs/3434792557/jobs/5726445104))
if the frontmatter contains invalid characters. In this case, it's
because the automatic sidebar label contains double quotes, which it
interprets as a new key having been declared:
```
Error: Error while parsing Markdown front matter.
This can happen if you use special characters in front matter values (try using double quotes around that value).
Error: Loading of version failed for version current
Error: Unable to build website for locale en.
Error: YAMLException: can not read a block mapping entry; a multiline key may not be an implicit key at line 4, column 12:
description: "Create a user with the 'viewe ...
^
```
For some reason, I cannot reproduce this error locally. Instead, the
generation goes as expected.
---
Regarding using description instead of summary: summaries should be very
short and sweet, especially because they're also used in the generated
sidebar. Descriptions can be a bit wordier, so I added a shorter summary
for going forward.
## Generated output
This is what the old configuration would generate. Notice the
`sidedar_label` key on line 2:
```md
---
id: add-public-signup-token-user
sidebar_label: Create a user with the "viewer" root role and link them to a signup token
hide_title: true
hide_table_of_contents: true
api: {'tags': ['Public signup tokens'], 'operationId': 'addPublicSignupTokenUser', 'requestBody': {'description': 'createInvitedUserSchema', 'required': true, 'content': {'application/json': {'schema': {'type': 'object', 'additionalProperties': false, 'required': ['email', 'name', 'password'], 'properties': {'username': { 'type': 'string' }, 'email': { 'type': 'string' }, 'name': { 'type': 'string' }, 'password': { 'type': 'string' },},},},},}, 'responses': {'200': {'description': 'userSchema', 'content': {'application/json': {'schema': {'type': 'object', 'additionalProperties': false, 'required': ['id'], 'properties': {'id': {'type': 'number',}, 'isAPI': {'type': 'boolean',}, 'name': {'type': 'string',}, 'email': {'type': 'string',}, 'username': {'type': 'string',}, 'imageUrl': {'type': 'string',}, 'inviteLink': {'type': 'string',}, 'loginAttempts': {'type': 'number',}, 'emailSent': {'type': 'boolean',}, 'rootRole': {'type': 'number',}, 'seenAt': {'type': 'string', 'format': 'date-time', 'nullable': true,}, 'createdAt': {'type': 'string', 'format': 'date-time',},},},},},}, '400': {'description': 'The request data does not match what we expect.',}, '409': {'description': 'The provided resource can not be created or updated because it would conflict with the current state of the resource or with an already existing resource, respectively.',},}, 'parameters': [{'name': 'token', 'in': 'path', 'required': true, 'schema': { 'type': 'string' },},], 'description': 'Create a user with the "viewer" root role and link them to a signup token', 'method': 'post', 'path': '/invite/{token}/signup', 'servers': [{ 'url': '<your-unleash-url>' }], 'security': [{ 'apiKey': [] }], 'securitySchemes': {'apiKey': {'type': 'apiKey', 'in': 'header', 'name': 'Authorization',},}, 'jsonRequestBodyExample': {'username': 'string', 'email': 'string', 'name': 'string', 'password': 'string',}, 'info': { 'title': 'Unleash API', 'version': '4.17.2' }, 'postman': {'name': 'Create a user with the "viewer" root role and link them to a signup token', 'description': { 'type': 'text/plain' }, 'url': {'path': ['invite', ':token', 'signup'], 'host': ['{{baseUrl}}'], 'query': [], 'variable': [{'disabled': false, 'description': {'content': '(Required) ', 'type': 'text/plain',}, 'type': 'any', 'value': '', 'key': 'token',},],}, 'header': [{ 'key': 'Content-Type', 'value': 'application/json' }, { 'key': 'Accept', 'value': 'application/json' },], 'method': 'POST', 'body': {'mode': 'raw', 'raw': '""', 'options': { 'raw': { 'language': 'json' } }}}}
sidebar_class_name: 'post api-method'
info_path: docs/reference/api/unleash/unleash-api
---
import ApiTabs from "@theme/ApiTabs"; import MimeTabs from "@theme/MimeTabs"; import ParamsItem from "@theme/ParamsItem"; import ResponseSamples from "@theme/ResponseSamples"; import SchemaItem from "@theme/SchemaItem" import SchemaTabs from "@theme/SchemaTabs"; import DiscriminatorTabs from "@theme/DiscriminatorTabs"; import TabItem from "@theme/TabItem";
## Create a user with the "viewer" root role and link them to a signup token
Create a user with the "viewer" root role and link them to a signup token
<!-- And much much more! -->
```
2022-11-10 22:55:01 +01:00
|
|
|
summary: 'Add a user via a signup token',
|
|
|
|
description:
|
|
|
|
'Create a user with the viewer root role and link them to the provided signup token',
|
2022-09-30 13:01:32 +02:00
|
|
|
requestBody: createRequestSchema('createInvitedUserSchema'),
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('userSchema'),
|
|
|
|
...getStandardResponses(400, 409),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async validate(
|
|
|
|
req: IAuthRequest<TokenParam, void>,
|
2022-10-10 15:12:11 +02:00
|
|
|
res: Response,
|
2022-09-30 13:01:32 +02:00
|
|
|
): Promise<void> {
|
|
|
|
const { token } = req.params;
|
|
|
|
const valid = await this.publicSignupTokenService.validate(token);
|
|
|
|
if (valid) {
|
2022-11-10 08:20:15 +01:00
|
|
|
res.status(200).end();
|
2022-09-30 13:01:32 +02:00
|
|
|
} else {
|
2022-11-10 08:20:15 +01:00
|
|
|
res.status(400).end();
|
2022-09-30 13:01:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async addTokenUser(
|
|
|
|
req: IAuthRequest<TokenParam, void, CreateInvitedUserSchema>,
|
|
|
|
res: Response<UserSchema>,
|
|
|
|
): Promise<void> {
|
|
|
|
const { token } = req.params;
|
|
|
|
const valid = await this.publicSignupTokenService.validate(token);
|
|
|
|
if (!valid) {
|
2022-11-10 08:20:15 +01:00
|
|
|
res.status(400).end();
|
|
|
|
return;
|
2022-09-30 13:01:32 +02:00
|
|
|
}
|
|
|
|
const user = await this.publicSignupTokenService.addTokenUser(
|
|
|
|
token,
|
|
|
|
req.body,
|
|
|
|
);
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
201,
|
|
|
|
res,
|
|
|
|
userSchema.$id,
|
|
|
|
serializeDates(user),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|