14 Commits

21 changed files with 560 additions and 523 deletions

View File

@@ -1,12 +1,14 @@
# TOTPFIT # TOTPFIT
### Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4 with Google Authenticator migration Support
### Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4 with Google Authenticator migration support
![alt text](docs/assets/image2.png) ![alt text](docs/assets/image2.png)
### Features: ### Features:
- Supports of otpauth links with parameters "issuer", "algorithm", "digits", "period"
- Supports of `otpauth://` links with parameters "client", "issuer", "algorithm", "digits", "period", "offset"
- Addition/Edition/Deletion of TOTPs from mobile app - Addition/Edition/Deletion of TOTPs from mobile app
- Support of google migration links formated: ```otpauth-migration://offline?data=...``` - Support of Google Authenticator migration links formated: `otpauth-migration://offline?data=...` (At this stage with only 6 digits and only 30 seconds period)
### Guides: ### Guides:

View File

@@ -1,17 +1,13 @@
import { BaseSideService } from "@zeppos/zml/base-side" import { BaseSideService } from "@zeppos/zml/base-side";
AppSideService( AppSideService(
BaseSideService( BaseSideService({
{ onInit() {},
onInit(){ onRequest(request, response) {
if (request.method === "totps") {
}, response(null, settings.settingsStorage.getItem("TOTPs"));
onRequest(request, response){
if(request.method === 'totps'){
response(null, settings.settingsStorage.getItem('TOTPs'))
} }
}, },
onSettingsChange(){ } onSettingsChange() {},
} }),
) );
)

2
app.js
View File

@@ -7,5 +7,5 @@ App(
}, },
onCreate() {}, onCreate() {},
onDestroy() {}, onDestroy() {},
}) }),
); );

View File

