@softistx/janus 0.73.1 → 0.75.0

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.
Files changed (46) hide show
  1. package/README.md +22 -2
  2. package/dist/auth/config/index.d.ts +1 -1
  3. package/dist/auth/config/index.d.ts.map +1 -1
  4. package/dist/auth/config/resolve-social.d.ts.map +1 -1
  5. package/dist/auth/config/social-config.d.ts +31 -0
  6. package/dist/auth/config/social-config.d.ts.map +1 -1
  7. package/dist/auth/index.d.ts +2 -2
  8. package/dist/auth/index.d.ts.map +1 -1
  9. package/dist/auth/passkeys/sign-in.d.ts +5 -2
  10. package/dist/auth/passkeys/sign-in.d.ts.map +1 -1
  11. package/dist/auth/port/memory/social-links.d.ts.map +1 -1
  12. package/dist/auth/port/types/social-links.d.ts +8 -0
  13. package/dist/auth/port/types/social-links.d.ts.map +1 -1
  14. package/dist/auth/sessions/amr.d.ts +1 -1
  15. package/dist/auth/shared-api.d.ts +3 -0
  16. package/dist/auth/shared-api.d.ts.map +1 -1
  17. package/dist/auth/social/complete.d.ts.map +1 -1
  18. package/dist/auth/social/federated-amr.d.ts +15 -0
  19. package/dist/auth/social/federated-amr.d.ts.map +1 -0
  20. package/dist/auth/social/find-provider.d.ts.map +1 -1
  21. package/dist/auth/social/finish.d.ts +2 -0
  22. package/dist/auth/social/finish.d.ts.map +1 -1
  23. package/dist/auth/social/index.d.ts.map +1 -1
  24. package/dist/auth/social/unlink.d.ts +7 -0
  25. package/dist/auth/social/unlink.d.ts.map +1 -1
  26. package/dist/auth/types/index.d.ts +1 -1
  27. package/dist/auth/types/index.d.ts.map +1 -1
  28. package/dist/auth/types/passkeys.d.ts +16 -1
  29. package/dist/auth/types/passkeys.d.ts.map +1 -1
  30. package/dist/auth/types/social.d.ts +8 -0
  31. package/dist/auth/types/social.d.ts.map +1 -1
  32. package/dist/chunks/{index-k6y18nx9.js → index-7h34nqrm.js} +10 -2
  33. package/dist/chunks/{index-k6y18nx9.js.map → index-7h34nqrm.js.map} +3 -3
  34. package/dist/conformance/cases/social-links/reads.d.ts.map +1 -1
  35. package/dist/conformance/index.js +33 -2
  36. package/dist/conformance/index.js.map +3 -3
  37. package/dist/index.js +121 -23
  38. package/dist/index.js.map +13 -12
  39. package/docs/guide/adapters.md +6 -3
  40. package/docs/guide/errors.md +1 -1
  41. package/docs/guide/passkeys.md +16 -1
  42. package/docs/guide/sessions.md +1 -1
  43. package/docs/guide/social.md +50 -0
  44. package/docs/roadmap.md +3 -0
  45. package/docs/troubleshooting.md +28 -0
  46. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  createMemoryApiKeys2,
12
12
  createMemoryInvitations2,
13
13
  createMemoryStores2
14
- } from "./chunks/index-k6y18nx9.js";
14
+ } from "./chunks/index-7h34nqrm.js";
15
15
  import {
16
16
  JanusError2,
17
17
  UserInvalidError2,
@@ -833,6 +833,93 @@ function resolveSecondFactor(config, where) {
833
833
  };
834
834
  }
835
835
 
