@dfns/sdk-keysigner 0.3.4 → 0.4.0-alpha.1
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 +5 -13
- package/index.js +10 -78
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CredentialSigner, KeyAssertion,
|
|
1
|
+
import { CredentialSigner, KeyAssertion, AllowCredential } from '@dfns/sdk';
|
|
2
2
|
export declare class AsymmetricKeySigner implements CredentialSigner<KeyAssertion> {
|
|
3
3
|
private options;
|
|
4
4
|
constructor(options: {
|
|
@@ -8,16 +8,8 @@ export declare class AsymmetricKeySigner implements CredentialSigner<KeyAssertio
|
|
|
8
8
|
crossOrigin?: boolean;
|
|
9
9
|
algorithm?: string;
|
|
10
10
|
});
|
|
11
|
-
sign(challenge: string
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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>;
|
|
11
|
+
sign(challenge: string, allowCredentials: {
|
|
12
|
+
key: AllowCredential[];
|
|
13
|
+
webauthn: AllowCredential[];
|
|
14
|
+
}): Promise<KeyAssertion>;
|
|
23
15
|
}
|
package/index.js
CHANGED
|
@@ -23,14 +23,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.AsymmetricKeySigner = void 0;
|
|
27
27
|
const crypto = __importStar(require("crypto"));
|
|
28
28
|
const utils_1 = require("@dfns/sdk/utils");
|
|
29
29
|
class AsymmetricKeySigner {
|
|
30
30
|
constructor(options) {
|
|
31
31
|
this.options = options;
|
|
32
32
|
}
|
|
33
|
-
async sign(challenge) {
|
|
33
|
+
async sign(challenge, allowCredentials) {
|
|
34
|
+
const credId = this.options.credId;
|
|
35
|
+
if (credId === undefined || credId === '') {
|
|
36
|
+
throw new Error('credId is needed to sign');
|
|
37
|
+
}
|
|
38
|
+
const allowedCredId = allowCredentials.key.map(cred => cred.id);
|
|
39
|
+
if (!allowedCredId.includes(credId)) {
|
|
40
|
+
throw new Error(`CredId ${credId} does not exist for this account: ${allowedCredId}`);
|
|
41
|
+
}
|
|
34
42
|
const clientData = Buffer.from(JSON.stringify({
|
|
35
43
|
type: 'key.get',
|
|
36
44
|
challenge,
|
|
@@ -48,79 +56,3 @@ class AsymmetricKeySigner {
|
|
|
48
56
|
}
|
|
49
57
|
}
|
|
50
58
|
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-alpha.1",
|
|
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-alpha.1"
|
|
11
11
|
},
|
|
12
12
|
"main": "./index.js",
|
|
13
13
|
"type": "commonjs"
|