Skip to content
Closed
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
7 changes: 6 additions & 1 deletion .lintstagedrc.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default {
"*.{j,t}s": [() => "npm run build:src:tsgo", "eslint --concurrency 4" /* sweet spot it seems */, "prettier --write"],
"src/schemas/{*,**/*}.ts": [() => "npm run build:src:tsgo", () => "node scripts/schema.js", () => "node scripts/openapi.js", () => "git add assets/schemas.json assets/openapi.json"],
"src/schemas/{*,**/*}.ts": [
() => "npm run build:src:tsgo",
() => "node scripts/schema.js",
() => "node scripts/openapi.js",
() => "git add assets/schemas.json assets/openapi.json",
],
};
16 changes: 13 additions & 3 deletions assets/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -901,11 +901,15 @@
"friend_sync": {
"type": "boolean"
},
"openid_params": {}
"two_way_link_code": {
"type": "string"
},
"openid_params": {
"$ref": "#/components/schemas/ConnectionCallbackOpenIdParams"
}
},
"required": [
"friend_sync",
"insecure",
"code",
"state"
]
},
Expand Down Expand Up @@ -7834,6 +7838,12 @@
"fetched_at"
]
},
"ConnectionCallbackOpenIdParams": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"AllowedMentions": {
"type": "object",
"properties": {
Expand Down
17 changes: 14 additions & 3 deletions assets/schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,16 @@
"friend_sync": {
"type": "boolean"
},
"openid_params": {}
"two_way_link_code": {
"type": "string"
},
"openid_params": {
"$ref": "#/definitions/ConnectionCallbackOpenIdParams"
}
},
"additionalProperties": false,
"required": [
"friend_sync",
"insecure",
"code",
"state"
],
"$schema": "http://json-schema.org/draft-07/schema#"
Expand Down Expand Up @@ -8338,6 +8342,13 @@
],
"$schema": "http://json-schema.org/draft-07/schema#"
},
"ConnectionCallbackOpenIdParams": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"$schema": "http://json-schema.org/draft-07/schema#"
},
"AllowedMentions": {
"type": "object",
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default defineConfig([
// "sort-imports": ["error", {}],
"default-case": "error",
"default-case-last": "error",
"yoda": "error",
yoda: "error",
// unsure what the defaults are here, but we want them to error
"for-direction": "error",
"constructor-super": "error",
Expand Down
2 changes: 1 addition & 1 deletion scripts/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function combineSchemas(schemas) {
specification.components = specification.components || {};
specification.components.schemas = specification.components.schemas || {};
specification.components.schemas[key] = definitions[key];
delete definitions[key].additionalProperties;
if (definitions[key].additionalProperties === false) delete definitions[key].additionalProperties;
delete definitions[key].$schema;
const definition = definitions[key];

Expand Down
4 changes: 3 additions & 1 deletion src/cdn/util/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) {
const forcePathStyle = process.env.STORAGE_FORCE_PATH_STYLE === "true";

if (process.env.STORAGE_FORCE_PATH_STYLE === undefined) {
console.warn(`[CDN] STORAGE_FORCE_PATH_STYLE is not set for S3 provider; defaulting to virtual-hosted style. Set STORAGE_FORCE_PATH_STYLE=true to enable path-style addressing.`);
console.warn(
`[CDN] STORAGE_FORCE_PATH_STYLE is not set for S3 provider; defaulting to virtual-hosted style. Set STORAGE_FORCE_PATH_STYLE=true to enable path-style addressing.`,
);
}

const { S3Storage } = require("./S3Storage");
Expand Down
133 changes: 133 additions & 0 deletions src/schemas/uncategorised/ConnectionCallbackSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";
import { describe, test } from "node:test";
import Ajv from "ajv";

const schemaPath = path.join(process.cwd(), "assets", "schemas.json");
const openApiPath = path.join(process.cwd(), "assets", "openapi.json");
const rawSchemas = JSON.parse(readFileSync(schemaPath, "utf8"));
const ajvSchemas = JSON.parse(readFileSync(schemaPath, "utf8").replaceAll("#/definitions/", ""));
const openApi = JSON.parse(readFileSync(openApiPath, "utf8"));

describe("ConnectionCallbackSchema", () => {
test("emits typed OpenID params and Discord-compatible callback fields", () => {
const schema = rawSchemas.ConnectionCallbackSchema;

assert.equal(schema.properties.code.type, "string");
assert.equal(schema.properties.state.type, "string");
assert.equal(schema.properties.insecure.type, "boolean");
assert.equal(schema.properties.friend_sync.type, "boolean");
assert.equal(schema.properties.two_way_link_code.type, "string");
assert.deepEqual(schema.properties.openid_params, {
$ref: "#/definitions/ConnectionCallbackOpenIdParams",
});
assert.deepEqual(schema.required, ["code", "state"]);
assert.deepEqual(rawSchemas.ConnectionCallbackOpenIdParams, {
type: "object",
additionalProperties: {
type: "string",
},
$schema: "http://json-schema.org/draft-07/schema#",
});
});

test("preserves typed OpenID params in generated OpenAPI", () => {
assert.deepEqual(openApi.components.schemas.ConnectionCallbackOpenIdParams, {
type: "object",
additionalProperties: {
type: "string",
},
});
assert.deepEqual(openApi.components.schemas.ConnectionCallbackSchema.properties.openid_params, {
$ref: "#/components/schemas/ConnectionCallbackOpenIdParams",
});
});

test("validates callback payloads", () => {
const ajv = new Ajv({
allErrors: true,
schemas: ajvSchemas,
strict: true,
strictRequired: true,
allowUnionTypes: true,
});

const validate = ajv.getSchema("ConnectionCallbackSchema");
assert.ok(validate);

assert.equal(
validate({
code: "oauth-code",
state: "state",
openid_params: {
id_token: "id-token",
access_token: "access-token",
scope: "openid profile",
},
}),
true,
);

assert.equal(
validate({
code: "oauth-code",
state: "state",
insecure: false,
friend_sync: true,
two_way_link_code: "device-code",
openid_params: {
id_token: "id-token",
access_token: "access-token",
scope: "openid profile",
},
}),
true,
);

assert.equal(
validate({
state: "state",
}),
false,
);

assert.equal(
validate({
code: "oauth-code",
}),
false,
);

assert.equal(
validate({
code: "oauth-code",
state: "state",
unexpected: "field",
}),
false,
);

assert.equal(
validate({
code: "oauth-code",
state: "state",
openid_params: "id-token",
}),
false,
);

assert.equal(
validate({
code: "oauth-code",
state: "state",
openid_params: {
id_token: {
nested: true,
},
},
}),
false,
);
});
});
13 changes: 9 additions & 4 deletions src/schemas/uncategorised/ConnectionCallbackSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
*/

export interface ConnectionCallbackSchema {
code?: string;
code: string;
state: string;
insecure: boolean;
friend_sync: boolean;
openid_params?: unknown; // TODO: types
insecure?: boolean;
friend_sync?: boolean;
two_way_link_code?: string;
openid_params?: ConnectionCallbackOpenIdParams;
}

export interface ConnectionCallbackOpenIdParams {
[key: string]: string;
}
Loading