@@ -6,16 +6,13 @@
"appType": "app", "appType": "app",
"version": { "version": {
"code": 1, "code": 1,
"name": "1.3" "name": "1.3.0"
}, },
"icon": "icon.png", "icon": "icon.png",
"vender": "zepp", "vender": "zepp",
"description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4" "description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4"
}, },
"permissions": [ "permissions": ["data:os.device.info", "device:os.local_storage"],
"data:os.device.info",
"device:os.local_storage"
],
"runtime": { "runtime": {
"apiVersion": { "apiVersion": {
"compatible": "3.0.0", "compatible": "3.0.0",
@@ -27,10 +24,7 @@
"default": { "default": {
"module": { "module": {
"page": { "page": {
"pages": [ "pages": ["page/index", "page/tip"]
"page/index",
"page/tip"
]
}, },
"app-side": { "app-side": {
"path": "app-side/index" "path": "app-side/index"

View File

@@ -6,7 +6,7 @@ To add 2FA TOTP records using 2FA TOTP QR-Codes, you must scan QR-Code of servic
![QR Code with URI](image.png) ![QR Code with URI](image.png)
Copy this URI string and paste it to app using button *"Add new TOTP record"*: Copy this URI string and paste it to app using button _"Add new TOTP record"_:
![Add new TOTP record popup](image-2.png) ![Add new TOTP record popup](image-2.png)
@@ -14,24 +14,24 @@ Then press OK, record will appear on page
![Added record](image-4.png) ![Added record](image-4.png)
You can edit your otpauth:// records using button "Change TOTP link". Your previous record will be replaced with a new otpauth:// link entered on text field, and previous link will not be shown on field. You can edit your otpauth:// records using button "Change TOTP link". Your previous record will be replaced with a new otpauth:// link from text field, and previous link will not be shown on field.
### If you use google migrations (otpauth-migration:// links) ### If you use google migrations (otpauth-migration:// links)
To add 2FA TOTP recods using migration from Google Authenticator app, you must go to menu, select "Transfer accounts" -> "Export accounts" To add 2FA TOTP recods using migration from Google Authenticator app, you must go to menu, select "Transfer accounts" -> "Export accounts"
Select codes then screenshot QR code and and scan (decode) it to a URI. Use any app providing scan from image, ex: "Search screen" function (Google Lens) on Google Assistant. Select codes then screenshot QR code and scan (decode) it to a URI. Use any app providing scan from image, ex: "Search screen" function (Google Lens) on Google Assistant.
For example, this QR-Code will represent next URI string: For example, this QR-Code will represent next URI string:
![Google lens scan from Google Authenticator](image-5.png) ![Google lens scan from Google Authenticator](image-5.png)
After scaning copy this URI string and paste it to app using button *"Add new TOTP record"*: After scaning copy this URI string and paste it to app using button _"Add new TOTP record"_:
![Add new TOTP record using otpauth-migration](image-6.png) ![Add new TOTP record using otpauth-migration](image-6.png)
Then press OK, all selected records on Google Authenticator will appear on page Then press OK, all selected records from Google Authenticator will appear on page
![Added records from otpauth-migration](image-7.png) ![Added records from otpauth-migration](image-7.png)
You can edit your records using button "Change TOTP link". Your previous record will be replaced with a new otpauth:// link entered on text field, and previous link will not be shown on field. You can edit your records using button "Change TOTP link". Your previous record will be replaced with a new otpauth:// link from text field (otpauth-migration:// will not work), and previous link will not be shown on field.

View File

@@ -1,4 +1,4 @@
import { Buffer } from 'buffer' import { Buffer } from "buffer";
export function parseInput(input) { export function parseInput(input) {
const normalizedInput = input.replace(/\s/g, ""); const normalizedInput = input.replace(/\s/g, "");
@@ -33,4 +33,5 @@ export function bufferLeToBeHex(buffer) {
return output; return output;
} }
export const bufferToPrettyHex = b => [...b].map(c => c.toString(16).padStart(2, '0')).join(' '); export const bufferToPrettyHex = (b) =>
[...b].map((c) => c.toString(16).padStart(2, "0")).join(" ");

View File

@@ -48,7 +48,7 @@ export class BufferReader {
"Not enough bytes left. Requested: " + "Not enough bytes left. Requested: " +
length + length +
" left: " + " left: " +
bytesAvailable bytesAvailable,
); );
} }
} }
@@ -66,7 +66,7 @@ export const TYPES = {
VARINT: 0, VARINT: 0,
FIXED64: 1, FIXED64: 1,
LENDELIM: 2, LENDELIM: 2,
FIXED32: 5 FIXED32: 5,
}; };
export function decodeProto(buffer) { export function decodeProto(buffer) {
@@ -102,17 +102,17 @@ export function decodeProto(buffer) {
byteRange, byteRange,
index, index,
type, type,
value value,
}); });
} }
} catch (err) { } catch (err) {
reader.resetToCheckpoint(); reader.resetToCheckpoint();
console.log(err); console.log(`ERR: ${err}`);
} }
return { return {
parts, parts,
leftOver: reader.readBuffer(reader.leftBytes()) leftOver: reader.readBuffer(reader.leftBytes()),
}; };
} }

View File

@@ -18,6 +18,6 @@ export function decodeVarint(buffer, offset) {
return { return {
value: res, value: res,
length: shift / 7 length: shift / 7,
}; };
} }

View File