836
+ // src/auth/sessions/amr.ts
837
+ var AMR = {
838
+ password: ["pwd"],
839
+ oneTimeCode: ["otp"],
840
+ passkey: ["hwk", "user"],
841
+ appCode: ["otp", "mfa"],
842
+ recoveryCode: ["otp", "mfa"],
843
+ passkeySecondFactor: ["hwk", "user", "mfa"],
844
+ social: ["fed"]
845
+ };
846
+ function mergeAmr(a, b) {
847
+ return [...new Set([...a ?? [], ...b])];
848
+ }
849
+ function isMultiFactor(session) {
850
+ const { amr } = session;
851
+ return amr.includes("mfa") || amr.includes("hwk") && amr.includes("user");
852
+ }
853
+
854
+ // src/auth/social/federated-amr.ts
855
+ var GRANTABLE = new Set([
856
+ "pwd",
857
+ "otp",
858
+ "hwk",
859
+ "user",
860
+ "mfa",
861
+ "sms",
862
+ "swk",
863
+ "pin",
864
+ "fpt",
865
+ "face",
866
+ "iris",
867
+ "retina",
868
+ "mca",
869
+ "sc",
870
+ "tel",
871
+ "vbm",
872
+ "geo",
873
+ "kba",
874
+ "rba",
875
+ "wia"
876
+ ]);
877
+ var MAX_REPORTED_AMR = 16;
878
+ function checkFederatedAmr(mapping, at) {
879
+ if (mapping === undefined)
880
+ return;
881
+ const fail = (why) => new TypeError(`${at}: federatedAmr: ${why}`);
882
+ if (mapping === null || typeof mapping !== "object") {
883
+ throw fail("must be { acr?: Record, amr?: Record }");
884
+ }
885
+ for (const [name, table] of Object.entries(mapping)) {
886
+ if (name !== "acr" && name !== "amr") {
887
+ throw fail("an unknown key: acr and amr only");
888
+ }
889
+ if (table === null || typeof table !== "object" || Array.isArray(table)) {
890
+ throw fail(`${name} must map each upstream value to an array of methods`);
891
+ }
892
+ for (const methods of Object.values(table)) {
893
+ if (!Array.isArray(methods) || methods.length === 0 || methods.some((m) => typeof m !== "string" || !GRANTABLE.has(m))) {
894
+ throw fail(`${name}: every entry must be a non-empty array of RFC 8176 methods (${[...GRANTABLE].join(", ")})`);
895
+ }
896
+ }
897
+ }
898
+ }
899
+ function grantable(granted) {
900
+ return Array.isArray(granted) ? granted.filter((m) => typeof m === "string" && GRANTABLE.has(m)) : [];
901
+ }
902
+ var own = (table, key) => table !== null && typeof table === "object" && Object.hasOwn(table, key) ? table[key] : undefined;
903
+ function federatedMethods(mapping, identity) {
904
+ let methods = [...AMR.social];
905
+ if (mapping === undefined || mapping === null)
906
+ return methods;
907
+ const { acr, amr } = identity;
908
+ const byAcr = own(mapping, "acr");
909
+ const byAmr = own(mapping, "amr");
910
+ if (typeof acr === "string") {
911
+ methods = mergeAmr(methods, grantable(own(byAcr, acr)));
912
+ }
913
+ if (Array.isArray(amr)) {
914
+ for (const value of amr.slice(0, MAX_REPORTED_AMR)) {
915
+ if (typeof value === "string") {
916
+ methods = mergeAmr(methods, grantable(own(byAmr, value)));
917
+ }
918
+ }
919
+ }
920
+ return methods;
921
+ }
922
+
836
923
  // src/auth/config/resolve-social.ts
837
924
  var PROVIDER_NAME = /^[a-z][a-z0-9-]{0,31}$/;
838
925
  function resolveSocial(social, type, at) {
@@ -857,6 +944,7 @@ function resolveSocial(social, type, at) {
857
944
  if (!isSocialProvider(provider)) {
858
945
  throw new TypeError(`${at}: social.providers.${name} must be a provider — an object with authorizationUrl and exchange, such as google() from @softistx/janus-social`);
859
946
  }
947
+ checkFederatedAmr(provider.federatedAmr, `${at}: social.providers.${name}`);
860
948
  providers.set(name, provider);
861
949
  }
862
950
  const signUp = social.signUp ?? false;
@@ -2842,24 +2930,6 @@ function sharedApi(context) {
2842
2930
  types: [...config.types.keys()]
2843
2931
  };
2844
2932
  }
2845
- // src/auth/sessions/amr.ts
2846
- var AMR = {
2847
- password: ["pwd"],
2848
- oneTimeCode: ["otp"],
2849
- passkey: ["hwk", "user"],
2850
- appCode: ["otp", "mfa"],
2851
- recoveryCode: ["otp", "mfa"],
2852
- passkeySecondFactor: ["hwk", "user", "mfa"],
2853
- social: ["fed"]
2854
- };
2855
- function mergeAmr(a, b) {
2856
- return [...new Set([...a ?? [], ...b])];
2857
- }
2858
- function isMultiFactor(session) {
2859
- const { amr } = session;
2860
- return amr.includes("mfa") || amr.includes("hwk") && amr.includes("user");
2861
- }
2862
-
2863
2933
  // src/auth/sign-in-attempts.ts
2864
2934
  var PURPOSE3 = "janus/sign-in/attempts/v1";
