@dfns/sdk-keysigner 0.3.4 → 0.4.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.
- package/index.d.ts +3 -16
- package/index.js +11 -83
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
import { CredentialSigner, KeyAssertion,
|
|
1
|
+
import { CredentialSigner, KeyAssertion, UserActionChallenge } from '@dfns/sdk';
|
|
2
2
|
export declare class AsymmetricKeySigner implements CredentialSigner<KeyAssertion> {
|
|
3
3
|
private options;
|
|
4
4
|
constructor(options: {
|
|
5
|
-
privateKey: string;
|
|
6
5
|
credId: string;
|
|
7
|
-
|
|
8
|
-
crossOrigin?: boolean;
|
|
6
|
+
privateKey: string;
|
|
9
7
|
algorithm?: string;
|
|
10
8
|
});
|
|
11
|
-
sign(challenge:
|
|
12
|
-
}
|
|
13
|
-
export declare class BrowserKeySigner implements CredentialSigner<KeyAssertion> {
|
|
14
|
-
private options;
|
|
15
|
-
constructor(options: {
|
|
16
|
-
keyPair: CryptoKeyPair;
|
|
17
|
-
credId?: string;
|
|
18
|
-
appOrigin: string;
|
|
19
|
-
crossOrigin?: boolean;
|
|
20
|
-
});
|
|
21
|
-
create(challenge: UserRegistrationChallenge): Promise<KeyAttestation>;
|
|
22
|
-
sign(challenge: string): Promise<KeyAssertion>;
|
|
9
|
+
sign(challenge: UserActionChallenge): Promise<KeyAssertion>;
|
|
23
10
|
}
|
package/index.js
CHANGED
|
@@ -23,104 +23,32 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const crypto = __importStar(require("crypto"));
|
|
26
|
+
exports.AsymmetricKeySigner = void 0;
|
|
27
|
+
const crypto = __importStar(require("node:crypto"));
|
|
28
|
+
const sdk_1 = require("@dfns/sdk");
|
|
28
29
|
const utils_1 = require("@dfns/sdk/utils");
|
|
29
30
|
class AsymmetricKeySigner {
|
|
30
31
|
constructor(options) {
|
|
31
32
|
this.options = options;
|
|
32
33
|
}
|
|
33
34
|
async sign(challenge) {
|
|
35
|
+
const { credId, privateKey, algorithm } = this.options;
|
|
36
|
+
const allowedCredId = challenge.allowCredentials.key.map((cred) => cred.id);
|
|
37
|
+
if (!allowedCredId.includes(credId)) {
|
|
38
|
+
throw new sdk_1.DfnsError(-1, `${credId} does not match allowed credentials: ${allowedCredId}`);
|
|
39
|
+
}
|
|
34
40
|
const clientData = Buffer.from(JSON.stringify({
|
|
35
41
|
type: 'key.get',
|
|
36
|
-
challenge,
|
|
37
|
-
origin: this.options.appOrigin,
|
|
38
|
-
crossOrigin: this.options.crossOrigin ?? false,
|
|
42
|
+
challenge: challenge.challenge,
|
|
39
43
|
}));
|
|
40
44
|
return {
|
|
41
45
|
kind: 'Key',
|
|
42
46
|
credentialAssertion: {
|
|
43
|
-
credId
|
|
47
|
+
credId,
|
|
44
48
|
clientData: (0, utils_1.toBase64Url)(clientData),
|
|
45
|
-
signature: (0, utils_1.toBase64Url)(crypto.sign(
|
|
49
|
+
signature: (0, utils_1.toBase64Url)(crypto.sign(algorithm || undefined, clientData, privateKey)),
|
|
46
50
|
},
|
|
47
51
|
};
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
exports.AsymmetricKeySigner = AsymmetricKeySigner;
|
|
51
|
-
class BrowserKeySigner {
|
|
52
|
-
constructor(options) {
|
|
53
|
-
this.options = options;
|
|
54
|
-
}
|
|
55
|
-
async create(challenge) {
|
|
56
|
-
let credId = this.options.credId;
|
|
57
|
-
if (credId === undefined || credId === '') {
|
|
58
|
-
credId = (0, utils_1.toBase64Url)(Buffer.from((0, utils_1.generateRandom)(32)));
|
|
59
|
-
this.options.credId = credId;
|
|
60
|
-
}
|
|
61
|
-
const publicKeyPem = await (0, utils_1.exportPublicKeyInPemFormatBrowser)(this.options.keyPair);
|
|
62
|
-
const clientData = JSON.stringify({
|
|
63
|
-
type: 'key.create',
|
|
64
|
-
challenge: challenge.challenge,
|
|
65
|
-
origin: this.options.appOrigin,
|
|
66
|
-
crossOrigin: this.options.crossOrigin ?? false,
|
|
67
|
-
});
|
|
68
|
-
const clientDataHash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(clientData));
|
|
69
|
-
const clientDataHashHex = (0, utils_1.toHex)(clientDataHash);
|
|
70
|
-
const credInfoFingerprint = JSON.stringify({
|
|
71
|
-
clientDataHash: clientDataHashHex,
|
|
72
|
-
publicKey: publicKeyPem,
|
|
73
|
-
});
|
|
74
|
-
let rawSignature;
|
|
75
|
-
const algorithm = this.options.keyPair.privateKey.algorithm.name;
|
|
76
|
-
if (algorithm == 'ECDSA') {
|
|
77
|
-
rawSignature = await crypto.subtle.sign({ name: 'ECDSA', hash: { name: 'SHA-256' } }, this.options.keyPair.privateKey, new TextEncoder().encode(credInfoFingerprint));
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
throw new Error(`${algorithm} is not supported`);
|
|
81
|
-
}
|
|
82
|
-
const signature = (0, utils_1.rawSignatureToAns1)(new Uint8Array(rawSignature));
|
|
83
|
-
const attestationData = JSON.stringify({
|
|
84
|
-
publicKey: publicKeyPem,
|
|
85
|
-
signature: (0, utils_1.toHex)(signature)
|
|
86
|
-
});
|
|
87
|
-
return {
|
|
88
|
-
credentialKind: 'Key',
|
|
89
|
-
credentialInfo: {
|
|
90
|
-
credId: credId,
|
|
91
|
-
clientData: (0, utils_1.toBase64Url)(clientData),
|
|
92
|
-
attestationData: (0, utils_1.toBase64Url)(attestationData),
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
async sign(challenge) {
|
|
97
|
-
const credId = this.options.credId;
|
|
98
|
-
if (credId === undefined || credId === '') {
|
|
99
|
-
throw new Error('credId is needed to sign');
|
|
100
|
-
}
|
|
101
|
-
const clientData = JSON.stringify({
|
|
102
|
-
type: 'key.get',
|
|
103
|
-
challenge,
|
|
104
|
-
origin: this.options.appOrigin,
|
|
105
|
-
crossOrigin: this.options.crossOrigin ?? false,
|
|
106
|
-
});
|
|
107
|
-
let rawSignature;
|
|
108
|
-
const algorithm = this.options.keyPair.privateKey.algorithm.name;
|
|
109
|
-
if (algorithm == 'ECDSA') {
|
|
110
|
-
rawSignature = await crypto.subtle.sign({ name: 'ECDSA', hash: { name: 'SHA-256' } }, this.options.keyPair.privateKey, new TextEncoder().encode(clientData));
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
throw new Error(`${algorithm} is not supported`);
|
|
114
|
-
}
|
|
115
|
-
const signature = (0, utils_1.rawSignatureToAns1)(new Uint8Array(rawSignature));
|
|
116
|
-
return {
|
|
117
|
-
kind: 'Key',
|
|
118
|
-
credentialAssertion: {
|
|
119
|
-
credId: this.options.credId ?? '',
|
|
120
|
-
clientData: (0, utils_1.toBase64Url)(clientData),
|
|
121
|
-
signature: (0, utils_1.toBase64Url)(Buffer.from(signature)),
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
exports.BrowserKeySigner = BrowserKeySigner;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dfns/sdk-keysigner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"buffer": "6.0.3",
|
|
6
6
|
"cross-fetch": "3.1.6",
|
|
7
7
|
"uuid": "9.0.0"
|
|
8
8
|
},
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"@dfns/sdk": "0.
|
|
10
|
+
"@dfns/sdk": "0.4.0"
|
|
11
11
|
},
|
|
12
12
|
"main": "./index.js",
|
|
13
13
|
"type": "commonjs"
|