From 315728415b2269981a04ee51af7ef18412d7bf70 Mon Sep 17 00:00:00 2001 From: Willy Woitas Date: Sun, 18 Feb 2024 01:49:42 +0100 Subject: feat: Parse 2FA SMS token and copy to clipboard (#1561) --- src/stores/ServicesStore.ts | 52 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'src/stores') diff --git a/src/stores/ServicesStore.ts b/src/stores/ServicesStore.ts index 175a2ce16..d7804a3fe 100644 --- a/src/stores/ServicesStore.ts +++ b/src/stores/ServicesStore.ts @@ -1,5 +1,5 @@ import { join } from 'node:path'; -import { ipcRenderer, shell } from 'electron'; +import { clipboard, ipcRenderer, shell } from 'electron'; import { action, reaction, computed, observable, makeObservable } from 'mobx'; import { debounce, remove } from 'lodash'; import ms from 'ms'; @@ -834,6 +834,54 @@ export default class ServicesStore extends TypedStore { break; } case 'notification': { + const { notificationId, options } = args[0]; + + const { isTwoFactorAutoCatcherEnabled, twoFactorAutoCatcherMatcher } = + this.stores.settings.all.app; + + debug( + 'Settings for catch tokens', + isTwoFactorAutoCatcherEnabled, + twoFactorAutoCatcherMatcher, + ); + + if (isTwoFactorAutoCatcherEnabled) { + /* + parse the token digits from sms body, find "token" or "code" in options.body which reflect the sms content + --- + Token: 03624 / SMS-Code = PIN Token + --- + Prüfcode 010313 für Microsoft-Authentifizierung verwenden. + --- + 483133 is your GitHub authentication code. @github.com #483133 + --- + eBay: Ihr Sicherheitscode lautet 080090. \nEr läuft in 15 Minuten ab. Geben Sie den Code nicht an andere weiter. + --- + PayPal: Ihr Sicherheitscode lautet: 989605. Geben Sie diesen Code nicht weiter. + */ + + const rawBody = options.body; + const { 0: token } = /\d{5,6}/.exec(options.body) || []; + + const wordsToCatch = twoFactorAutoCatcherMatcher + .replaceAll(', ', ',') + .split(','); + + debug('wordsToCatch', wordsToCatch); + + if ( + token && + wordsToCatch.some(a => + options.body.toLowerCase().includes(a.toLowerCase()), + ) + ) { + // with the extra "+ " it shows its copied to clipboard in the notification + options.body = `+ ${rawBody}`; + clipboard.writeText(token); + debug('Token parsed and copied to clipboard'); + } + } + // Check if we are in scheduled Do-not-Disturb time const { scheduledDNDEnabled, scheduledDNDStart, scheduledDNDEnd } = this.stores.settings.all.app; @@ -845,8 +893,6 @@ export default class ServicesStore extends TypedStore { return; } - const { notificationId, options } = args[0]; - if (service.isMuted || this.stores.settings.all.app.isAppMuted) { Object.assign(options, { silent: true, -- cgit v1.2.3-54-g00ecf