Skip to content

Commit

Permalink
fix: use base64url to encode the migration param
Browse files Browse the repository at this point in the history
  • Loading branch information
LowderPlay committed Oct 10, 2023
1 parent a866743 commit fcf1f15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions bot/main.py
Expand Up @@ -19,14 +19,18 @@


async def migrate(update: Update, context: ContextTypes.DEFAULT_TYPE):
data = urllib.parse.quote_plus(update.message.web_app_data.data)
data = update.message.web_app_data.data

qr = qrcode.make(f"otpauth-migration://offline?data={data}")
urlencoded = urllib.parse.quote_plus(data)

qr = qrcode.make(f"otpauth-migration://offline?data={urlencoded}")
qr_bytes = BytesIO()
qr_bytes.name = "image.png"
qr.save(qr_bytes, "png")
qr_bytes.seek(0)

data = data.replace("+", "-").replace("/", "_").replace("=", "")

url = f"{app_tg}?startapp={data}"

await context.bot.send_photo(chat_id=update.effective_chat.id,
Expand Down
10 changes: 9 additions & 1 deletion src/migration/import.ts
Expand Up @@ -7,9 +7,17 @@ export default function decodeGoogleAuthenticator(uri: string): Account[] | null
if (!uri.startsWith("otpauth-migration://offline")) return null;

const url = new URL(uri);
const dataParam = url.searchParams.get("data");
let dataParam = url.searchParams.get("data");
if (!dataParam) return null;

// Convert from base64url
dataParam = dataParam
.replace(/-/g, '+')
.replace(/_/g, '/');

const pad = dataParam.length % 4;
dataParam += "=".repeat(pad);

const buffer = Uint8Array.from(atob(dataParam), (c) => c.charCodeAt(0));

let payload;
Expand Down

0 comments on commit fcf1f15

Please sign in to comment.