Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions src/http/middleware/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,7 @@ export const authMiddleware = createMiddleware<Env>(async (ctx, next) => {
return errorThrow(ErrorCode.Validation, token.summary);
}

const dotIndex = token.indexOf(".");
if (dotIndex === -1) {
// unhashed token
if (token.length === 32) {
// @ts-expect-error unindexed select
const id = mutableDatabase.user.get("token", token)?.id;
if (!id) {
return errorThrow(ErrorCode.UserInvalidToken);
}

ctx.set("userId", id);

return next();
}

return errorThrow(ErrorCode.UserInvalidToken);
}

const id = token.slice(0, dotIndex);
const id = token.slice(0, token.indexOf("."));
if (!id) {
return errorThrow(ErrorCode.UserInvalidToken);
}
Expand Down
6 changes: 1 addition & 5 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export const initUnhashedTokenCheck = (): void => {
}

if (userUnhashedToken) {
log.warn(
"Users with unhashed tokens found!",
"Those users may lose access in future versions of JSPaste!",
"See: https://github.com/jspaste/backend/issues/318"
);
log.error("Users with unhashed tokens found!", "See: https://github.com/jspaste/backend/issues/318");
}
};
4 changes: 1 addition & 3 deletions src/utils/validator/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ export const validatorDocumentListObject = type({
name: validatorDocumentName,
created: validatorCreationTimestamp
}).configure({
// FIXME: schema references not being generated when using toOpenAPISchema()
// Invalid object key "DocumentListMetadata" at position 2 in "/components/schemas/DocumentListMetadata": key not found in object
//ref: "DocumentListMetadata",
ref: "DocumentListObject",
description: "An object with document metadata"
});
29 changes: 3 additions & 26 deletions src/utils/validator/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { type } from "arktype";

import { constantUserTokenLength } from "#/global.ts";

import { regexBase64URL, regexHeaderBearer } from "../regex.ts";
import { regexHeaderBearer } from "../regex.ts";

// FIXME: schema references not being generated when using toOpenAPISchema()
export const validatorUserToken = type.string.exactlyLength(constantUserTokenLength).configure({
ref: "UserToken.default",
ref: "UserToken",
description: "A user token",
examples: ["myUserTokenHere"],
expected: (ctx) => {
Expand All @@ -25,31 +24,9 @@ export const validatorUserToken = type.string.exactlyLength(constantUserTokenLen
}
});

export const validatorUserTokenLegacy = type(regexBase64URL)
.exactlyLength(32)
.configure({
ref: "UserToken.legacy",
description: "An unhashed user token",
examples: ["myUserTokenHere"],
expected: (ctx) => {
// oxlint-disable-next-line typescript-eslint/switch-exhaustiveness-check
switch (ctx.code) {
case "pattern": {
return "a valid Base64URL";
}
case "exactLength": {
return `exactly ${ctx.rule} characters`;
}
default: {
return "valid";
}
}
}
});

export const validatorUserHeader = type(regexHeaderBearer)
.configure({
description: "A RFC 6750 structured Bearer header",
expected: "a valid header"
})
.pipe((string) => string.split(" ")[1], validatorUserToken.or(validatorUserTokenLegacy));
.pipe((string) => string.split(" ")[1], validatorUserToken);