@@ -1,6 +1,6 @@
import { decode } from "./base32decoder.js"; import { decode } from "./base32decoder.js";
import jsSHA from "jssha"; import jsSHA from "jssha";
"use bigint" ("use bigint");
/** /**
* get HOTP based on counter * get HOTP based on counter
* @param {BigInt} counter BigInt counter of HOTP * @param {BigInt} counter BigInt counter of HOTP
@@ -9,30 +9,34 @@ import jsSHA from "jssha";
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation) * @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
* @returns HOTP string * @returns HOTP string
*/ */
export function getHOTP(counter, secret, digits = 6, hashType = 'SHA-1'){ export function getHOTP(counter, secret, digits = 6, hashType = "SHA-1") {
//Stage 1: Prepare data //Stage 1: Prepare data
const rawDataCounter = new DataView(new ArrayBuffer(8)) const rawDataCounter = new DataView(new ArrayBuffer(8));
rawDataCounter.setUint32(4, counter) rawDataCounter.setUint32(4, counter);
const bCounter = new Uint8Array(rawDataCounter.buffer) const bCounter = new Uint8Array(rawDataCounter.buffer);
const bSecret = new Uint8Array(decode(secret).match(/.{1,2}/g).map(chunk => parseInt(chunk, 16))); //confirmed const bSecret = new Uint8Array(
decode(secret)
.match(/.{1,2}/g)
.map((chunk) => parseInt(chunk, 16)),
); //confirmed
//Stage 2: Hash data //Stage 2: Hash data
const jssha = new jsSHA(hashType, 'UINT8ARRAY') const jssha = new jsSHA(hashType, "UINT8ARRAY");
jssha.setHMACKey(bSecret, 'UINT8ARRAY') jssha.setHMACKey(bSecret, "UINT8ARRAY");
jssha.update(bCounter) jssha.update(bCounter);
const hmacResult = jssha.getHMAC('UINT8ARRAY') //confirmed const hmacResult = jssha.getHMAC("UINT8ARRAY"); //confirmed
//Stage 3: Dynamic truncate //Stage 3: Dynamic truncate
const offsetB = hmacResult[19] & 0xf; const offsetB = hmacResult[19] & 0xf;
const P = hmacResult.slice(offsetB, offsetB + 4) const P = hmacResult.slice(offsetB, offsetB + 4);
P[0] = P[0] & 0x7f; P[0] = P[0] & 0x7f;
//Stage 4: Format string //Stage 4: Format string
let res = (new DataView(P.buffer).getInt32(0) % Math.pow(10, digits)).toString() let res = (
while(res.length < digits) new DataView(P.buffer).getInt32(0) % Math.pow(10, digits)
res = '0' + res; ).toString();
while (res.length < digits) res = "0" + res;
return res; return res;
} }
@@ -46,8 +50,14 @@ export function getHOTP(counter, secret, digits = 6, hashType = 'SHA-1'){
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation) * @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
* @returns TOTP string * @returns TOTP string
*/ */
export function getTOTP(secret, digits = 6, time = Date.now(), fetchTime = 30, timeOffset = 0, hashType = 'SHA-1') export function getTOTP(
{ secret,
const unixTime = Math.round((time / 1000 + timeOffset) / fetchTime) digits = 6,
return getHOTP(BigInt(unixTime), secret, digits) time = Date.now(),
fetchTime = 30,
timeOffset = 0,
hashType = "SHA-1",
) {
const unixTime = Math.round((time / 1000n + timeOffset) / fetchTime);
return getHOTP(BigInt(unixTime), secret, digits, hashType);
} }

View File

