Move token enpoints from /users to /me

This commit is contained in:
ricardo 2022-04-25 20:00:31 -04:00
parent 43c58b23d9
commit 90af2ef6ab
2 changed files with 49 additions and 30 deletions

View file

@ -11,6 +11,8 @@ import { validatePassword, sanitizeUser, compareHash, hashPassword } from '../Us
import type { AuthenticatedRequest, MeRequest } from '../../requests';
import { validateEntity } from '../../GenericHelpers';
import { User } from '../../databases/entities/User';
import { randomBytes } from 'crypto';
import { userInfo } from 'os';
export function meNamespace(this: N8nApp): void {
/**
@ -149,4 +151,50 @@ export function meNamespace(this: N8nApp): void {
return { success: true };
}),
);
/**
* Creates an API Key
*/
this.app.post(
`/${this.restEndpoint}/users/me/api-key`,
ResponseHelper.send(async (req: AuthenticatedRequest) => {
const ramdonToken = randomBytes(20).toString('hex');
const apiKey = `n8n_api_${ramdonToken}`;
await Db.collections.User!.update(req.user.id, {
apiKey,
});
return { apiKey, success: true };
}),
);
/**
* Deletes an API Key
*/
this.app.delete(
`/${this.restEndpoint}/me/api-key`,
ResponseHelper.send(async (req: AuthenticatedRequest) => {
await Db.collections.User!.update(req.user.id, {
apiKey: null,
});
return { success: true };
}),
);
/**
* Get an API Key
*/
this.app.delete(
`/${this.restEndpoint}/me/api-key`,
ResponseHelper.send(async () => {
const user = await Db.collections.User!.findOne({
where: {
apiKey: null,
},
});
if (user) {
return { apiKey: user.apiKey, success: true };
}
}),
);
}

View file

@ -5,10 +5,9 @@ import { In } from 'typeorm';
import validator from 'validator';
import { LoggerProxy as Logger } from 'n8n-workflow';
import { randomBytes } from 'crypto';
import { Db, InternalHooksManager, ITelemetryUserDeletionData, ResponseHelper } from '../..';
import { N8nApp, PublicUser } from '../Interfaces';
import { AuthenticatedRequest, UserRequest } from '../../requests';
import { UserRequest } from '../../requests';
import {
getInstanceBaseUrl,
hashPassword,
@ -565,32 +564,4 @@ export function usersNamespace(this: N8nApp): void {
return { success: true };
}),
);
/**
* Creates an API Key
*/
this.app.post(
`/${this.restEndpoint}/users/me/api-key`,
ResponseHelper.send(async (req: AuthenticatedRequest) => {
const ramdonToken = randomBytes(20).toString('hex');
const apiKey = `n8n_api_${ramdonToken}`;
await Db.collections.User!.update(req.user.id, {
apiKey,
});
return { apiKey, success: true };
}),
);
/**
* Deletes an API Key
*/
this.app.delete(
`/${this.restEndpoint}/users/me/api-key`,
ResponseHelper.send(async (req: AuthenticatedRequest) => {
await Db.collections.User!.update(req.user.id, {
apiKey: null,
});
return { success: true };
}),
);
}