2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { AccessTokensRepository } from '@/models/index.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2020-03-28 03:24:37 +01:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['auth'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2020-03-28 03:24:37 +01:00
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
2021-03-06 14:34:11 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 14:34:11 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-03-28 03:24:37 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
session: { type: 'string', nullable: true },
|
|
|
|
name: { type: 'string', nullable: true },
|
|
|
|
description: { type: 'string', nullable: true },
|
|
|
|
iconUrl: { type: 'string', nullable: true },
|
|
|
|
permission: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
|
|
|
},
|
|
|
|
required: ['session', 'permission'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
2020-03-28 03:24:37 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private idService: IdService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
// Generate access token
|
|
|
|
const accessToken = secureRndstr(32, true);
|
2020-08-04 16:09:48 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const now = new Date();
|
2020-07-18 05:12:10 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Insert access token doc
|
|
|
|
await this.accessTokensRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: now,
|
|
|
|
lastUsedAt: now,
|
|
|
|
session: ps.session,
|
|
|
|
userId: me.id,
|
|
|
|
token: accessToken,
|
|
|
|
hash: accessToken,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description,
|
|
|
|
iconUrl: ps.iconUrl,
|
|
|
|
permission: ps.permission,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
token: accessToken,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|