なんかもうめっちゃ変えた

This commit is contained in:
syuilo 2022-09-18 03:27:08 +09:00 committed by GitHub
parent d9ab03f086
commit b75184ec8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
946 changed files with 41219 additions and 28839 deletions

View file

@ -1,9 +1,11 @@
import { v4 as uuid } from 'uuid';
import config from '@/config/index.js';
import define from '../../../define.js';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { AppsRepository, AuthSessionsRepository } from '@/models/index.js';
import { IdService } from '@/core/IdService.js';
import { Config } from '@/config.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
import { Apps, AuthSessions } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
export const meta = {
tags: ['auth'],
@ -44,29 +46,45 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps) => {
// Lookup app
const app = await Apps.findOneBy({
secret: ps.appSecret,
});
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.config)
private config: Config,
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
@Inject(DI.appsRepository)
private appsRepository: AppsRepository,
@Inject(DI.authSessionsRepository)
private authSessionsRepository: AuthSessionsRepository,
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
// Lookup app
const app = await this.appsRepository.findOneBy({
secret: ps.appSecret,
});
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
}
// Generate token
const token = uuid();
// Create session token document
const doc = await this.authSessionsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
appId: app.id,
token: token,
}).then(x => this.authSessionsRepository.findOneByOrFail(x.identifiers[0]));
return {
token: doc.token,
url: `${this.config.authUrl}/${doc.token}`,
};
});
}
// Generate token
const token = uuid();
// Create session token document
const doc = await AuthSessions.insert({
id: genId(),
createdAt: new Date(),
appId: app.id,
token: token,
}).then(x => AuthSessions.findOneByOrFail(x.identifiers[0]));
return {
token: doc.token,
url: `${config.authUrl}/${doc.token}`,
};
});
}

View file

@ -1,6 +1,9 @@
import define from '../../../define.js';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { AuthSessionsRepository } from '@/models/index.js';
import { AuthSessionEntityService } from '@/core/entities/AuthSessionEntityService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
import { AuthSessions } from '@/models/index.js';
export const meta = {
tags: ['auth'],
@ -46,15 +49,25 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Lookup session
const session = await AuthSessions.findOneBy({
token: ps.token,
});
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.authSessionsRepository)
private authSessionsRepository: AuthSessionsRepository,
if (session == null) {
throw new ApiError(meta.errors.noSuchSession);
private authSessionEntityService: AuthSessionEntityService,
) {
super(meta, paramDef, async (ps, me) => {
// Lookup session
const session = await this.authSessionsRepository.findOneBy({
token: ps.token,
});
if (session == null) {
throw new ApiError(meta.errors.noSuchSession);
}
return await this.authSessionEntityService.pack(session, me);
});
}
return await AuthSessions.pack(session, user);
});
}

View file

@ -1,6 +1,9 @@
import define from '../../../define.js';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { UsersRepository, AppsRepository, AccessTokensRepository, AuthSessionsRepository } from '@/models/index.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
import { Apps, AuthSessions, AccessTokens, Users } from '@/models/index.js';
export const meta = {
tags: ['auth'],
@ -55,43 +58,62 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps) => {
// Lookup app
const app = await Apps.findOneBy({
secret: ps.appSecret,
});
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
@Inject(DI.appsRepository)
private appsRepository: AppsRepository,
@Inject(DI.authSessionsRepository)
private authSessionsRepository: AuthSessionsRepository,
@Inject(DI.accessTokensRepository)
private accessTokensRepository: AccessTokensRepository,
private userEntityService: UserEntityService,
) {
super(meta, paramDef, async (ps, me) => {
// Lookup app
const app = await this.appsRepository.findOneBy({
secret: ps.appSecret,
});
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
}
// Fetch token
const session = await this.authSessionsRepository.findOneBy({
token: ps.token,
appId: app.id,
});
if (session == null) {
throw new ApiError(meta.errors.noSuchSession);
}
if (session.userId == null) {
throw new ApiError(meta.errors.pendingSession);
}
// Lookup access token
const accessToken = await this.accessTokensRepository.findOneByOrFail({
appId: app.id,
userId: session.userId,
});
// Delete session
this.authSessionsRepository.delete(session.id);
return {
accessToken: accessToken.token,
user: await this.userEntityService.pack(session.userId, null, {
detail: true,
}),
};
});
}
// Fetch token
const session = await AuthSessions.findOneBy({
token: ps.token,
appId: app.id,
});
if (session == null) {
throw new ApiError(meta.errors.noSuchSession);
}
if (session.userId == null) {
throw new ApiError(meta.errors.pendingSession);
}
// Lookup access token
const accessToken = await AccessTokens.findOneByOrFail({
appId: app.id,
userId: session.userId,
});
// Delete session
AuthSessions.delete(session.id);
return {
accessToken: accessToken.token,
user: await Users.pack(session.userId, null, {
detail: true,
}),
};
});
}