@remit/backend 0.0.103 → 0.0.104
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OAuth callback through the dev-server's own app — the process a
|
|
3
|
+
* self-host container runs.
|
|
4
|
+
*
|
|
5
|
+
* One hop further than the handler test: the app object imported from
|
|
6
|
+
* `server.ts`, so what is asserted is the header express actually writes onto
|
|
7
|
+
* the socket for Microsoft's returning browser. The handler answering 302 is
|
|
8
|
+
* not the same claim as the browser being sent anywhere, and the difference
|
|
9
|
+
* between the two was a blank page.
|
|
10
|
+
*
|
|
11
|
+
* The identity tier is left unmounted — this app serves the route either way,
|
|
12
|
+
* and the callback's exemption from the session gate is pinned where that gate
|
|
13
|
+
* runs, in `src/handlers/account-oauth-callback.test.ts`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import assert from "node:assert/strict";
|
|
17
|
+
import type { Server } from "node:http";
|
|
18
|
+
import { after, before, describe, it } from "node:test";
|
|
19
|
+
import {
|
|
20
|
+
_resetForTest,
|
|
21
|
+
type RemitClient,
|
|
22
|
+
setClient,
|
|
23
|
+
} from "../src/service/data-client.js";
|
|
24
|
+
|
|
25
|
+
const WEB_ORIGIN = "https://mail.example.com";
|
|
26
|
+
|
|
27
|
+
let server: Server;
|
|
28
|
+
let port: number;
|
|
29
|
+
|
|
30
|
+
before(async () => {
|
|
31
|
+
// Microsoft's redirect carries no session, so the callback answers before it
|
|
32
|
+
// reads anything: no store is touched on this path.
|
|
33
|
+
setClient({} as unknown as RemitClient);
|
|
34
|
+
|
|
35
|
+
process.env.BETTER_AUTH_SECRET = "a-signing-secret-of-at-least-32-characters";
|
|
36
|
+
process.env.CORS_ALLOWED_ORIGINS = WEB_ORIGIN;
|
|
37
|
+
process.env.SERVER_PORT = "0";
|
|
38
|
+
|
|
39
|
+
const imported = await import("./server.js");
|
|
40
|
+
server = imported.listener;
|
|
41
|
+
if (!server.listening) {
|
|
42
|
+
await new Promise<void>((resolve) => server.once("listening", resolve));
|
|
43
|
+
}
|
|
44
|
+
const address = server.address();
|
|
45
|
+
port = typeof address === "object" && address ? address.port : 0;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
after(async () => {
|
|
49
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
50
|
+
_resetForTest();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("GET /accounts/oauth/microsoft/callback through the dev-server's own app", () => {
|
|
54
|
+
it("answers the browser with somewhere to go", async () => {
|
|
55
|
+
const response = await fetch(
|
|
56
|
+
`http://127.0.0.1:${port}/accounts/oauth/microsoft/callback?error=access_denied`,
|
|
57
|
+
{ redirect: "manual" },
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
assert.equal(response.status, 302);
|
|
61
|
+
assert.equal(
|
|
62
|
+
response.headers.get("location"),
|
|
63
|
+
`${WEB_ORIGIN}/settings/accounts?oauthError=access_denied`,
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser Microsoft sends back, and where it ends up.
|
|
3
|
+
*
|
|
4
|
+
* The callback answers a redirect, so the whole response is its Location
|
|
5
|
+
* header: an answer that arrives without it is a blank page with the account
|
|
6
|
+
* either connected or not and no way to tell. Driven through the real OpenAPI
|
|
7
|
+
* document and the self-host request path, so what is asserted is what a
|
|
8
|
+
* self-hosted deployment writes onto the wire — the header included, which a
|
|
9
|
+
* handler returning a bare `{statusCode, headers}` shape lost on the way out.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import assert from "node:assert/strict";
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { createServer, type Server } from "node:http";
|
|
15
|
+
import type { AddressInfo } from "node:net";
|
|
16
|
+
import { after, before, describe, it } from "node:test";
|
|
17
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
|
|
18
|
+
import {
|
|
19
|
+
_resetForTest,
|
|
20
|
+
type RemitClient,
|
|
21
|
+
setClient,
|
|
22
|
+
} from "../service/data-client.js";
|
|
23
|
+
import { signState } from "./account-oauth.js";
|
|
24
|
+
import { createCalendarSqliteClient } from "./calendar-sqlite-fixture.js";
|
|
25
|
+
|
|
26
|
+
const CLIENT_SECRET = "callback-test-client-secret";
|
|
27
|
+
const WEB_ORIGIN = "https://mail.example.com";
|
|
28
|
+
const CALLBACK_PATH = "/accounts/oauth/microsoft/callback";
|
|
29
|
+
|
|
30
|
+
let handler: (
|
|
31
|
+
event: APIGatewayProxyEvent,
|
|
32
|
+
context: unknown,
|
|
33
|
+
) => Promise<APIGatewayProxyResult>;
|
|
34
|
+
let client: RemitClient;
|
|
35
|
+
let cleanup: () => void;
|
|
36
|
+
let tokenEndpoint: Server;
|
|
37
|
+
|
|
38
|
+
const jwt = (claims: Record<string, unknown>): string =>
|
|
39
|
+
[
|
|
40
|
+
Buffer.from(JSON.stringify({ alg: "RS256" })).toString("base64url"),
|
|
41
|
+
Buffer.from(JSON.stringify(claims), "utf8").toString("base64url"),
|
|
42
|
+
"signature-not-checked-here",
|
|
43
|
+
].join(".");
|
|
44
|
+
|
|
45
|
+
/** Microsoft's token endpoint, close enough for the one call the callback makes. */
|
|
46
|
+
const startTokenEndpoint = async (): Promise<string> => {
|
|
47
|
+
tokenEndpoint = createServer((_request, response) => {
|
|
48
|
+
response.writeHead(200, { "Content-Type": "application/json" });
|
|
49
|
+
response.end(
|
|
50
|
+
JSON.stringify({
|
|
51
|
+
access_token: "access-token",
|
|
52
|
+
refresh_token: "refresh-token",
|
|
53
|
+
id_token: jwt({ preferred_username: "matthijs@example.com" }),
|
|
54
|
+
expires_in: 3600,
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
await new Promise<void>((resolve) => tokenEndpoint.listen(0, resolve));
|
|
59
|
+
const { port } = tokenEndpoint.address() as AddressInfo;
|
|
60
|
+
return `http://127.0.0.1:${port}/token`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const callback = (
|
|
64
|
+
query: Record<string, string>,
|
|
65
|
+
): Promise<APIGatewayProxyResult> =>
|
|
66
|
+
handler(
|
|
67
|
+
{
|
|
68
|
+
httpMethod: "GET",
|
|
69
|
+
path: CALLBACK_PATH,
|
|
70
|
+
headers: {},
|
|
71
|
+
queryStringParameters: query,
|
|
72
|
+
body: null,
|
|
73
|
+
requestContext: {},
|
|
74
|
+
} as unknown as APIGatewayProxyEvent,
|
|
75
|
+
{ awsRequestId: `req-${randomUUID()}`, functionName: "test" },
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const headerOf = (
|
|
79
|
+
response: APIGatewayProxyResult,
|
|
80
|
+
name: string,
|
|
81
|
+
): string | undefined => {
|
|
82
|
+
for (const [key, value] of Object.entries(response.headers ?? {})) {
|
|
83
|
+
if (key.toLowerCase() === name) return String(value);
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
before(async () => {
|
|
89
|
+
_resetForTest();
|
|
90
|
+
process.env.DATA_BACKEND = "sqlite";
|
|
91
|
+
process.env.CORS_ALLOWED_ORIGINS = WEB_ORIGIN;
|
|
92
|
+
process.env.MSOAUTH_REDIRECT_URI = `${WEB_ORIGIN}${CALLBACK_PATH}`;
|
|
93
|
+
process.env.MSOAUTH_CLIENT_ID = "callback-test-client-id";
|
|
94
|
+
process.env.MSOAUTH_CLIENT_SECRET = CLIENT_SECRET;
|
|
95
|
+
process.env.MSOAUTH_TOKEN_ENDPOINT = await startTokenEndpoint();
|
|
96
|
+
// The self-host key that encrypts the stored refresh token.
|
|
97
|
+
process.env.FAKE_KMS_DATAKEY = "0".repeat(64);
|
|
98
|
+
// Static credentials so the best-effort sync enqueue fails against the local
|
|
99
|
+
// queue instead of walking the instance-metadata chain for a minute.
|
|
100
|
+
process.env.AWS_REGION = "eu-west-1";
|
|
101
|
+
process.env.AWS_ACCESS_KEY_ID = "test";
|
|
102
|
+
process.env.AWS_SECRET_ACCESS_KEY = "test";
|
|
103
|
+
({ client, cleanup } = await createCalendarSqliteClient());
|
|
104
|
+
setClient(client);
|
|
105
|
+
({ handler } = (await import("../index.js")) as unknown as {
|
|
106
|
+
handler: typeof handler;
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
after(async () => {
|
|
111
|
+
_resetForTest();
|
|
112
|
+
cleanup();
|
|
113
|
+
await new Promise<void>((resolve) => tokenEndpoint.close(() => resolve()));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("the Microsoft OAuth callback", () => {
|
|
117
|
+
it("sends the consenting browser on to the connected account", async () => {
|
|
118
|
+
const accountConfigId = randomUUID();
|
|
119
|
+
const state = await signState(
|
|
120
|
+
{ accountConfigId, nonce: "nonce", timestamp: Date.now() },
|
|
121
|
+
CLIENT_SECRET,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const response = await callback({ code: "auth-code", state });
|
|
125
|
+
|
|
126
|
+
assert.equal(response.statusCode, 302, response.body);
|
|
127
|
+
const location = headerOf(response, "location");
|
|
128
|
+
assert.ok(location, "a redirect without a Location header is a blank page");
|
|
129
|
+
const [connected] =
|
|
130
|
+
await client.account.listAllByAccountConfig(accountConfigId);
|
|
131
|
+
assert.ok(connected, "the callback stored the account it redirects to");
|
|
132
|
+
assert.equal(
|
|
133
|
+
location,
|
|
134
|
+
`${WEB_ORIGIN}/settings/accounts?connected=${encodeURIComponent(connected.accountId)}`,
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("sends a refused consent back with the reason Microsoft gave", async () => {
|
|
139
|
+
const response = await callback({ error: "access_denied" });
|
|
140
|
+
|
|
141
|
+
assert.equal(response.statusCode, 302, response.body);
|
|
142
|
+
assert.equal(
|
|
143
|
+
headerOf(response, "location"),
|
|
144
|
+
`${WEB_ORIGIN}/settings/accounts?oauthError=access_denied`,
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -16,6 +16,7 @@ import type { Context } from "openapi-backend";
|
|
|
16
16
|
import { getAccountConfigIdFromEvent } from "../auth.js";
|
|
17
17
|
import { getMsOAuthConfig } from "../config/msoauth.js";
|
|
18
18
|
import { safeJsonParse } from "../json.js";
|
|
19
|
+
import { rawApiResponse } from "../response.js";
|
|
19
20
|
import { getClient } from "../service/data-client.js";
|
|
20
21
|
import type { MicrosoftOAuthOperationIds, OperationHandler } from "../types.js";
|
|
21
22
|
import { triggerAccountSyncSafe } from "./account.js";
|
|
@@ -228,11 +229,15 @@ export const MicrosoftOAuthOperations: Record<
|
|
|
228
229
|
const webOrigin = getWebOrigin();
|
|
229
230
|
const qs = event.queryStringParameters ?? {};
|
|
230
231
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
232
|
+
// Marked raw so the response formatter forwards the Location header
|
|
233
|
+
// instead of serializing this object as a JSON body. Microsoft's browser
|
|
234
|
+
// redirect lands here, so a dropped Location is a blank page.
|
|
235
|
+
const redirect = (url: string): APIGatewayProxyResult =>
|
|
236
|
+
rawApiResponse({
|
|
237
|
+
statusCode: 302,
|
|
238
|
+
headers: { Location: url },
|
|
239
|
+
body: "",
|
|
240
|
+
});
|
|
236
241
|
|
|
237
242
|
// If Microsoft returned an error, pass it through to the frontend
|
|
238
243
|
if (qs.error) {
|
|
@@ -1,50 +1,22 @@
|
|
|
1
|
-
import { mkdtempSync,
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
import { applyShippedMigrations } from "@remit/drizzle-service/test-sqlite";
|
|
4
5
|
import Database from "better-sqlite3";
|
|
5
6
|
import { buildSqliteClient } from "../service/compose-sqlite.js";
|
|
6
7
|
import type { RemitClient } from "../service/data-client.js";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
* A
|
|
10
|
-
*
|
|
10
|
+
* A handler test's store: the backend's own SQLite composition, over the DDL a
|
|
11
|
+
* self-host deployment actually runs.
|
|
11
12
|
*
|
|
12
|
-
* The migrations are
|
|
13
|
-
* a handler test exercises the shipped column shapes
|
|
14
|
-
* `
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
const MIGRATIONS = new URL(
|
|
20
|
-
"../../../../deploy/vps/migrations-sqlite/entities/",
|
|
21
|
-
import.meta.url,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
interface Journal {
|
|
25
|
-
entries: Array<{ idx: number; tag: string }>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const applyEntityMigrations = (sqlite: Database.Database): void => {
|
|
29
|
-
const journal = JSON.parse(
|
|
30
|
-
readFileSync(new URL("meta/_journal.json", MIGRATIONS), "utf8"),
|
|
31
|
-
) as Journal;
|
|
32
|
-
const ordered = [...journal.entries].sort(
|
|
33
|
-
(left, right) => left.idx - right.idx,
|
|
34
|
-
);
|
|
35
|
-
for (const entry of ordered) {
|
|
36
|
-
const sql = readFileSync(new URL(`${entry.tag}.sql`, MIGRATIONS), "utf8");
|
|
37
|
-
for (const statement of sql.split("--> statement-breakpoint")) {
|
|
38
|
-
if (statement.trim() === "") continue;
|
|
39
|
-
sqlite.exec(statement);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A migrated database and the client that reads it. One per test file: every
|
|
46
|
-
* calendar row is scoped by account config, so a test that mints its own
|
|
47
|
-
* account config sees only what it wrote.
|
|
13
|
+
* The migrations are applied rather than pushed from the drizzle table objects,
|
|
14
|
+
* so a handler test exercises the shipped column shapes. The client is built by
|
|
15
|
+
* `compose-sqlite.ts`, the composition root a self-host process boots, so
|
|
16
|
+
* nothing here is a second wiring that could drift from the one that ships.
|
|
17
|
+
*
|
|
18
|
+
* One per test file: every row is scoped by account config, so a test that
|
|
19
|
+
* mints its own account config sees only what it wrote.
|
|
48
20
|
*/
|
|
49
21
|
export const createCalendarSqliteClient = async (): Promise<{
|
|
50
22
|
client: RemitClient;
|
|
@@ -53,7 +25,7 @@ export const createCalendarSqliteClient = async (): Promise<{
|
|
|
53
25
|
const directory = mkdtempSync(join(tmpdir(), "remit-calendar-handlers-"));
|
|
54
26
|
const path = join(directory, "remit.db");
|
|
55
27
|
const sqlite = new Database(path);
|
|
56
|
-
|
|
28
|
+
applyShippedMigrations(sqlite);
|
|
57
29
|
sqlite.close();
|
|
58
30
|
|
|
59
31
|
process.env.SQLITE_DB_PATH = path;
|
package/src/response.ts
CHANGED
|
@@ -10,13 +10,14 @@ import {
|
|
|
10
10
|
const RAW_API_RESPONSE: unique symbol = Symbol("remit.rawApiResponse");
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* A response
|
|
14
|
-
* joins
|
|
13
|
+
* A response the caller reads by something other than its JSON body — an
|
|
14
|
+
* iCalendar feed, an OAuth redirect's Location, and whatever later joins them.
|
|
15
15
|
*
|
|
16
16
|
* Every other handler returns a plain object and lets this module decide the
|
|
17
17
|
* status, the content type and the serialization. That is the right default and
|
|
18
|
-
* stays the default; a handler that has to own its own media type
|
|
19
|
-
* `rawApiResponse` rather than by returning a shape this one has
|
|
18
|
+
* stays the default; a handler that has to own its own media type or headers
|
|
19
|
+
* says so with `rawApiResponse` rather than by returning a shape this one has
|
|
20
|
+
* to guess at.
|
|
20
21
|
* The CORS and correlation headers are still added here, so a raw response is
|
|
21
22
|
* not a way around them.
|
|
22
23
|
*/
|