Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt code from v12 to fix FIDO2 reg and login #1309

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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