2865
2935
  async function countLogin(context, type, login, where) {
@@ -3045,6 +3115,7 @@ async function signInOptions(context, where) {
3045
3115
  async function signInWithPasskey(context, type, given, where) {
3046
3116
  const { store } = requireFirstFactor(context, where);
3047
3117
  const device = deviceHint(context, given.options, where);
3118
+ const guard = guardOf(given.options, where);
3048
3119
  const challenge = given.challenge;
3049
3120
  if (typeof challenge !== "string" || challenge === "") {
3050
3121
  throw unknownOneTime(where, "challenge");
@@ -3070,7 +3141,10 @@ async function signInWithPasskey(context, type, given, where) {
3070
3141
  assertion: given.response,
3071
3142
  expectedChallenge: challenge,
3072
3143
  requireUserVerified: true,
3073
- beforeRecord: async () => refuseAfterPassword(type, record, where)
3144
+ beforeRecord: async () => {
3145
+ refuseAfterPassword(type, record, where);
3146
+ await guard?.(toUser(record));
3147
+ }
3074
3148
  }, where);
3075
3149
  if (!accepted)
3076
3150
  throw refuse();
@@ -3078,6 +3152,13 @@ async function signInWithPasskey(context, type, given, where) {
3078
3152
  await reportNewDevice(context, signedIn);
3079
3153
  return signedIn;
3080
3154
  }
3155
+ function guardOf(options, where) {
3156
+ const guard = options?.guard;
3157
+ if (guard !== undefined && typeof guard !== "function") {
3158
+ throw new TypeError(`${where}: options.guard must be a function`);
3159
+ }
3160
+ return guard;
3161
+ }
3081
3162
  function userHandleOf(response) {
3082
3163
  const handle = response?.response?.userHandle;
3083
3164
  return typeof handle === "string" && handle !== "" ? handle : null;
@@ -3265,6 +3346,10 @@ function assertSocialLinks(store, where) {
3265
3346
  throw new TypeError(`${where}: store.socialLinks has no method ${method}, which the port requires`);
3266
3347
  }
3267
3348
  }
3349
+ const counter = store.countSocialLinks;
3350
+ if (counter !== undefined && typeof counter !== "function") {
3351
+ throw new TypeError(`${where}: store.socialLinks.countSocialLinks must be a function or absent`);
3352
+ }
3268
3353
  }
3269
3354
  function assertPasskeys(store, where) {
3270
3355
  if (store === undefined)
@@ -5971,7 +6056,7 @@ function afterFlow(ending, provider) {
5971
6056
  };
5972
6057
  }
5973
6058
  async function finishSocial(ending, record, outcome) {
5974
- const { context, type, finish, device, returnTo, stepUp, where } = ending;
6059
+ const { context, type, finish, device, returnTo, stepUp, amr, where } = ending;
5975
6060
  if (!record.active) {
5976
6061
  throw new UserInactiveError2(`${where}: the user is inactive`, {
5977
6062
  userId: record.id,
@@ -5979,7 +6064,7 @@ async function finishSocial(ending, record, outcome) {
5979
6064
  ...afterFlow(ending)
5980
6065
  });
5981
6066
  }
5982
- const result = await finish(record, where, device, AMR.social);
6067
+ const result = await finish(record, where, device, amr);
5983
6068
  await reportNewDevice(context, result);
5984
6069
  return result.status === "signedIn" ? { ...result, ...outcome, returnTo, stepUp } : { ...result, provider: outcome.provider, returnTo, stepUp };
5985
6070
  }
@@ -6132,6 +6217,7 @@ async function findProvider(social, name, staticNames) {
6132
6217
  if (!isSocialProvider(resolved)) {
6133
6218
  throw new TypeError(`social.resolveProvider("${name}") must answer a provider — an object with authorizationUrl and exchange — or null`);
6134
6219
  }
6220
+ checkFederatedAmr(resolved.federatedAmr, `social.resolveProvider("${name}")`);
6135
6221
  return resolved;
6136
6222
  }
6137
6223
 
@@ -6547,6 +6633,7 @@ async function completeFlow(context, finishers, input, options) {
6547
6633
  device,
6548
6634
  returnTo: meta.returnTo,
6549
6635
  stepUp,
6636
+ amr: federatedMethods(configured.federatedAmr, identity),
6550
6637
  where: WHERE7
6551
6638
  };
6552
6639
  return redeemed.userId === null ? signInAccount(ending, meta.provider, identity) : completeLink(ending, redeemed.userId, meta.provider, identity);
@@ -6653,6 +6740,16 @@ async function unlinkAccount(context, user, link) {
6653
6740
  });
6654
6741
  return true;
6655
6742
  }
6743
+ async function countLinks(context, query) {
6744
+ const { provider } = query ?? {};
6745
+ if (typeof provider !== "string")
6746
+ return 0;
6747
+ const store = linkStore(context);
6748
+ if (store.countSocialLinks === undefined) {
6749
+ throw new UnsupportedError2("social.countLinks: store.socialLinks does not implement countSocialLinks — use a store that does, or implement the method", { slot: "socialLinks", operation: "countSocialLinks" });
6750
+ }
6751
+ return store.countSocialLinks(provider);
6752
+ }
6656
6753
  async function listLinks(context, user) {
6657
6754
  const id = refId(user);
6658
6755
  if (!isId2(id) || context.store.socialLinks === undefined)
@@ -6684,6 +6781,7 @@ function socialApi(context) {
6684
6781
  complete: (input, options) => completeFlow(context, finishers, input, options),
6685
6782
  unlink: (user, link) => unlinkAccount(context, user, link),
6686
6783
  list: (user) => listLinks(context, user),
6784
+ countLinks: (query) => countLinks(context, query),
6687
6785
  cookie: socialCookie(context)
6688
6786
  };
6689
6787
  }
@@ -6929,5 +7027,5 @@ export {
6929
7027
  systemClock
6930
7028
  };
6931
7029
 
6932
- //# debugId=72063D91C6C7511964756E2164756E21
7030
+ //# debugId=CB8060676CC7CD1C64756E2164756E21
6933
7031
  //# sourceMappingURL=index.js.map