@@ -26,35 +26,37 @@ function leftpad(str, len, pad) {
} }
export function encode(bytes) { export function encode(bytes) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
let bits = 0; let bits = 0;
let value = 0; let value = 0;
let output = ''; let output = "";
for (let i = 0; i < bytes.length; i++) { for (let i = 0; i < bytes.length; i++) {
value = (value << 8) | bytes[i]; value = (value << 8) | bytes[i];
bits += 8; bits += 8;
while (bits >= 5) { while (bits >= 5) {
output += alphabet[(value >>> (bits - 5)) & 0x1F]; output += alphabet[(value >>> (bits - 5)) & 0x1f];
bits -= 5; bits -= 5;
} }
} }
if (bits > 0) { if (bits > 0) {
output += alphabet[(value << (5 - bits)) & 0x1F]; output += alphabet[(value << (5 - bits)) & 0x1f];
} }
const paddingLength = (8 - (output.length % 8)) % 8; const paddingLength = (8 - (output.length % 8)) % 8;
output += '='.repeat(paddingLength); output += "=".repeat(paddingLength);
return output; return output;
} }
export function base64decode(base64) { export function base64decode(base64) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let result = []; let result = [];
let i = 0, j = 0; let i = 0,
j = 0;
let b1, b2, b3, b4; let b1, b2, b3, b4;
while (i < base64.length) { while (i < base64.length) {

View File

@@ -1,4 +1,4 @@
import { getHOTP } from "./OTPGenerator.js" import { getHOTP } from "./OTPGenerator.js";
/** /**
* TOTP instance * TOTP instance
*/ */
@@ -13,31 +13,33 @@ export class TOTP {
* @param {number} [timeOffset=0] time offset for token in seconds * @param {number} [timeOffset=0] time offset for token in seconds
* @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation) * @param {string} [hashType='SHA-1'] type of hash (more in jsSHA documentation)
*/ */
constructor(secret, constructor(
secret,
issuer, issuer,
client, client,
digits = 6, digits = 6,
fetchTime = 30, fetchTime = 30,
timeOffset = 0, timeOffset = 0,
hashType = 'SHA-1') { hashType = "SHA-1",
this.secret = secret ) {
this.issuer = issuer this.secret = secret;
this.client = client this.issuer = issuer;
this.digits = digits this.client = client;
this.fetchTime = fetchTime this.digits = digits;
this.timeOffset = timeOffset this.fetchTime = fetchTime;
this.hashType = hashType this.timeOffset = timeOffset;
this.hashType = hashType;
} }
static copy(totp){ static copy(totp) {
return new TOTP( return new TOTP(
secret = totp.secret, (secret = totp.secret),
issuer = totp.TOTPissuer, (issuer = totp.TOTPissuer),
client = totp.client, (client = totp.client),
digits = totp.digits, (digits = totp.digits),
fetchTime = totp.fetchTime, (fetchTime = totp.fetchTime),
timeOffset = totp.timeOffset, (timeOffset = totp.timeOffset),
hashType = totp.hashType (hashType = totp.hashType),
) );
} }
/** /**
* *
@@ -45,16 +47,17 @@ export class TOTP {
* @returns OTP instance * @returns OTP instance
*/ */
getOTP(time = Date.now()) { getOTP(time = Date.now()) {
const unixTime = (time / 1000 + this.timeOffset) / this.fetchTime const unixTime = (time / 1000 + this.timeOffset) / this.fetchTime;
const otp = getHOTP(Math.floor(unixTime), this.secret, this.digits) const otp = getHOTP(Math.floor(unixTime), this.secret, this.digits, this.hashType);
const expireTime = time + const expireTime =
time +
(this.fetchTime - (this.fetchTime -
(time / 1000 + this.timeOffset) % ((time / 1000 + this.timeOffset) % this.fetchTime)) *
this.fetchTime) * 1000 1000;
const createdTime = time - (((time / 1000 + this.timeOffset) % const createdTime =
this.fetchTime) * 1000) time - ((time / 1000 + this.timeOffset) % this.fetchTime) * 1000;
return new OTP(otp, createdTime, expireTime) return new OTP(otp, createdTime, expireTime);
} }
} }
@@ -69,8 +72,8 @@ export class OTP {
* @param {number} expireTime time in unix epoch to expire OTP * @param {number} expireTime time in unix epoch to expire OTP
*/ */
constructor(otp, createdTime, expireTime) { constructor(otp, createdTime, expireTime) {
this.otp = otp this.otp = otp;
this.createdTime = createdTime this.createdTime = createdTime;
this.expireTime = expireTime this.expireTime = expireTime;
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "totpfit", "name": "totpfit",
"version": "1.3", "version": "1.3.0",
"description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4", "description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4",
"main": "app.js", "main": "app.js",
"author": "Lisoveliy", "author": "Lisoveliy",

View File

@@ -17,19 +17,18 @@ Page(
let localStorage = new LocalStorage(); let localStorage = new LocalStorage();
localStorage.setItem( localStorage.setItem(
"TOTPs", "TOTPs",
JSON.stringify(app._options.globalData.TOTPS) JSON.stringify(app._options.globalData.TOTPS),
); );
this.initPage(); this.initPage();
}) })
.catch((x) => { .catch((x) => {
console.log(`Init failed: ${x}`); console.log(`ERR: Init failed - ${x}`);
try{ try {
let localStorage = new LocalStorage(); let localStorage = new LocalStorage();
app._options.globalData.TOTPS = JSON.parse( app._options.globalData.TOTPS = JSON.parse(
localStorage.getItem("TOTPs", []) localStorage.getItem("TOTPs", []),
); );
} } catch {
catch{
app._options.globalData.TOTPS = []; app._options.globalData.TOTPS = [];
} }
this.initPage(); this.initPage();
@@ -56,5 +55,5 @@ Page(
method: "totps", method: "totps",
}); });
}, },
}) }),
); );

View File

