2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { UserSecurityKeysRepository } from '@/models/_.js';
|
2023-02-20 07:40:24 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { ApiError } from '../../../error.js';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchKey: {
|
|
|
|
message: 'No such key.',
|
|
|
|
code: 'NO_SUCH_KEY',
|
|
|
|
id: 'f9c5467f-d492-4d3c-9a8g-a70dacc86512',
|
|
|
|
},
|
|
|
|
|
|
|
|
accessDenied: {
|
2023-09-08 05:05:03 +00:00
|
|
|
message: 'You do not have edit privilege of this key.',
|
2023-02-20 07:40:24 +00:00
|
|
|
code: 'ACCESS_DENIED',
|
|
|
|
id: '1fb7cb09-d46a-4fff-b8df-057708cce513',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string', minLength: 1, maxLength: 30 },
|
|
|
|
credentialId: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['name', 'credentialId'],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
@Injectable()
|
2023-08-17 12:20:58 +00:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2023-02-20 07:40:24 +00:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.userSecurityKeysRepository)
|
|
|
|
private userSecurityKeysRepository: UserSecurityKeysRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const key = await this.userSecurityKeysRepository.findOneBy({
|
|
|
|
id: ps.credentialId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (key == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.userId !== me.id) {
|
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
}
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
await this.userSecurityKeysRepository.update(key.id, {
|
|
|
|
name: ps.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Publish meUpdated event
|
|
|
|
this.globalEventService.publishMainStream(me.id, 'meUpdated', await this.userEntityService.pack(me.id, me, {
|
2024-01-31 06:45:35 +00:00
|
|
|
schema: 'MeDetailed',
|
2023-02-20 07:40:24 +00:00
|
|
|
includeSecrets: true,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|