Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Adapt code from v12 to fix FIDO2 reg and login (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
CGsama authored and sousuke0422 committed Aug 3, 2021
1 parent 362520b commit 06a3406
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
33 changes: 33 additions & 0 deletions src/client/app/common/scripts/2fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,36 @@ export function hexifyAB(buffer) {
.map(item => item.toString(16).padStart(2, 0))
.join('');
}
export function byteify(data: string, encoding: 'ascii' | 'base64' | 'hex') {
switch (encoding) {
case 'ascii':
return Uint8Array.from(data, c => c.charCodeAt(0));
case 'base64':
return Uint8Array.from(
atob(
data
.replace(/-/g, '+')
.replace(/_/g, '/')
),
c => c.charCodeAt(0)
);
case 'hex':
return new Uint8Array(
data
.match(/.{1,2}/g)
.map(byte => parseInt(byte, 16))
);
}
}

export function hexify(buffer: ArrayBuffer) {
return Array.from(new Uint8Array(buffer))
.reduce(
(str, byte) => str + byte.toString(16).padStart(2, '0'),
''
);
}

export function stringify(buffer: ArrayBuffer) {
return String.fromCharCode(... new Uint8Array(buffer));
}
9 changes: 2 additions & 7 deletions src/client/app/common/views/components/settings/2fa.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import Vue from 'vue';
import i18n from '../../../../i18n';
import { hostname } from '../../../../config';
import { hexifyAB } from '../../../scripts/2fa';
import { hexifyAB, byteify } from '../../../scripts/2fa';
function stringifyAB(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
Expand Down Expand Up @@ -190,12 +190,7 @@ export default Vue.extend({
challengeId: registration.challengeId,
stage: 0,
publicKeyOptions: {
challenge: Buffer.from(
registration.challenge
.replace(/\-/g, "+")
.replace(/_/g, "/"),
'base64'
),
challenge: byteify(registration.challenge, 'base64'),
rp: {
id: hostname,
name: 'Misskey'
Expand Down
11 changes: 3 additions & 8 deletions src/client/app/common/views/components/signin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Vue from 'vue';
import i18n from '../../../i18n';
import { apiUrl, host } from '../../../config';
import { toUnicode } from 'punycode';
import { hexifyAB } from '../../scripts/2fa';
import { hexifyAB, byteify } from '../../scripts/2fa';
export default Vue.extend({
i18n: i18n('common/views/components/signin.vue'),
Expand Down Expand Up @@ -98,14 +98,9 @@ export default Vue.extend({
this.queryingKey = true;
return navigator.credentials.get({
publicKey: {
challenge: Buffer.from(
this.challengeData.challenge
.replace(/\-/g, '+')
.replace(/_/g, '/'),
'base64'
),
challenge: byteify(this.challengeData.challenge, 'base64'),
allowCredentials: this.challengeData.securityKeys.map(key => ({
id: Buffer.from(key.id, 'hex'),
id: byteify(key.id, 'hex'),
type: 'public-key',
transports: ['usb', 'nfc', 'ble', 'internal']
})),
Expand Down

0 comments on commit 06a3406

Please sign in to comment.