-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.ts
More file actions
181 lines (153 loc) · 5.24 KB
/
import.ts
File metadata and controls
181 lines (153 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import protobuf from 'protobufjs'
import { Jimp } from 'jimp'
import jsQR from 'jsqr'
import { exec } from 'child_process'
import { promisify } from 'util'
import fs from 'fs'
import os from 'os'
import path from 'path'
const execAsync = promisify(exec)
// The Google Auth Migration Protocol Buffer Schema
const typeDefs = `
syntax = "proto3";
message MigrationPayload {
repeated OtpParameters otp_parameters = 1;
int32 version = 2;
int32 batch_size = 3;
int32 batch_index = 4;
int32 batch_id = 5;
message OtpParameters {
bytes secret = 1;
string name = 2;
string issuer = 3;
Algorithm algorithm = 4;
int32 digits = 5;
OtpType type = 6;
int64 counter = 7;
}
enum Algorithm {
ALGORITHM_UNSPECIFIED = 0;
ALGORITHM_SHA1 = 1;
ALGORITHM_SHA256 = 2;
ALGORITHM_SHA512 = 3;
ALGORITHM_MD5 = 4;
}
enum OtpType {
OTP_TYPE_UNSPECIFIED = 0;
OTP_TYPE_HOTP = 1;
OTP_TYPE_TOTP = 2;
}
}
`;
// Helper function to convert Buffer to Base32 (RFC 4648)
// This is required to make the secret readable for password managers
function toBase32(buffer:any) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
let bits = 0;
let value = 0;
let output = '';
for (let i = 0; i < buffer.length; i++) {
value = (value << 8) | buffer[i];
bits += 8;
while (bits >= 5) {
output += alphabet[(value >>> (bits - 5)) & 31];
bits -= 5;
}
}
if (bits > 0) {
output += alphabet[(value << (5 - bits)) & 31];
}
return output;
}
export async function readQRCode(imagePath: string): Promise<string> {
const image = await Jimp.read(imagePath);
const { data, width, height } = image.bitmap;
const code = jsQR(data, width, height);
if (!code) {
throw new Error("No QR code found in image");
}
return code.data;
}
// Selection Screenshot QR Reading
export async function readQRFromSelection(): Promise<string> {
const tempPath = path.join(os.tmpdir(), 'auth_screenshot.png');
console.log("Select the QR code area on screen...");
if (process.platform === 'win32') {
console.log("(Drag to select area with mouse, release to capture)");
} else {
console.log("(Use mouse to drag selection, then press Enter/Space to capture)");
}
try {
if (process.platform === 'darwin') {
// macOS: screencapture -i (interactive selection)
await execAsync(`screencapture -i "${tempPath}"`);
} else if (process.platform === 'linux') {
// Linux: scrot -s (selection)
await execAsync(`scrot -s "${tempPath}"`);
} else if (process.platform === 'win32') {
// Windows: PowerShell script for selection
const scriptPath = path.join(__dirname, 'windows_selection.ps1');
await execAsync(`powershell -ExecutionPolicy Bypass -File "${scriptPath}" "${tempPath}"`);
} else {
throw new Error("Selection screenshot not supported on this platform");
}
if (!fs.existsSync(tempPath)) {
throw new Error("Screenshot cancelled or failed");
}
const uri = await readQRCode(tempPath);
// Cleanup
fs.unlinkSync(tempPath);
return uri;
} catch (error) {
if (fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath);
}
throw error;
}
}
export async function decode(uriString: string) {
const root = protobuf.parse(typeDefs).root;
const MigrationPayload = root.lookupType("MigrationPayload");
try {
const urlParams = new URLSearchParams(uriString.split('?')[1]);
const dataParam = urlParams.get('data');
const base64 = dataParam.replace(/-/g, '+').replace(/_/g, '/');
const buffer = Buffer.from(base64, 'base64');
// Decode Protobuf
const message = MigrationPayload.decode(buffer);
//@ts-ignore Return the decoded accounts
const accounts = message.otpParameters.map((otp) => {
const secretBase32 = toBase32(otp.secret);
const type = otp.type === 2 ? 'TOTP' : 'HOTP';
return {
name: otp.name,
issuer: otp.issuer,
secret: secretBase32,
type,
};
});
return accounts;
} catch (e: typeof Error | any) {
throw new Error("Error decoding: " + e?.message );
}
}
export function parseOtpAuthUri(uri: string) {
// otpauth://totp/issuer:name?secret=...&issuer=...
const url = new URL(uri);
if (url.protocol !== 'otpauth:' || url.host !== 'totp') {
throw new Error("Invalid otpauth URI");
}
const path = url.pathname.slice(1); // Remove leading /
const params = new URLSearchParams(url.search);
const secret = params.get('secret');
const issuer = params.get('issuer') || path.split(':')[0];
const name = path.split(':').slice(1).join(':') || path;
if (!secret) {
throw new Error("No secret in URI");
}
return { issuer, name, secret };
}
export function generateOtpAuthUri(issuer: string, name: string, secret: string): string {
const label = `${issuer}:${name}`;
return `otpauth://totp/${encodeURIComponent(label)}?secret=${secret}&issuer=${encodeURIComponent(issuer)}`;
}