@flun/webauthn-browser 2.0.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/CHANGELOG.md +4 -0
- package/LICENSE.md +19 -0
- package/README.md +583 -0
- package/dist/index.es5.js +1 -0
- package/dist/index.js +270 -0
- package/helpers/base64urlAndBuffer.js +43 -0
- package/helpers/browserSupportsWebAuthn.js +19 -0
- package/helpers/browserSupportsWebAuthnAutofill.js +30 -0
- package/helpers/identifyAuthenticationError.js +50 -0
- package/helpers/identifyRegistrationError.js +99 -0
- package/helpers/index.d.ts +203 -0
- package/helpers/index.js +11 -0
- package/helpers/isValidDomain.js +18 -0
- package/helpers/platformAuthenticatorIsAvailable.js +14 -0
- package/helpers/toAuthenticatorAttachment.js +15 -0
- package/helpers/toPublicKeyCredentialDescriptor.js +23 -0
- package/helpers/webAuthnAbortService.js +51 -0
- package/helpers/webAuthnError.js +30 -0
- package/index.d.ts +42 -0
- package/index.js +5 -0
- package/methods/index.d.ts +40 -0
- package/methods/index.js +2 -0
- package/methods/startAuthentication.js +94 -0
- package/methods/startRegistration.js +123 -0
- package/package.json +52 -0
- package/types/dom.d.ts +302 -0
- package/types/index.d.ts +238 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bufferToBase64URLString, base64URLStringToBuffer, browserSupportsWebAuthn, identifyRegistrationError,
|
|
3
|
+
toAuthenticatorAttachment, toPublicKeyCredentialDescriptor, WebAuthnAbortService
|
|
4
|
+
} from '../helpers/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 通过 WebAuthn 证明开始认证器“注册”
|
|
8
|
+
* - 查看定义:@see {@link startRegistration}
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} options - 配置选项
|
|
11
|
+
* @param {PublicKeyCredentialCreationOptionsJSON} options.optionsJSON - 来自 **@flun/webauthn-server** 的 `generateRegistrationOptions()` 的输出
|
|
12
|
+
* @param {boolean} [options.useAutoRegister] - 尝试静默使用用户刚刚登录的密码管理器创建一个通行密钥,默认为 `false`
|
|
13
|
+
* @returns {Promise<{
|
|
14
|
+
* id: string,
|
|
15
|
+
* rawId: string,
|
|
16
|
+
* response: {
|
|
17
|
+
* attestationObject: string,
|
|
18
|
+
* clientDataJSON: string,
|
|
19
|
+
* transports?: AuthenticatorTransport[],
|
|
20
|
+
* publicKeyAlgorithm?: COSEAlgorithmIdentifier,
|
|
21
|
+
* publicKey?: string,
|
|
22
|
+
* authenticatorData?: string
|
|
23
|
+
* },
|
|
24
|
+
* type: PublicKeyCredentialType,
|
|
25
|
+
* clientExtensionResults: AuthenticationExtensionsClientOutputs,
|
|
26
|
+
* authenticatorAttachment: AuthenticatorAttachment | null
|
|
27
|
+
* }>}
|
|
28
|
+
*/
|
|
29
|
+
const startRegistration = async options => {
|
|
30
|
+
// 有意检查旧的调用结构,以警告不正确的 API 调用
|
|
31
|
+
if (!options.optionsJSON && options.challenge) {
|
|
32
|
+
console.warn('startRegistration() 的调用方式不正确;将继续尝试使用提供的选项,但应重构此调用以使用预期的调用结构;');
|
|
33
|
+
options = { optionsJSON: options }; // 将作为位置参数传入的 options 重新赋值给预期的变量
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!browserSupportsWebAuthn()) throw new Error('此浏览器不支持 WebAuthn');
|
|
37
|
+
const { optionsJSON, useAutoRegister = false } = options,
|
|
38
|
+
// 需要将部分值转换为 Uint8Array 后才能传递给 navigator 的 credentials
|
|
39
|
+
publicKey = {
|
|
40
|
+
...optionsJSON,
|
|
41
|
+
challenge: base64URLStringToBuffer(optionsJSON.challenge),
|
|
42
|
+
user: { ...optionsJSON.user, id: base64URLStringToBuffer(optionsJSON.user.id) },
|
|
43
|
+
excludeCredentials: optionsJSON.excludeCredentials?.map(toPublicKeyCredentialDescriptor)
|
|
44
|
+
}, createOptions = {}; // 准备传递给 `.create()` 的选项
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 尝试使用条件创建(conditional create)为用户注册一个通行密钥,
|
|
48
|
+
* 使用用户刚刚用于认证的密码管理器;浏览器不会向用户显示任何突出的 UI;
|
|
49
|
+
* 注意:`mediation` 在 CredentialCreationOptions 中尚不存在,但自 2024 年 9 月起已可用
|
|
50
|
+
*/
|
|
51
|
+
if (useAutoRegister) createOptions.mediation = 'conditional';
|
|
52
|
+
|
|
53
|
+
createOptions.publicKey = publicKey; // 最终确定选项
|
|
54
|
+
createOptions.signal = WebAuthnAbortService.createNewAbortSignal(); // 设置取消此请求的能力(如果用户尝试另一个请求)
|
|
55
|
+
|
|
56
|
+
// 等待用户完成证明
|
|
57
|
+
let credential;
|
|
58
|
+
try {
|
|
59
|
+
credential = await navigator.credentials.create(createOptions);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw identifyRegistrationError({ error: err, options: createOptions });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!credential) throw new Error('注册未完成');
|
|
65
|
+
|
|
66
|
+
const { id, rawId, response, type } = credential;
|
|
67
|
+
// 暂时继续安全地使用 `getTransports()`,即使 L3 类型声称它是必需的
|
|
68
|
+
let transports = void 0;
|
|
69
|
+
if (typeof response.getTransports === 'function') transports = response.getTransports();
|
|
70
|
+
|
|
71
|
+
// L3 声称这是必需的,但浏览器和 WebView 的支持仍不保证
|
|
72
|
+
let responsePublicKeyAlgorithm = void 0;
|
|
73
|
+
if (typeof response.getPublicKeyAlgorithm === 'function') {
|
|
74
|
+
try {
|
|
75
|
+
responsePublicKeyAlgorithm = response.getPublicKeyAlgorithm();
|
|
76
|
+
} catch (error) { warnOnBrokenImplementation('getPublicKeyAlgorithm()', error); }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let responsePublicKey = void 0;
|
|
80
|
+
if (typeof response.getPublicKey === 'function') {
|
|
81
|
+
try {
|
|
82
|
+
const _publicKey = response.getPublicKey();
|
|
83
|
+
if (_publicKey !== null) responsePublicKey = bufferToBase64URLString(_publicKey);
|
|
84
|
+
} catch (error) { warnOnBrokenImplementation('getPublicKey()', error); }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// L3 声称这是必需的,但浏览器和 WebView 的支持仍不保证
|
|
88
|
+
let responseAuthenticatorData;
|
|
89
|
+
if (typeof response.getAuthenticatorData === 'function') {
|
|
90
|
+
try {
|
|
91
|
+
responseAuthenticatorData = bufferToBase64URLString(response.getAuthenticatorData());
|
|
92
|
+
} catch (error) { warnOnBrokenImplementation('getAuthenticatorData()', error); }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
id,
|
|
97
|
+
rawId: bufferToBase64URLString(rawId),
|
|
98
|
+
response: {
|
|
99
|
+
attestationObject: bufferToBase64URLString(response.attestationObject),
|
|
100
|
+
clientDataJSON: bufferToBase64URLString(response.clientDataJSON),
|
|
101
|
+
transports,
|
|
102
|
+
publicKeyAlgorithm: responsePublicKeyAlgorithm,
|
|
103
|
+
publicKey: responsePublicKey,
|
|
104
|
+
authenticatorData: responseAuthenticatorData,
|
|
105
|
+
},
|
|
106
|
+
type,
|
|
107
|
+
clientExtensionResults: credential.getClientExtensionResults(),
|
|
108
|
+
authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 当检测到通行密钥提供方拦截 WebAuthn API 调用导致的问题时,发出可见警告
|
|
114
|
+
*
|
|
115
|
+
* @param {string} methodName - 被错误实现的 WebAuthn API 方法名称
|
|
116
|
+
* @param {unknown} cause - 捕获到的原始错误对象
|
|
117
|
+
* @returns {void}
|
|
118
|
+
*/
|
|
119
|
+
const warnOnBrokenImplementation = (methodName, cause) => {
|
|
120
|
+
console.warn(`拦截此 WebAuthn API 调用的浏览器扩展错误地实现了 ${methodName};请向该扩展的开发者报告此问题;\n`, cause);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { startRegistration };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flun/webauthn-browser",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"decjsion": "WebAuthn 前端库,提供简单易用的接口来处理 WebAuthn 认证流程,支持 Passkeys 和 FIDO2 标准;",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"webauthn",
|
|
8
|
+
"passkeys",
|
|
9
|
+
"fido",
|
|
10
|
+
"umd"
|
|
11
|
+
],
|
|
12
|
+
"main": "./index.js",
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./index.js"
|
|
16
|
+
},
|
|
17
|
+
"unpkg": "dist/index.js",
|
|
18
|
+
"author": "flun <cn@flun.top>",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"index.d.ts",
|
|
27
|
+
"dist/",
|
|
28
|
+
"helpers/",
|
|
29
|
+
"methods/",
|
|
30
|
+
"types/",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"README.md",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/flunGit/webauthn-browser.git"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://www.npmjs.com/package/@flun/webauthn-browser#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/flunGit/webauthn-browser/issues"
|
|
42
|
+
},
|
|
43
|
+
"license": "ISC",
|
|
44
|
+
"dependencies": {},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22.12.0",
|
|
47
|
+
"npm": ">=10.0.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^20.9.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/types/dom.d.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请勿修改这些文件!
|
|
3
|
+
*
|
|
4
|
+
* 这些文件是从 **types** 包复制而来的;要更新此文件,请修改相应包中的文件,
|
|
5
|
+
* 然后从 monorepo 根目录运行命令: deno task codegen:types
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 由 typescript@5.6.3 生成
|
|
9
|
+
* 要重新生成,请从包根目录运行命令: deno task extract-dom-types
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
13
|
+
*
|
|
14
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse)
|
|
15
|
+
*/
|
|
16
|
+
export interface AuthenticatorAssertionResponse extends AuthenticatorResponse {
|
|
17
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/authenticatorData) */
|
|
18
|
+
readonly authenticatorData: ArrayBuffer;
|
|
19
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/signature) */
|
|
20
|
+
readonly signature: ArrayBuffer;
|
|
21
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/userHandle) */
|
|
22
|
+
readonly userHandle: ArrayBuffer | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
26
|
+
*
|
|
27
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse)
|
|
28
|
+
*/
|
|
29
|
+
export interface AuthenticatorAttestationResponse extends AuthenticatorResponse {
|
|
30
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/attestationObject) */
|
|
31
|
+
readonly attestationObject: ArrayBuffer;
|
|
32
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/getAuthenticatorData) */
|
|
33
|
+
getAuthenticatorData(): ArrayBuffer;
|
|
34
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/getPublicKey) */
|
|
35
|
+
getPublicKey(): ArrayBuffer | null;
|
|
36
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/getPublicKeyAlgorithm) */
|
|
37
|
+
getPublicKeyAlgorithm(): COSEAlgorithmIdentifier;
|
|
38
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/getTransports) */
|
|
39
|
+
getTransports(): string[];
|
|
40
|
+
}
|
|
41
|
+
export interface AuthenticationExtensionsClientInputs {
|
|
42
|
+
appid?: string, credProps?: boolean, hmacCreateSecret?: boolean, minPinLength?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface AuthenticationExtensionsClientOutputs {
|
|
45
|
+
appid?: boolean, credProps?: CredentialPropertiesOutput, hmacCreateSecret?: boolean;
|
|
46
|
+
}
|
|
47
|
+
export interface AuthenticatorSelectionCriteria {
|
|
48
|
+
authenticatorAttachment?: AuthenticatorAttachment;
|
|
49
|
+
requireResidentKey?: boolean, residentKey?: ResidentKeyRequirement;
|
|
50
|
+
userVerification?: UserVerificationRequirement;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 当前上下文中可用的基本加密功能。它允许访问密码学强随机数生成器和加密原语。
|
|
54
|
+
*
|
|
55
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/Crypto)
|
|
56
|
+
*/
|
|
57
|
+
export interface Crypto {
|
|
58
|
+
/**
|
|
59
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
60
|
+
*
|
|
61
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/Crypto/subtle)
|
|
62
|
+
*/
|
|
63
|
+
readonly subtle: SubtleCrypto;
|
|
64
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) */
|
|
65
|
+
getRandomValues<T extends ArrayBufferView | null>(array: T): T;
|
|
66
|
+
/**
|
|
67
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
68
|
+
*
|
|
69
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID)
|
|
70
|
+
*/
|
|
71
|
+
randomUUID(): `${string}-${string}-${string}-${string}-${string}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
75
|
+
*
|
|
76
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential)
|
|
77
|
+
*/
|
|
78
|
+
export interface PublicKeyCredential extends Credential {
|
|
79
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/authenticatorAttachment) */
|
|
80
|
+
readonly authenticatorAttachment: string | null;
|
|
81
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/rawId) */
|
|
82
|
+
readonly rawId: ArrayBuffer;
|
|
83
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/response) */
|
|
84
|
+
readonly response: AuthenticatorResponse;
|
|
85
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/getClientExtensionResults) */
|
|
86
|
+
getClientExtensionResults(): AuthenticationExtensionsClientOutputs;
|
|
87
|
+
}
|
|
88
|
+
export interface PublicKeyCredentialCreationOptions {
|
|
89
|
+
attestation?: AttestationConveyancePreference;
|
|
90
|
+
authenticatorSelection?: AuthenticatorSelectionCriteria, challenge: BufferSource;
|
|
91
|
+
excludeCredentials?: PublicKeyCredentialDescriptor[];
|
|
92
|
+
extensions?: AuthenticationExtensionsClientInputs;
|
|
93
|
+
pubKeyCredParams: PublicKeyCredentialParameters[];
|
|
94
|
+
rp: PublicKeyCredentialRpEntity;
|
|
95
|
+
timeout?: number, user: PublicKeyCredentialUserEntity;
|
|
96
|
+
}
|
|
97
|
+
export interface PublicKeyCredentialDescriptor {
|
|
98
|
+
id: BufferSource, transports?: AuthenticatorTransport[], type: PublicKeyCredentialType;
|
|
99
|
+
}
|
|
100
|
+
export interface PublicKeyCredentialParameters {
|
|
101
|
+
alg: COSEAlgorithmIdentifier, type: PublicKeyCredentialType;
|
|
102
|
+
}
|
|
103
|
+
export interface PublicKeyCredentialRequestOptions {
|
|
104
|
+
allowCredentials?: PublicKeyCredentialDescriptor[];
|
|
105
|
+
challenge: BufferSource;
|
|
106
|
+
extensions?: AuthenticationExtensionsClientInputs;
|
|
107
|
+
rpId?: string, timeout?: number;
|
|
108
|
+
userVerification?: UserVerificationRequirement;
|
|
109
|
+
}
|
|
110
|
+
export interface PublicKeyCredentialUserEntity extends PublicKeyCredentialEntity {
|
|
111
|
+
displayName: string, id: BufferSource;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
115
|
+
*
|
|
116
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorResponse)
|
|
117
|
+
*/
|
|
118
|
+
export interface AuthenticatorResponse {
|
|
119
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/AuthenticatorResponse/clientDataJSON) */
|
|
120
|
+
readonly clientDataJSON: ArrayBuffer;
|
|
121
|
+
}
|
|
122
|
+
export interface CredentialPropertiesOutput {
|
|
123
|
+
rk?: boolean;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 此 Web Crypto API 接口提供了一系列低级加密函数;可通过窗口上下文中的 Crypto.subtle 属性访问(通过 Window.crypto);
|
|
127
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
128
|
+
*
|
|
129
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto)
|
|
130
|
+
*/
|
|
131
|
+
export interface SubtleCrypto {
|
|
132
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/decrypt) */
|
|
133
|
+
decrypt(algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey,
|
|
134
|
+
data: BufferSource): Promise<ArrayBuffer>;
|
|
135
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveBits) */
|
|
136
|
+
deriveBits(algorithm: AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey,
|
|
137
|
+
length: number): Promise<ArrayBuffer>;
|
|
138
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveKey) */
|
|
139
|
+
deriveKey(algorithm: AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey,
|
|
140
|
+
derivedKeyType: AlgorithmIdentifier | AesDerivedKeyParams | HmacImportParams | HkdfParams | Pbkdf2Params,
|
|
141
|
+
extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
142
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/digest) */
|
|
143
|
+
digest(algorithm: AlgorithmIdentifier, data: BufferSource): Promise<ArrayBuffer>;
|
|
144
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/encrypt) */
|
|
145
|
+
encrypt(algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey,
|
|
146
|
+
data: BufferSource): Promise<ArrayBuffer>;
|
|
147
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
|
|
148
|
+
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
|
|
149
|
+
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
|
|
150
|
+
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
|
|
151
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
|
|
152
|
+
generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">):
|
|
153
|
+
Promise<CryptoKeyPair>;
|
|
154
|
+
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean,
|
|
155
|
+
keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
|
|
156
|
+
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean,
|
|
157
|
+
keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
|
|
158
|
+
generateKey(algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: KeyUsage[]):
|
|
159
|
+
Promise<CryptoKeyPair | CryptoKey>;
|
|
160
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/importKey) */
|
|
161
|
+
importKey(format: "jwk", keyData: JsonWebKey, algorithm: AlgorithmIdentifier | RsaHashedImportParams |
|
|
162
|
+
EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>)
|
|
163
|
+
: Promise<CryptoKey>;
|
|
164
|
+
importKey(format: Exclude<KeyFormat, "jwk">, keyData: BufferSource, algorithm: AlgorithmIdentifier |
|
|
165
|
+
RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean,
|
|
166
|
+
keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
167
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/sign) */
|
|
168
|
+
sign(algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, data: BufferSource):
|
|
169
|
+
Promise<ArrayBuffer>;
|
|
170
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/unwrapKey) */
|
|
171
|
+
unwrapKey(format: KeyFormat, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: AlgorithmIdentifier
|
|
172
|
+
| RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, unwrappedKeyAlgorithm: AlgorithmIdentifier
|
|
173
|
+
| RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean,
|
|
174
|
+
keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
175
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/verify) */
|
|
176
|
+
verify(algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, signature: BufferSource,
|
|
177
|
+
data: BufferSource): Promise<boolean>;
|
|
178
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/wrapKey) */
|
|
179
|
+
wrapKey(format: KeyFormat, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: AlgorithmIdentifier |
|
|
180
|
+
RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams): Promise<ArrayBuffer>;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
184
|
+
*
|
|
185
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/Credential)
|
|
186
|
+
*/
|
|
187
|
+
export interface Credential {
|
|
188
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/Credential/id) */
|
|
189
|
+
readonly id: string;
|
|
190
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/Credential/type) */
|
|
191
|
+
readonly type: string;
|
|
192
|
+
}
|
|
193
|
+
export interface PublicKeyCredentialRpEntity extends PublicKeyCredentialEntity {
|
|
194
|
+
id?: string;
|
|
195
|
+
}
|
|
196
|
+
export interface PublicKeyCredentialEntity {
|
|
197
|
+
name: string;
|
|
198
|
+
}
|
|
199
|
+
export interface RsaOaepParams extends Algorithm {
|
|
200
|
+
label?: BufferSource;
|
|
201
|
+
}
|
|
202
|
+
export interface AesCtrParams extends Algorithm {
|
|
203
|
+
counter: BufferSource, length: number;
|
|
204
|
+
}
|
|
205
|
+
export interface AesCbcParams extends Algorithm {
|
|
206
|
+
iv: BufferSource;
|
|
207
|
+
}
|
|
208
|
+
export interface AesGcmParams extends Algorithm {
|
|
209
|
+
additionalData?: BufferSource, iv: BufferSource, tagLength?: number;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Web Crypto API 的 CryptoKey 字典表示一个加密密钥。
|
|
213
|
+
* 仅在安全上下文(secure contexts)中可用。
|
|
214
|
+
*
|
|
215
|
+
* [MDN 参考](https://developer.mozilla.org/docs/Web/API/CryptoKey)
|
|
216
|
+
*/
|
|
217
|
+
export interface CryptoKey {
|
|
218
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/CryptoKey/algorithm) */
|
|
219
|
+
readonly algorithm: KeyAlgorithm;
|
|
220
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/CryptoKey/extractable) */
|
|
221
|
+
readonly extractable: boolean;
|
|
222
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/CryptoKey/type) */
|
|
223
|
+
readonly type: KeyType;
|
|
224
|
+
/** [MDN 参考](https://developer.mozilla.org/docs/Web/API/CryptoKey/usages) */
|
|
225
|
+
readonly usages: KeyUsage[];
|
|
226
|
+
}
|
|
227
|
+
export interface EcdhKeyDeriveParams extends Algorithm {
|
|
228
|
+
public: CryptoKey;
|
|
229
|
+
}
|
|
230
|
+
export interface HkdfParams extends Algorithm {
|
|
231
|
+
hash: HashAlgorithmIdentifier, info: BufferSource, salt: BufferSource;
|
|
232
|
+
}
|
|
233
|
+
export interface Pbkdf2Params extends Algorithm {
|
|
234
|
+
hash: HashAlgorithmIdentifier, iterations: number, salt: BufferSource;
|
|
235
|
+
}
|
|
236
|
+
export interface AesDerivedKeyParams extends Algorithm {
|
|
237
|
+
length: number;
|
|
238
|
+
}
|
|
239
|
+
export interface HmacImportParams extends Algorithm {
|
|
240
|
+
hash: HashAlgorithmIdentifier, length?: number;
|
|
241
|
+
}
|
|
242
|
+
export interface JsonWebKey {
|
|
243
|
+
alg?: string, crv?: string, d?: string, dp?: string, dq?: string, e?: string, ext?: boolean, k?: string;
|
|
244
|
+
key_ops?: string[], kty?: string, n?: string, oth?: RsaOtherPrimesInfo[], p?: string, q?: string;
|
|
245
|
+
qi?: string, use?: string, x?: string, y?: string;
|
|
246
|
+
}
|
|
247
|
+
export interface CryptoKeyPair {
|
|
248
|
+
privateKey: CryptoKey, publicKey: CryptoKey;
|
|
249
|
+
}
|
|
250
|
+
export interface RsaHashedKeyGenParams extends RsaKeyGenParams {
|
|
251
|
+
hash: HashAlgorithmIdentifier;
|
|
252
|
+
}
|
|
253
|
+
export interface EcKeyGenParams extends Algorithm {
|
|
254
|
+
namedCurve: NamedCurve;
|
|
255
|
+
}
|
|
256
|
+
export interface AesKeyGenParams extends Algorithm {
|
|
257
|
+
length: number;
|
|
258
|
+
}
|
|
259
|
+
export interface HmacKeyGenParams extends Algorithm {
|
|
260
|
+
hash: HashAlgorithmIdentifier;
|
|
261
|
+
length?: number;
|
|
262
|
+
}
|
|
263
|
+
export interface RsaHashedImportParams extends Algorithm {
|
|
264
|
+
hash: HashAlgorithmIdentifier;
|
|
265
|
+
}
|
|
266
|
+
export interface EcKeyImportParams extends Algorithm {
|
|
267
|
+
namedCurve: NamedCurve;
|
|
268
|
+
}
|
|
269
|
+
export interface AesKeyAlgorithm extends KeyAlgorithm {
|
|
270
|
+
length: number;
|
|
271
|
+
}
|
|
272
|
+
export interface RsaPssParams extends Algorithm {
|
|
273
|
+
saltLength: number;
|
|
274
|
+
}
|
|
275
|
+
export interface EcdsaParams extends Algorithm {
|
|
276
|
+
hash: HashAlgorithmIdentifier;
|
|
277
|
+
}
|
|
278
|
+
export interface Algorithm {
|
|
279
|
+
name: string;
|
|
280
|
+
}
|
|
281
|
+
export interface KeyAlgorithm {
|
|
282
|
+
name: string;
|
|
283
|
+
}
|
|
284
|
+
export interface RsaOtherPrimesInfo { d?: string, r?: string, t?: string; }
|
|
285
|
+
export interface RsaKeyGenParams extends Algorithm {
|
|
286
|
+
modulusLength: number, publicExponent: BigInteger;
|
|
287
|
+
}
|
|
288
|
+
export type AttestationConveyancePreference = "direct" | "enterprise" | "indirect" | "none";
|
|
289
|
+
export type AuthenticatorTransport = "ble" | "hybrid" | "internal" | "nfc" | "usb";
|
|
290
|
+
export type COSEAlgorithmIdentifier = number;
|
|
291
|
+
export type ResidentKeyRequirement = "discouraged" | "preferred" | "required";
|
|
292
|
+
export type UserVerificationRequirement = "discouraged" | "preferred" | "required";
|
|
293
|
+
export type AuthenticatorAttachment = "cross-platform" | "platform";
|
|
294
|
+
export type BufferSource = ArrayBufferView | ArrayBuffer;
|
|
295
|
+
export type PublicKeyCredentialType = "public-key";
|
|
296
|
+
export type AlgorithmIdentifier = Algorithm | string;
|
|
297
|
+
export type KeyUsage = "decrypt" | "deriveBits" | "deriveKey" | "encrypt" | "sign" | "unwrapKey" | "verify" | "wrapKey";
|
|
298
|
+
export type KeyFormat = "jwk" | "pkcs8" | "raw" | "spki";
|
|
299
|
+
export type KeyType = "private" | "public" | "secret";
|
|
300
|
+
export type HashAlgorithmIdentifier = AlgorithmIdentifier;
|
|
301
|
+
export type NamedCurve = string;
|
|
302
|
+
export type BigInteger = Uint8Array;
|