2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
|
|
|
import * as crypto from 'node:crypto';
|
2023-06-04 14:03:46 +09:00
|
|
|
import cbor from 'cbor';
|
2023-03-11 14:13:39 +09:00
|
|
|
import * as OTPAuth from 'otpauth';
|
2023-09-08 14:05:03 +09:00
|
|
|
import { loadConfig } from '@/config.js';
|
2024-01-08 17:43:52 +09:00
|
|
|
import { api, signup } from '../utils.js';
|
2023-09-08 14:05:03 +09:00
|
|
|
import type {
|
|
|
|
AuthenticationResponseJSON,
|
|
|
|
AuthenticatorAssertionResponseJSON,
|
|
|
|
AuthenticatorAttestationResponseJSON,
|
|
|
|
PublicKeyCredentialCreationOptionsJSON,
|
|
|
|
PublicKeyCredentialRequestOptionsJSON,
|
|
|
|
RegistrationResponseJSON,
|
2024-02-16 20:09:07 +09:00
|
|
|
} from '@simplewebauthn/types';
|
2023-06-25 08:34:18 +09:00
|
|
|
import type * as misskey from 'misskey-js';
|
2023-03-11 14:13:39 +09:00
|
|
|
|
|
|
|
describe('2要素認証', () => {
|
2024-01-03 13:41:28 +09:00
|
|
|
let alice: misskey.entities.SignupResponse;
|
2023-03-11 14:13:39 +09:00
|
|
|
|
|
|
|
const config = loadConfig();
|
|
|
|
const password = 'test';
|
|
|
|
const username = 'alice';
|
|
|
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc8152
|
|
|
|
// 各値の定義は上記規格に基づく。鍵ペアは適当に生成したやつ
|
|
|
|
const coseKtyEc2 = 2;
|
|
|
|
const coseKid = 'meriadoc.brandybuck@buckland.example';
|
|
|
|
const coseAlgEs256 = -7;
|
|
|
|
const coseEc2CrvP256 = 1;
|
|
|
|
const coseEc2X = '4932eaacc657565705e4287e7870ce3aad55545d99d35a98a472dc52880cfc8f';
|
|
|
|
const coseEc2Y = '5ca68303bf2c0433473e3d5cb8586bc2c8c43a4945a496fce8dbeda8b23ab0b1';
|
|
|
|
|
|
|
|
// private key only for testing
|
|
|
|
const pemToSign = '-----BEGIN EC PRIVATE KEY-----\n' +
|
|
|
|
'MHcCAQEEIHqe/keuXyolbXzgLOu+YFJjDBGWVgXc3QCXfyqwDPf2oAoGCCqGSM49\n' +
|
|
|
|
'AwEHoUQDQgAESTLqrMZXVlcF5Ch+eHDOOq1VVF2Z01qYpHLcUogM/I9cpoMDvywE\n' +
|
|
|
|
'M0c+PVy4WGvCyMQ6SUWklvzo2+2osjqwsQ==\n' +
|
|
|
|
'-----END EC PRIVATE KEY-----\n';
|
|
|
|
|
|
|
|
const otpToken = (secret: string): string => {
|
|
|
|
return OTPAuth.TOTP.generate({
|
|
|
|
secret: OTPAuth.Secret.fromBase32(secret),
|
|
|
|
digits: 6,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const rpIdHash = (): Buffer => {
|
|
|
|
return crypto.createHash('sha256')
|
2023-09-08 14:05:03 +09:00
|
|
|
.update(Buffer.from(config.host, 'utf-8'))
|
2023-03-11 14:13:39 +09:00
|
|
|
.digest();
|
|
|
|
};
|
|
|
|
|
|
|
|
const keyDoneParam = (param: {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: string,
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName: string,
|
|
|
|
credentialId: Buffer,
|
2023-09-08 14:05:03 +09:00
|
|
|
creationOptions: PublicKeyCredentialCreationOptionsJSON,
|
2023-03-11 14:13:39 +09:00
|
|
|
}): {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: string,
|
2023-03-11 14:13:39 +09:00
|
|
|
password: string,
|
|
|
|
name: string,
|
2023-09-08 14:05:03 +09:00
|
|
|
credential: RegistrationResponseJSON,
|
2023-03-11 14:13:39 +09:00
|
|
|
} => {
|
|
|
|
// A COSE encoded public key
|
|
|
|
const credentialPublicKey = cbor.encode(new Map<number, unknown>([
|
|
|
|
[-1, coseEc2CrvP256],
|
|
|
|
[-2, Buffer.from(coseEc2X, 'hex')],
|
|
|
|
[-3, Buffer.from(coseEc2Y, 'hex')],
|
|
|
|
[1, coseKtyEc2],
|
|
|
|
[2, coseKid],
|
|
|
|
[3, coseAlgEs256],
|
|
|
|
]));
|
|
|
|
|
|
|
|
// AuthenticatorAssertionResponse.authenticatorData
|
2023-06-25 08:34:18 +09:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/AuthenticatorAssertionResponse/authenticatorData
|
2023-03-11 14:13:39 +09:00
|
|
|
const credentialIdLength = Buffer.allocUnsafe(2);
|
2023-09-08 14:05:03 +09:00
|
|
|
credentialIdLength.writeUInt16BE(param.credentialId.length, 0);
|
2023-03-11 14:13:39 +09:00
|
|
|
const authData = Buffer.concat([
|
|
|
|
rpIdHash(), // rpIdHash(32)
|
|
|
|
Buffer.from([0x45]), // flags(1)
|
|
|
|
Buffer.from([0x00, 0x00, 0x00, 0x00]), // signCount(4)
|
|
|
|
Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), // AAGUID(16)
|
|
|
|
credentialIdLength,
|
|
|
|
param.credentialId,
|
|
|
|
credentialPublicKey,
|
|
|
|
]);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
return {
|
|
|
|
password,
|
2023-09-22 14:12:33 +09:00
|
|
|
token: param.token,
|
2023-03-11 14:13:39 +09:00
|
|
|
name: param.keyName,
|
2023-09-08 14:05:03 +09:00
|
|
|
credential: <RegistrationResponseJSON>{
|
|
|
|
id: param.credentialId.toString('base64url'),
|
|
|
|
rawId: param.credentialId.toString('base64url'),
|
|
|
|
response: <AuthenticatorAttestationResponseJSON>{
|
|
|
|
clientDataJSON: Buffer.from(JSON.stringify({
|
|
|
|
type: 'webauthn.create',
|
|
|
|
challenge: param.creationOptions.challenge,
|
|
|
|
origin: config.scheme + '://' + config.host,
|
|
|
|
androidPackageName: 'org.mozilla.firefox',
|
|
|
|
}), 'utf-8').toString('base64url'),
|
|
|
|
attestationObject: cbor.encode({
|
|
|
|
fmt: 'none',
|
|
|
|
attStmt: {},
|
|
|
|
authData,
|
|
|
|
}).toString('base64url'),
|
|
|
|
},
|
|
|
|
clientExtensionResults: {},
|
|
|
|
type: 'public-key',
|
|
|
|
},
|
2023-03-11 14:13:39 +09:00
|
|
|
};
|
|
|
|
};
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
const signinParam = (): {
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
'g-recaptcha-response'?: string | null,
|
|
|
|
'hcaptcha-response'?: string | null,
|
|
|
|
} => {
|
|
|
|
return {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
'g-recaptcha-response': null,
|
|
|
|
'hcaptcha-response': null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const signinWithSecurityKeyParam = (param: {
|
|
|
|
keyName: string,
|
|
|
|
credentialId: Buffer,
|
2023-09-08 14:05:03 +09:00
|
|
|
requestOptions: PublicKeyCredentialRequestOptionsJSON,
|
2024-10-04 15:23:33 +09:00
|
|
|
}): misskey.entities.SigninRequest => {
|
2023-03-11 14:13:39 +09:00
|
|
|
// AuthenticatorAssertionResponse.authenticatorData
|
2023-06-25 08:34:18 +09:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/AuthenticatorAssertionResponse/authenticatorData
|
2023-03-11 14:13:39 +09:00
|
|
|
const authenticatorData = Buffer.concat([
|
|
|
|
rpIdHash(),
|
|
|
|
Buffer.from([0x05]), // flags(1)
|
|
|
|
Buffer.from([0x00, 0x00, 0x00, 0x01]), // signCount(4)
|
|
|
|
]);
|
|
|
|
const clientDataJSONBuffer = Buffer.from(JSON.stringify({
|
|
|
|
type: 'webauthn.get',
|
2023-09-08 14:05:03 +09:00
|
|
|
challenge: param.requestOptions.challenge,
|
2023-03-11 14:13:39 +09:00
|
|
|
origin: config.scheme + '://' + config.host,
|
|
|
|
androidPackageName: 'org.mozilla.firefox',
|
2023-09-08 14:05:03 +09:00
|
|
|
}), 'utf-8');
|
2023-03-11 14:13:39 +09:00
|
|
|
const hashedclientDataJSON = crypto.createHash('sha256')
|
|
|
|
.update(clientDataJSONBuffer)
|
|
|
|
.digest();
|
|
|
|
const privateKey = crypto.createPrivateKey(pemToSign);
|
2023-06-25 08:34:18 +09:00
|
|
|
const signature = crypto.createSign('SHA256')
|
2023-03-11 14:13:39 +09:00
|
|
|
.update(Buffer.concat([authenticatorData, hashedclientDataJSON]))
|
|
|
|
.sign(privateKey);
|
|
|
|
return {
|
|
|
|
username,
|
|
|
|
password,
|
2023-09-08 14:05:03 +09:00
|
|
|
credential: <AuthenticationResponseJSON>{
|
|
|
|
id: param.credentialId.toString('base64url'),
|
|
|
|
rawId: param.credentialId.toString('base64url'),
|
|
|
|
response: <AuthenticatorAssertionResponseJSON>{
|
|
|
|
clientDataJSON: clientDataJSONBuffer.toString('base64url'),
|
|
|
|
authenticatorData: authenticatorData.toString('base64url'),
|
|
|
|
signature: signature.toString('base64url'),
|
|
|
|
},
|
|
|
|
clientExtensionResults: {},
|
|
|
|
type: 'public-key',
|
|
|
|
},
|
2023-03-11 14:13:39 +09:00
|
|
|
'g-recaptcha-response': null,
|
|
|
|
'hcaptcha-response': null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
alice = await signup({ username, password });
|
|
|
|
}, 1000 * 60 * 2);
|
|
|
|
|
|
|
|
test('が設定でき、OTPでログインできる。', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
assert.notEqual(registerResponse.body.qr, undefined);
|
|
|
|
assert.notEqual(registerResponse.body.url, undefined);
|
|
|
|
assert.notEqual(registerResponse.body.secret, undefined);
|
|
|
|
assert.strictEqual(registerResponse.body.label, username);
|
|
|
|
assert.strictEqual(registerResponse.body.issuer, config.host);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-10-04 15:23:33 +09:00
|
|
|
const signinWithoutTokenResponse = await api('signin', {
|
|
|
|
...signinParam(),
|
|
|
|
});
|
|
|
|
assert.strictEqual(signinWithoutTokenResponse.status, 403);
|
|
|
|
assert.deepStrictEqual(signinWithoutTokenResponse.body, {
|
|
|
|
error: {
|
|
|
|
id: '144ff4f8-bd6c-41bc-82c3-b672eb09efbf',
|
|
|
|
next: 'totp',
|
|
|
|
},
|
|
|
|
});
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinParam(),
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
});
|
|
|
|
assert.strictEqual(signinResponse.status, 200);
|
|
|
|
assert.notEqual(signinResponse.body.i, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test('が設定でき、セキュリティキーでログインできる。', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerKeyResponse = await api('i/2fa/register-key', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerKeyResponse.status, 200);
|
2023-09-08 14:05:03 +09:00
|
|
|
assert.notEqual(registerKeyResponse.body.rp, undefined);
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.notEqual(registerKeyResponse.body.challenge, undefined);
|
|
|
|
|
|
|
|
const keyName = 'example-key';
|
|
|
|
const credentialId = crypto.randomBytes(0x41);
|
2024-03-03 20:15:35 +09:00
|
|
|
const keyDoneResponse = await api('i/2fa/key-done', keyDoneParam({
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName,
|
|
|
|
credentialId,
|
2023-09-08 14:05:03 +09:00
|
|
|
creationOptions: registerKeyResponse.body,
|
2024-07-14 09:33:16 +09:00
|
|
|
} as any) as any, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.status, 200);
|
2023-09-08 14:05:03 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.body.id, credentialId.toString('base64url'));
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.body.name, keyName);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinParam(),
|
|
|
|
});
|
2024-10-04 15:23:33 +09:00
|
|
|
const signinResponseBody = signinResponse.body as unknown as {
|
|
|
|
error: {
|
|
|
|
id: string;
|
|
|
|
next: 'passkey';
|
|
|
|
authRequest: PublicKeyCredentialRequestOptionsJSON;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert.strictEqual(signinResponse.status, 403);
|
|
|
|
assert.strictEqual(signinResponseBody.error.id, '06e661b9-8146-4ae3-bde5-47138c0ae0c4');
|
|
|
|
assert.strictEqual(signinResponseBody.error.next, 'passkey');
|
|
|
|
assert.notEqual(signinResponseBody.error.authRequest.challenge, undefined);
|
|
|
|
assert.notEqual(signinResponseBody.error.authRequest.allowCredentials, undefined);
|
|
|
|
assert.strictEqual(signinResponseBody.error.authRequest.allowCredentials && signinResponseBody.error.authRequest.allowCredentials[0]?.id, credentialId.toString('base64url'));
|
2023-03-11 14:13:39 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse2 = await api('signin', signinWithSecurityKeyParam({
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName,
|
|
|
|
credentialId,
|
2024-10-04 15:23:33 +09:00
|
|
|
requestOptions: signinResponseBody.error.authRequest,
|
|
|
|
}));
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(signinResponse2.status, 200);
|
|
|
|
assert.notEqual(signinResponse2.body.i, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test('が設定でき、セキュリティキーでパスワードレスログインできる。', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerKeyResponse = await api('i/2fa/register-key', {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerKeyResponse.status, 200);
|
|
|
|
|
|
|
|
const keyName = 'example-key';
|
|
|
|
const credentialId = crypto.randomBytes(0x41);
|
2024-03-03 20:15:35 +09:00
|
|
|
const keyDoneResponse = await api('i/2fa/key-done', keyDoneParam({
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName,
|
|
|
|
credentialId,
|
2023-09-08 14:05:03 +09:00
|
|
|
creationOptions: registerKeyResponse.body,
|
2024-07-14 09:33:16 +09:00
|
|
|
} as any) as any, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const passwordLessResponse = await api('i/2fa/password-less', {
|
2023-03-11 14:13:39 +09:00
|
|
|
value: true,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(passwordLessResponse.status, 204);
|
|
|
|
|
2024-10-04 15:23:33 +09:00
|
|
|
const iResponse = await api('i', {}, alice);
|
|
|
|
assert.strictEqual(iResponse.status, 200);
|
|
|
|
assert.strictEqual(iResponse.body.usePasswordLessLogin, true);
|
2023-03-11 14:13:39 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinParam(),
|
|
|
|
password: '',
|
|
|
|
});
|
2024-10-04 15:23:33 +09:00
|
|
|
const signinResponseBody = signinResponse.body as unknown as {
|
|
|
|
error: {
|
|
|
|
id: string;
|
|
|
|
next: 'passkey';
|
|
|
|
authRequest: PublicKeyCredentialRequestOptionsJSON;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert.strictEqual(signinResponse.status, 403);
|
|
|
|
assert.strictEqual(signinResponseBody.error.id, '06e661b9-8146-4ae3-bde5-47138c0ae0c4');
|
|
|
|
assert.strictEqual(signinResponseBody.error.next, 'passkey');
|
|
|
|
assert.notEqual(signinResponseBody.error.authRequest.challenge, undefined);
|
|
|
|
assert.notEqual(signinResponseBody.error.authRequest.allowCredentials, undefined);
|
2023-03-11 14:13:39 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse2 = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinWithSecurityKeyParam({
|
|
|
|
keyName,
|
|
|
|
credentialId,
|
2024-10-04 15:23:33 +09:00
|
|
|
requestOptions: signinResponseBody.error.authRequest,
|
2024-07-14 09:33:16 +09:00
|
|
|
} as any),
|
2023-03-11 14:13:39 +09:00
|
|
|
password: '',
|
|
|
|
});
|
|
|
|
assert.strictEqual(signinResponse2.status, 200);
|
|
|
|
assert.notEqual(signinResponse2.body.i, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test('が設定でき、設定したセキュリティキーの名前を変更できる。', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerKeyResponse = await api('i/2fa/register-key', {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerKeyResponse.status, 200);
|
|
|
|
|
|
|
|
const keyName = 'example-key';
|
|
|
|
const credentialId = crypto.randomBytes(0x41);
|
2024-03-03 20:15:35 +09:00
|
|
|
const keyDoneResponse = await api('i/2fa/key-done', keyDoneParam({
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName,
|
|
|
|
credentialId,
|
2023-09-08 14:05:03 +09:00
|
|
|
creationOptions: registerKeyResponse.body,
|
2024-07-14 09:33:16 +09:00
|
|
|
} as any) as any, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
const renamedKey = 'other-key';
|
2024-03-03 20:15:35 +09:00
|
|
|
const updateKeyResponse = await api('i/2fa/update-key', {
|
2023-03-11 14:13:39 +09:00
|
|
|
name: renamedKey,
|
2023-09-08 14:05:03 +09:00
|
|
|
credentialId: credentialId.toString('base64url'),
|
2023-03-11 14:13:39 +09:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(updateKeyResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const iResponse = await api('i', {
|
2023-03-11 14:13:39 +09:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(iResponse.status, 200);
|
2024-07-14 09:33:16 +09:00
|
|
|
assert.ok(iResponse.body.securityKeysList);
|
2023-09-08 14:05:03 +09:00
|
|
|
const securityKeys = iResponse.body.securityKeysList.filter((s: { id: string; }) => s.id === credentialId.toString('base64url'));
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(securityKeys.length, 1);
|
|
|
|
assert.strictEqual(securityKeys[0].name, renamedKey);
|
|
|
|
assert.notEqual(securityKeys[0].lastUsed, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test('が設定でき、設定したセキュリティキーを削除できる。', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerKeyResponse = await api('i/2fa/register-key', {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerKeyResponse.status, 200);
|
|
|
|
|
|
|
|
const keyName = 'example-key';
|
|
|
|
const credentialId = crypto.randomBytes(0x41);
|
2024-03-03 20:15:35 +09:00
|
|
|
const keyDoneResponse = await api('i/2fa/key-done', keyDoneParam({
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
keyName,
|
|
|
|
credentialId,
|
2023-09-08 14:05:03 +09:00
|
|
|
creationOptions: registerKeyResponse.body,
|
2024-07-14 09:33:16 +09:00
|
|
|
} as any) as any, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
assert.strictEqual(keyDoneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
// テストの実行順によっては複数残ってるので全部消す
|
2024-10-04 15:23:33 +09:00
|
|
|
const beforeIResponse = await api('i', {
|
2023-03-11 14:13:39 +09:00
|
|
|
}, alice);
|
2024-10-04 15:23:33 +09:00
|
|
|
assert.strictEqual(beforeIResponse.status, 200);
|
|
|
|
assert.ok(beforeIResponse.body.securityKeysList);
|
|
|
|
for (const key of beforeIResponse.body.securityKeysList) {
|
2024-03-03 20:15:35 +09:00
|
|
|
const removeKeyResponse = await api('i/2fa/remove-key', {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
credentialId: key.id,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(removeKeyResponse.status, 200);
|
|
|
|
}
|
|
|
|
|
2024-10-04 15:23:33 +09:00
|
|
|
const afterIResponse = await api('i', {}, alice);
|
|
|
|
assert.strictEqual(afterIResponse.status, 200);
|
|
|
|
assert.strictEqual(afterIResponse.body.securityKeys, false);
|
2023-03-11 14:13:39 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinParam(),
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
});
|
|
|
|
assert.strictEqual(signinResponse.status, 200);
|
|
|
|
assert.notEqual(signinResponse.body.i, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2023-03-11 14:13:39 +09:00
|
|
|
test('が設定でき、設定解除できる。(パスワードのみでログインできる。)', async () => {
|
2024-03-03 20:15:35 +09:00
|
|
|
const registerResponse = await api('i/2fa/register', {
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(registerResponse.status, 200);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const doneResponse = await api('i/2fa/done', {
|
2023-03-11 14:13:39 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-08-28 18:25:31 +09:00
|
|
|
assert.strictEqual(doneResponse.status, 200);
|
2023-06-25 08:34:18 +09:00
|
|
|
|
2024-10-04 15:23:33 +09:00
|
|
|
const iResponse = await api('i', {}, alice);
|
|
|
|
assert.strictEqual(iResponse.status, 200);
|
|
|
|
assert.strictEqual(iResponse.body.twoFactorEnabled, true);
|
2023-03-11 14:13:39 +09:00
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const unregisterResponse = await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
token: otpToken(registerResponse.body.secret),
|
2023-03-11 14:13:39 +09:00
|
|
|
password,
|
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(unregisterResponse.status, 204);
|
|
|
|
|
2024-03-03 20:15:35 +09:00
|
|
|
const signinResponse = await api('signin', {
|
2023-03-11 14:13:39 +09:00
|
|
|
...signinParam(),
|
|
|
|
});
|
|
|
|
assert.strictEqual(signinResponse.status, 200);
|
|
|
|
assert.notEqual(signinResponse.body.i, undefined);
|
2023-09-22 14:12:33 +09:00
|
|
|
|
|
|
|
// 後片付け
|
2024-03-03 20:15:35 +09:00
|
|
|
await api('i/2fa/unregister', {
|
2023-09-22 14:12:33 +09:00
|
|
|
password,
|
|
|
|
token: otpToken(registerResponse.body.secret),
|
|
|
|
}, alice);
|
2023-03-11 14:13:39 +09:00
|
|
|
});
|
|
|
|
});
|