@jskit-ai/users-core 0.2.1 → 0.2.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/users-core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@jskit-ai/crud-core": "0.2.1",
13
13
  "@jskit-ai/resource-crud-core": "0.1.192",
14
- "@jskit-ai/workspaces-core": "0.2.1"
14
+ "@jskit-ai/workspaces-core": "0.2.2"
15
15
  },
16
16
  "jskit": {
17
17
  "kind": "runtime",
@@ -8,6 +8,7 @@ import {
8
8
  import {
9
9
  accountSettingsResponseFormatter
10
10
  } from "../common/formatters/accountSettingsResponseFormatter.js";
11
+ import { accountSecurityStatusFormatter } from "../common/formatters/accountSecurityStatusFormatter.js";
11
12
 
12
13
  function createService({
13
14
  userSettingsRepository,
@@ -99,9 +100,12 @@ function createService({
99
100
  });
100
101
  }
101
102
 
102
- return returnJsonApiData(await authService.unlinkProvider(request, {
103
+ const result = await authService.unlinkProvider(request, {
103
104
  provider: payload.provider
104
- }));
105
+ });
106
+ return returnJsonApiData({
107
+ securityStatus: accountSecurityStatusFormatter(result?.securityStatus)
108
+ });
105
109
  }
106
110
 
107
111
  async function logoutOtherSessions(request, _user, options = {}) {
@@ -132,6 +132,10 @@ function registerAccountSecurityRoutes(router, { authService } = {}) {
132
132
  }
133
133
  });
134
134
 
135
+ // As at sign-in, a provider that binds the attempt to this browser sets its cookie.
136
+ if (typeof authService?.writeOAuthStartCookies === "function") {
137
+ authService.writeOAuthStartCookies(reply, result);
138
+ }
135
139
  reply.redirect(result.url);
136
140
  }
137
141
  );
@@ -1,6 +1,9 @@
1
1
  import test from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import { buildSecurityStatusFromAuthMethodsStatus } from "@jskit-ai/auth-core/shared/authSecurityStatus";
4
+ import { validateOperationSection } from "@jskit-ai/http-runtime/shared/validators/operationValidation";
3
5
  import { createService } from "../src/server/accountSecurity/accountSecurityService.js";
6
+ import { userSettingsResource } from "../src/shared/resources/userSettingsResource.js";
4
7
 
5
8
  test("account security service returns no content result for other sessions", async () => {
6
9
  const calls = [];
@@ -30,3 +33,34 @@ test("account security service returns no content result for other sessions", as
30
33
  assert.equal(result, null);
31
34
  assert.deepEqual(calls, [request]);
32
35
  });
36
+
37
+ test("unlinking returns the security status in the settings output contract", async () => {
38
+ // What an auth provider returns: auth-core's status, without settings-only fields.
39
+ const securityStatus = buildSecurityStatusFromAuthMethodsStatus({
40
+ methods: [
41
+ { id: "password", kind: "password", provider: "email", label: "Password", configured: true, enabled: true },
42
+ { id: "oauth:github", kind: "oauth", provider: "github", label: "GitHub", configured: false, enabled: false, canEnable: true }
43
+ ]
44
+ });
45
+ const outputOf = (value) => validateOperationSection({
46
+ operation: userSettingsResource.operations.oauthUnlink,
47
+ section: "output",
48
+ value
49
+ });
50
+ assert.equal(outputOf({ securityStatus }).ok, false);
51
+
52
+ const service = createService({
53
+ userSettingsRepository: {},
54
+ userProfilesRepository: {},
55
+ authService: {
56
+ async unlinkProvider(_request, { provider }) {
57
+ assert.equal(provider, "github");
58
+ return { securityStatus };
59
+ }
60
+ }
61
+ });
62
+ const result = await service.unlinkOAuthProvider({}, { id: "user-1" }, { provider: "github" });
63
+ const output = outputOf(result.value);
64
+ assert.equal(output.ok, true, JSON.stringify(output.fieldErrors));
65
+ assert.equal(result.value.securityStatus.authMethods[1].canEnable, true);
66
+ });
@@ -204,6 +204,28 @@ test("account route handlers build action input from request.input", async () =>
204
204
  assert.equal(calls[7].actionId, "settings.security.sessions.logout_others");
205
205
  });
206
206
 
207
+ test("starting a provider link lets the auth service bind the attempt to the browser", async () => {
208
+ const written = [];
209
+ const routes = await registerRoutes({
210
+ authService: {
211
+ writeOAuthStartCookies(reply, result) {
212
+ written.push({ reply, result });
213
+ }
214
+ }
215
+ });
216
+ const reply = createReplyDouble();
217
+ const result = { url: "https://github.test/authorize", attempt: "browser-key" };
218
+ await findRoute(routes, { method: "GET", path: "/api/settings/security/oauth/:provider/start" }).handler(
219
+ createActionRequest({
220
+ input: { params: { provider: "github" }, query: {} },
221
+ executeAction: async () => result
222
+ }),
223
+ reply
224
+ );
225
+ assert.deepEqual(written, [{ reply, result }]);
226
+ assert.equal(reply.redirectedTo, "https://github.test/authorize");
227
+ });
228
+
207
229
  test("account route handlers preserve omitted optional action fields", async () => {
208
230
  const routes = await registerRoutes();
209
231
  const calls = [];