@adminforth/two-factors-auth 3.3.6 → 3.3.8

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 (3) hide show
  1. package/dist/index.js +15 -4
  2. package/index.ts +15 -4
  3. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -23,6 +23,17 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
23
23
  instanceUniqueRepresentation(pluginOptions) {
24
24
  return `single`;
25
25
  }
26
+ parseIfNeeded(value) {
27
+ if (typeof value === 'string') {
28
+ try {
29
+ return JSON.parse(value);
30
+ }
31
+ catch (e) {
32
+ throw new Error('Failed to parse JSON string: ' + e.message);
33
+ }
34
+ }
35
+ return value;
36
+ }
26
37
  useChellenge(challenge, expiresIn) {
27
38
  const expiresInSeconds = expiresIn ? convertPeriodToSeconds(expiresIn) : undefined;
28
39
  this.options.passkeys.keyValueAdapter.set(challenge, 'stub_value', expiresInSeconds);
@@ -279,7 +290,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
279
290
  if (!cred) {
280
291
  throw new Error('Credential not found.');
281
292
  }
282
- const credMeta = JSON.parse(cred[this.options.passkeys.credentialMetaFieldName]);
293
+ const credMeta = this.parseIfNeeded(cred[this.options.passkeys.credentialMetaFieldName]);
283
294
  if (!credMeta || !credMeta.public_key) {
284
295
  throw new Error('Credential public key not found.');
285
296
  }
@@ -766,7 +777,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
766
777
  const temp = yield this.adminforth.resource(this.options.passkeys.credentialResourceID).list([Filters.EQ(this.options.passkeys.credentialUserIdFieldName, adminUser.pk)]);
767
778
  for (const rec of temp) {
768
779
  if (rec.credential_id && rec.credential_id.length > 0) {
769
- const meta = JSON.parse(rec[this.options.passkeys.credentialMetaFieldName]);
780
+ const meta = this.parseIfNeeded(rec[this.options.passkeys.credentialMetaFieldName]);
770
781
  excludeCredentials.push({
771
782
  id: rec.credential_id,
772
783
  type: "public-key",
@@ -903,7 +914,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
903
914
  }
904
915
  let dataToReturn = [];
905
916
  for (const pk of passkeys) {
906
- const parsedKey = JSON.parse(pk[this.options.passkeys.credentialMetaFieldName]);
917
+ const parsedKey = this.parseIfNeeded(pk[this.options.passkeys.credentialMetaFieldName]);
907
918
  dataToReturn.push({
908
919
  name: parsedKey.name,
909
920
  light_icon: ((_b = aaguids[parsedKey.aaguid]) === null || _b === void 0 ? void 0 : _b.icon_light) || null,
@@ -982,7 +993,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
982
993
  if (!passkeyRecord) {
983
994
  return { ok: false, error: 'Passkey not found' };
984
995
  }
985
- const meta = JSON.parse(passkeyRecord[this.options.passkeys.credentialMetaFieldName]);
996
+ const meta = this.parseIfNeeded(passkeyRecord[this.options.passkeys.credentialMetaFieldName]);
986
997
  meta.name = newName;
987
998
  const newRecord = Object.assign(Object.assign({}, passkeyRecord), { [this.options.passkeys.credentialMetaFieldName]: JSON.stringify(meta) });
988
999
  try {
package/index.ts CHANGED
@@ -29,6 +29,17 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
29
29
  return `single`;
30
30
  }
31
31
 
32
+ private parseIfNeeded(value: Record<string, any> | string): Record<string, any> {
33
+ if (typeof value === 'string') {
34
+ try {
35
+ return JSON.parse(value);
36
+ } catch (e) {
37
+ throw new Error('Failed to parse JSON string: ' + e.message);
38
+ }
39
+ }
40
+ return value;
41
+ }
42
+
32
43
  private useChellenge(challenge: string, expiresIn?: string): void {
33
44
  const expiresInSeconds = expiresIn ? convertPeriodToSeconds(expiresIn) : undefined;
34
45
  this.options.passkeys.keyValueAdapter.set(challenge, 'stub_value', expiresInSeconds);
@@ -293,7 +304,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
293
304
  if (!cred) {
294
305
  throw new Error('Credential not found.');
295
306
  }
296
- const credMeta = JSON.parse(cred[this.options.passkeys.credentialMetaFieldName]);
307
+ const credMeta = this.parseIfNeeded(cred[this.options.passkeys.credentialMetaFieldName]);
297
308
  if (!credMeta || !credMeta.public_key) {
298
309
  throw new Error('Credential public key not found.');
299
310
  }
@@ -831,7 +842,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
831
842
  const temp = await this.adminforth.resource(this.options.passkeys.credentialResourceID).list([Filters.EQ(this.options.passkeys.credentialUserIdFieldName, adminUser.pk)]);
832
843
  for (const rec of temp) {
833
844
  if (rec.credential_id && rec.credential_id.length > 0) {
834
- const meta = JSON.parse(rec[this.options.passkeys.credentialMetaFieldName]);
845
+ const meta = this.parseIfNeeded(rec[this.options.passkeys.credentialMetaFieldName]);
835
846
  excludeCredentials.push({
836
847
  id: rec.credential_id,
837
848
  type: "public-key",
@@ -969,7 +980,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
969
980
  }
970
981
  let dataToReturn = [];
971
982
  for (const pk of passkeys) {
972
- const parsedKey = JSON.parse(pk[this.options.passkeys.credentialMetaFieldName]);
983
+ const parsedKey = this.parseIfNeeded(pk[this.options.passkeys.credentialMetaFieldName]);
973
984
  dataToReturn.push({
974
985
  name: parsedKey.name,
975
986
  light_icon: aaguids[parsedKey.aaguid]?.icon_light || null,
@@ -1049,7 +1060,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
1049
1060
  if (!passkeyRecord) {
1050
1061
  return { ok: false, error: 'Passkey not found' };
1051
1062
  }
1052
- const meta = JSON.parse(passkeyRecord[this.options.passkeys.credentialMetaFieldName]);
1063
+ const meta = this.parseIfNeeded(passkeyRecord[this.options.passkeys.credentialMetaFieldName]);
1053
1064
  meta.name = newName;
1054
1065
  const newRecord = { ...passkeyRecord, [this.options.passkeys.credentialMetaFieldName]: JSON.stringify(meta) };
1055
1066
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/two-factors-auth",
3
- "version": "3.3.6",
3
+ "version": "3.3.8",
4
4
  "description": "AdminForth plugin for two factors authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -28,11 +28,11 @@
28
28
  "node-2fa": "^2.0.3"
29
29
  },
30
30
  "peerDependencies": {
31
- "adminforth": "^2.50.1"
31
+ "adminforth": "^2.70.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^22.10.7",
35
- "adminforth": "^2.66.0",
35
+ "adminforth": "2.70.0",
36
36
  "semantic-release": "^24.2.1",
37
37
  "semantic-release-slack-bot": "^4.0.2",
38
38
  "typescript": "^5.7.3"