@@ -30,7 +30,7 @@ function renderTOTPs(buffer) {
expireBar: RenderExpireBar( expireBar: RenderExpireBar(
i, i,
otpData.createdTime, otpData.createdTime,
buffer[i].fetchTime buffer[i].fetchTime,
), ),
}; };
setInterval(() => { setInterval(() => {
@@ -38,7 +38,7 @@ function renderTOTPs(buffer) {
(Date.now() - otpData.createdTime) / (Date.now() - otpData.createdTime) /
1000 / 1000 /
buffer[i].fetchTime - buffer[i].fetchTime -
1 1,
); );
renderData[i].expireBar.setProperty(prop.MORE, { renderData[i].expireBar.setProperty(prop.MORE, {

View File

@@ -77,7 +77,7 @@ export function RenderOTPValue(position, otpValue) {
export function RenderExpireBar(position, createdTime, fetchTime) { export function RenderExpireBar(position, createdTime, fetchTime) {
const yPos = getYPos(position); const yPos = getYPos(position);
const expireDif = Math.abs( const expireDif = Math.abs(
(Date.now() - createdTime) / 1000 / fetchTime - 1 (Date.now() - createdTime) / 1000 / fetchTime - 1,
); );
return createWidget(widget.ARC, { return createWidget(widget.ARC, {
x: buttonWidth - 50, x: buttonWidth - 50,

View File

@@ -27,5 +27,5 @@ Page(
text: "To add TOTP record open\n settings on Zepp app", text: "To add TOTP record open\n settings on Zepp app",
}); });
}, },
}) }),
); );

34
setting/consts.js Normal file
View File

@@ -0,0 +1,34 @@
export const colors = {
bg: "#101010",
linkBg: "#ffffffc0",
secondaryBg: "#282828",
text: "#fafafa",
alert: "#ad3c23",
notify: "#555555",
bigText: "#fafafa",
};
export const content = {
addTotpsHint: "For add a 2FA TOTP record you must have otpauth:// link or otpauth-migration:// link from Google Authenticator Migration QR-Code",
createButton: {
placeHolder: "otpauth(-migration)://",
label: "Add new TOTP record"
},
instructionLink: {
label: "Instruction | Report issue (GitHub)",
source: "https://github.com/Lisoveliy/totpfit/blob/main/docs/guides/how-to-add-totps/README.md"
},
changeButton: {
label: "Change TOTP link",
placeHolder: "otpauth(-migration)://"
},
deleteButton: {
label: "Delete"
},
totpLabelText: {
eval(issuer, client) {return `${issuer}: ${client}`;}
},
totpDescText: {
eval(hashType, digits, fetchTime, timeOffset) { return `${hashType} | ${digits} digits | ${fetchTime} seconds | ${timeOffset} sec offset`; }
}
};

View File

@@ -1,26 +1,19 @@
import { getTOTPByLink } from "./utils/queryParser.js"; import { getTOTPByLink } from "./utils/queryParser.js";
import { colors, content } from "./consts.js";
let _props = null; let _props = null;
const colors = {
bg: "#101010",
linkBg: "#ffffffc0",
secondaryBg: "#282828",
text: "#fafafa",
alert: "#ad3c23",
notify: "#555555",
bigText: "#fafafa"
};
AppSettingsPage({ AppSettingsPage({
build(props) { build(props) {
_props = props; _props = props;
const storage = JSON.parse( const storage = JSON.parse(
props.settingsStorage.getItem("TOTPs") ?? "[]" props.settingsStorage.getItem("TOTPs") ?? "[]",
); );
const totpEntrys = GetTOTPList(storage); const totpEntrys = GetTOTPList(storage);
const addTOTPsHint = storage.length < 1 ? const addTOTPsHint =
Text({ storage.length < 1
? Text(
{
paragraph: true, paragraph: true,
align: "center", align: "center",
style: { style: {
@@ -31,20 +24,20 @@ AppSettingsPage({
verticalAlign: "middle", verticalAlign: "middle",
}, },
}, },
"For add a 2FA TOTP record you must have otpauth:// link or otpauth-migration:// link from Google Authenticator Migration QR-Code" content.addTotpsHint,
) : null; )
: null;
const createButton = TextInput({ const createButton = TextInput({
placeholder: "otpauth(-migration)://", placeholder: content.createButton.placeHolder,
label: "Add new TOTP record", label: content.createButton.label,
onChange: (changes) => { onChange: (changes) => {
let link = getTOTPByLink(changes); let link = getTOTPByLink(changes);
if (link == null) { if (link == null) {
console.log("link is invalid"); console.log("ERR: link is invalid");
return; return;
} }
if (Array.isArray(link)) if (Array.isArray(link)) storage.push(...link);
storage.push(...link);
else storage.push(link); else storage.push(link);
updateStorage(storage); updateStorage(storage);
@@ -61,7 +54,7 @@ AppSettingsPage({
position: storage.length < 1 ? "absolute" : null, //TODO: Сделать что-то с этим кошмаром position: storage.length < 1 ? "absolute" : null, //TODO: Сделать что-то с этим кошмаром
bottom: storage.length < 1 ? "0px" : null, bottom: storage.length < 1 ? "0px" : null,
left: storage.length < 1 ? "0px" : null, left: storage.length < 1 ? "0px" : null,
right: storage.length < 1 ? "0px" : null right: storage.length < 1 ? "0px" : null,
}, },
}); });
@@ -79,7 +72,9 @@ AppSettingsPage({
textAlign: "center", textAlign: "center",
}, },
}, },
storage.length < 1 ? addTOTPsHint : Text( storage.length < 1
? addTOTPsHint
: Text(
{ {
align: "center", align: "center",
paragraph: true, paragraph: true,
@@ -91,46 +86,65 @@ AppSettingsPage({
verticalAlign: "middle", verticalAlign: "middle",
}, },
}, },
"TOTP records:" "TOTP records:",
) ),
), ),
...totpEntrys, ...totpEntrys,
createButton, createButton,
View({ View(
{
style: { style: {
display: "flex", display: "flex",
justifyContent: "center" justifyContent: "center",
}
}, },
Link({
source: "https://github.com/Lisoveliy/totpfit/blob/main/docs/guides/how-to-add-totps/README.md"
}, },
"Instruction | Report issue (GitHub)") Link(
{
source: content.instructionLink.source,
},
content.instructionLink.label,
), ),
] ),
],
); );
return body; return body;
}, },
}); });
/**
* Get DOM elements of TOTP records
* @param {*} storage Array of TOTP objects
* @returns Array of DOM elements
*/
function GetTOTPList(storage) { function GetTOTPList(storage) {
let totpEntrys = []; let totpEntrys = [];
let counter = 0; let counter = 0;
storage.forEach((element) => { storage.forEach((element) => {
const elementId = counter; const elementId = counter;
const textInput = TextInput({ const totpLabelText = Text(
placeholder: "otpauth(-migration)://", {
label: "Change TOTP link", align: "center",
style: {
color: colors.text,
fontSize: "18px",
fontWeight: "500",
},
paragraph: true,
},
content.totpLabelText.eval(element.issuer, element.client),
);
const changeButton = TextInput({
placeholder: content.changeButton.placeHolder,
label: content.changeButton.label,
onChange: (changes) => { onChange: (changes) => {
try { try {
let link = getTOTPByLink(changes); let link = getTOTPByLink(changes);
if (Array.isArray(link)) if (Array.isArray(link)) return;
return;
storage[elementId] = link; storage[elementId] = link;
updateStorage(storage); updateStorage(storage);
} catch (err) { } catch (err) {
console.log(err); console.log(`ERR: ${err}`);
} }
}, },
labelStyle: { labelStyle: {
@@ -146,22 +160,20 @@ function GetTOTPList(storage) {
borderRadius: "5px", borderRadius: "5px",
}, },
}); });
const textBig = Text( const totpDescText = Text(
{ {
align: "center",
style: { style: {
color: colors.text, color: colors.text,
fontSize: "18px", fontSize: "14px",
fontWeight: "500"
}, },
paragraph: true, align: "center",
}, },
`${element.issuer}: ${element.client}` content.totpDescText.eval(element.hashType, element.digits, element.fetchTime, element.timeOffset),
); );
const delButton = Button({ const deleteButton = Button({
onClick: () => { onClick: () => {
storage = storage.filter( storage = storage.filter(
(x) => storage.indexOf(x) != elementId (x) => storage.indexOf(x) != elementId,
); );
updateStorage(storage); updateStorage(storage);
}, },
@@ -172,31 +184,20 @@ function GetTOTPList(storage) {
height: "fit-content", height: "fit-content",
margin: "10px", margin: "10px",
}, },
label: "Delete", label: content.deleteButton.label,
}); });
const text = Text(
{
style: {
color: colors.text,
fontSize: "14px",
},
align: "center",
},
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | ${element.timeOffset} sec offset`
);
const view = View( const view = View(
{ {
style: { style: {
textAlign: "center", textAlign: "center",
backgroundColor: colors.secondaryBg, backgroundColor: colors.secondaryBg,
//border: "2px solid white",
borderRadius: "5px", borderRadius: "5px",
margin: "10px", margin: "10px",
}, },
}, },
[ [
textBig, totpLabelText,
text, totpDescText,
View( View(
{ {
style: { style: {
@@ -204,17 +205,21 @@ function GetTOTPList(storage) {
gridTemplateColumns: "1fr 100px", gridTemplateColumns: "1fr 100px",
}, },
}, },
[textInput, delButton] [changeButton, deleteButton],
), ),
] ],
); );
totpEntrys.push({ text: text, view: view }); totpEntrys.push({ text: totpDescText, view: view });
counter++; counter++;
}); });
return totpEntrys.map((x) => x.view); return totpEntrys.map((x) => x.view);
} }
/**
* Update
* @param {*} storage Array of TOTP objects
*/
function updateStorage(storage) { function updateStorage(storage) {
_props.settingsStorage.setItem("TOTPs", JSON.stringify(storage)); _props.settingsStorage.setItem("TOTPs", JSON.stringify(storage));
} }

View File

@@ -6,10 +6,9 @@ const otpauthScheme = "otpauth:/";
const googleMigrationScheme = "otpauth-migration:/"; const googleMigrationScheme = "otpauth-migration:/";
export function getTOTPByLink(link) { export function getTOTPByLink(link) {
if (link.includes(otpauthScheme)) if (link.includes(otpauthScheme)) return getByOtpauthScheme(link);
return getByOtpauthScheme(link)
if (link.includes(googleMigrationScheme)) if (link.includes(googleMigrationScheme))
return getByGoogleMigrationScheme(link) return getByGoogleMigrationScheme(link);
return null; return null;
} }
@@ -54,16 +53,15 @@ function getByOtpauthScheme(link) {
Number(digits), Number(digits),
Number(period), Number(period),
Number(offset), Number(offset),
getHashType(algorithm) getHashType(algorithm),
); );
} catch (err) { } catch (err) {
console.log(err) console.log(`ERR: ${err}`);
return null; return null;
} }
} }
function getByGoogleMigrationScheme(link) { function getByGoogleMigrationScheme(link) {
let data = link.split("data=")[1]; //Returns base64 encoded data let data = link.split("data=")[1]; //Returns base64 encoded data
data = decodeURIComponent(data); data = decodeURIComponent(data);
let decode = base64decode(data); let decode = base64decode(data);
@@ -71,42 +69,35 @@ function getByGoogleMigrationScheme(link) {
let protoTotps = []; let protoTotps = [];
proto.parts.forEach(part => { proto.parts.forEach((part) => {
if (part.type == TYPES.LENDELIM) { if (part.type == TYPES.LENDELIM) {
protoTotps.push(decodeProto(part.value)); protoTotps.push(decodeProto(part.value));
} }
}); });
let totps = []; let totps = [];
protoTotps.forEach(x => { protoTotps.forEach((x) => {
let type = x.parts.filter(x => x.index == 6)[0]; //find type of OTP let type = x.parts.filter((x) => x.index == 6)[0]; //find type of OTP
if (type.value !== '2') { if (type.value !== "2") {
console.log("ERR: it's a not TOTP record") console.log("ERR: it's a not TOTP record");
return; return;
} }
let secret = x.parts.filter(x => x.index == 1)[0].value; let secret = x.parts.filter((x) => x.index == 1)[0].value;
secret = encode(secret); secret = encode(secret);
let name = bytesToString(x.parts.filter(x => x.index == 2)[0].value); let name = bytesToString(x.parts.filter((x) => x.index == 2)[0].value);
let issuer = bytesToString(x.parts.filter(x => x.index == 3)[0].value); let issuer = bytesToString(
x.parts.filter((x) => x.index == 3)[0].value,
);
totps.push(new TOTP( totps.push(new TOTP(secret, issuer, name, 6, 30, 0, "SHA-1"));
secret,
issuer,
name,
6,
30,
0,
"SHA-1"
));
}); });
return totps; return totps;
} }
function bytesToString(bytes) { function bytesToString(bytes) {
let str = ''; let str = "";
for (let i = 0; i < bytes.length; i++) { for (let i = 0; i < bytes.length; i++) {
str += String.fromCharCode(bytes[i]); str += String.fromCharCode(bytes[i]);
} }