Fixed 267U.pre2
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import * as crypto from "node:crypto";
|
||||
import define from "../../define.js";
|
||||
import { ApiError } from "../../error.js";
|
||||
import { AuthSessions, AccessTokens, Apps } from "../../../../models/index.js";
|
||||
import { genId } from "../../../../misc/gen-id.js";
|
||||
import { secureRndstr } from "../../../../misc/secure-rndstr.js";
|
||||
export const meta = {
|
||||
tags: [
|
||||
"auth"
|
||||
],
|
||||
requireCredential: true,
|
||||
secure: true,
|
||||
errors: {
|
||||
noSuchSession: {
|
||||
message: "No such session.",
|
||||
code: "NO_SUCH_SESSION",
|
||||
id: "9c72d8de-391a-43c1-9d06-08d29efde8df"
|
||||
}
|
||||
}
|
||||
};
|
||||
export const paramDef = {
|
||||
type: "object",
|
||||
properties: {
|
||||
token: {
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
required: [
|
||||
"token"
|
||||
]
|
||||
};
|
||||
export default define(meta, paramDef, async (ps, user)=>{
|
||||
// Fetch token
|
||||
const session = await AuthSessions.findOneBy({
|
||||
token: ps.token
|
||||
});
|
||||
if (session == null) {
|
||||
throw new ApiError(meta.errors.noSuchSession);
|
||||
}
|
||||
// Generate access token
|
||||
const accessToken = secureRndstr(32);
|
||||
// Fetch exist access token
|
||||
const exist = await AccessTokens.exist({
|
||||
where: {
|
||||
appId: session.appId,
|
||||
userId: user.id
|
||||
}
|
||||
});
|
||||
if (!exist) {
|
||||
// Lookup app
|
||||
const app = await Apps.findOneByOrFail({
|
||||
id: session.appId
|
||||
});
|
||||
// Generate Hash
|
||||
const sha256 = crypto.createHash("sha256");
|
||||
sha256.update(accessToken + app.secret);
|
||||
const hash = sha256.digest("hex");
|
||||
const now = new Date();
|
||||
// Insert access token doc
|
||||
await AccessTokens.insert({
|
||||
id: genId(),
|
||||
createdAt: now,
|
||||
lastUsedAt: now,
|
||||
appId: session.appId,
|
||||
userId: user.id,
|
||||
token: accessToken,
|
||||
hash: hash
|
||||
});
|
||||
}
|
||||
// Update session
|
||||
await AuthSessions.update(session.id, {
|
||||
userId: user.id
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import { v4 as uuid } from "uuid";
|
||||
import config from "../../../../../config/index.js";
|
||||
import define from "../../../define.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"
|
||||
],
|
||||
requireCredential: false,
|
||||
res: {
|
||||
type: "object",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
properties: {
|
||||
token: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
nullable: false
|
||||
},
|
||||
url: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
format: "url"
|
||||
}
|
||||
}
|
||||
},
|
||||
errors: {
|
||||
noSuchApp: {
|
||||
message: "No such app.",
|
||||
code: "NO_SUCH_APP",
|
||||
id: "92f93e63-428e-4f2f-a5a4-39e1407fe998"
|
||||
}
|
||||
}
|
||||
};
|
||||
export const paramDef = {
|
||||
type: "object",
|
||||
properties: {
|
||||
appSecret: {
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
required: [
|
||||
"appSecret"
|
||||
]
|
||||
};
|
||||
export default define(meta, paramDef, async (ps)=>{
|
||||
// Lookup app
|
||||
const app = await Apps.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 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}`
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import define from "../../../define.js";
|
||||
import { ApiError } from "../../../error.js";
|
||||
import { AuthSessions } from "../../../../../models/index.js";
|
||||
export const meta = {
|
||||
tags: [
|
||||
"auth"
|
||||
],
|
||||
requireCredential: false,
|
||||
errors: {
|
||||
noSuchSession: {
|
||||
message: "No such session.",
|
||||
code: "NO_SUCH_SESSION",
|
||||
id: "bd72c97d-eba7-4adb-a467-f171b8847250"
|
||||
}
|
||||
},
|
||||
res: {
|
||||
type: "object",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
properties: {
|
||||
id: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
format: "id"
|
||||
},
|
||||
app: {
|
||||
type: "object",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
ref: "App"
|
||||
},
|
||||
token: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
nullable: false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
export const paramDef = {
|
||||
type: "object",
|
||||
properties: {
|
||||
token: {
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
required: [
|
||||
"token"
|
||||
]
|
||||
};
|
||||
export default define(meta, paramDef, async (ps, user)=>{
|
||||
// Lookup session
|
||||
const session = await AuthSessions.findOneBy({
|
||||
token: ps.token
|
||||
});
|
||||
if (session == null) {
|
||||
throw new ApiError(meta.errors.noSuchSession);
|
||||
}
|
||||
return await AuthSessions.pack(session, user);
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import define from "../../../define.js";
|
||||
import { ApiError } from "../../../error.js";
|
||||
import { Apps, AuthSessions, AccessTokens, Users } from "../../../../../models/index.js";
|
||||
export const meta = {
|
||||
tags: [
|
||||
"auth"
|
||||
],
|
||||
requireCredential: false,
|
||||
res: {
|
||||
type: "object",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
properties: {
|
||||
accessToken: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
nullable: false
|
||||
},
|
||||
user: {
|
||||
type: "object",
|
||||
optional: false,
|
||||
nullable: false,
|
||||
ref: "UserDetailedNotMe"
|
||||
}
|
||||
}
|
||||
},
|
||||
errors: {
|
||||
noSuchApp: {
|
||||
message: "No such app.",
|
||||
code: "NO_SUCH_APP",
|
||||
id: "fcab192a-2c5a-43b7-8ad8-9b7054d8d40d"
|
||||
},
|
||||
noSuchSession: {
|
||||
message: "No such session.",
|
||||
code: "NO_SUCH_SESSION",
|
||||
id: "5b5a1503-8bc8-4bd0-8054-dc189e8cdcb3"
|
||||
},
|
||||
pendingSession: {
|
||||
message: "This session is not completed yet.",
|
||||
code: "PENDING_SESSION",
|
||||
id: "8c8a4145-02cc-4cca-8e66-29ba60445a8e"
|
||||
}
|
||||
}
|
||||
};
|
||||
export const paramDef = {
|
||||
type: "object",
|
||||
properties: {
|
||||
appSecret: {
|
||||
type: "string"
|
||||
},
|
||||
token: {
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
required: [
|
||||
"appSecret",
|
||||
"token"
|
||||
]
|
||||
};
|
||||
export default define(meta, paramDef, async (ps)=>{
|
||||
// Lookup app
|
||||
const app = await Apps.findOneBy({
|
||||
secret: ps.appSecret
|
||||
});
|
||||
if (app == null) {
|
||||
throw new ApiError(meta.errors.noSuchApp);
|
||||
}
|
||||
// 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
|
||||
})
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user