Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f6fb86fee | |||
e43cf044e0 | |||
08894ee061 | |||
1e69a70ea0 | |||
86f382bef9 | |||
eaf6aefd5b | |||
52d053e024 | |||
f8fa7a8507 | |||
259c2cdd2e | |||
d45be3ee68 | |||
56d5edc37b | |||
ea75f03fbe | |||
7c33fcffc8 | |||
3e037da6da | |||
cfa6bf0c23 | |||
54526f0724 | |||
1d61879baf | |||
6a2ab542b9 | |||
3aaedec7f6 | |||
70841349b7 | |||
7eafac6916 | |||
08b11a3d09 |
@ -1 +1 @@
|
|||||||
tabWidth: 4
|
tabWidth: 4
|
||||||
|
21
README.md
@ -1,15 +1,22 @@
|
|||||||
# TOTPFIT
|
# TOTPFIT
|
||||||
### Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4 with Google Authenticator migration Support
|
|
||||||
|
|
||||||

|
### Another 2FAuthenticator based on TOTP for Zepp OS with Google Authenticator and Proton Authenticator support
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
### Features:
|
### Features:
|
||||||
- Supports of otpauth links with parameters "issuer", "algorithm", "digits", "period"
|
|
||||||
- Addition/Edition/Deletion of TOTPs from mobile app
|
- Support of `otpauth://` links with parameters "client", "issuer", "algorithm", "digits", "period", "offset"
|
||||||
- Support of google migration links formated: ```otpauth-migration://offline?data=...```
|
- Support of Google Authenticator migration links formated: `otpauth-migration://offline?data=...`
|
||||||
|
- Support of **Proton Authenticator** export with parameters "client", "issuer", "algorithm", "digits", "period", "offset" **(BETA)**
|
||||||
|
- Addition/Sort/Edition/Deletion of TOTPs from mobile app
|
||||||
|
|
||||||
### Guides:
|
### Guides:
|
||||||
|
|
||||||
[How to add 2FA TOTP records (keys) on app](/docs/guides/how-to-add-totps/README.md)
|
[How to add 2FA TOTP records (keys) on app](/docs/guides/how-to-add-totps/README.md)
|
||||||
|
|
||||||
#### This repo has mirror for issues on [GitHub](https://github.com/Lisoveliy/totpfit)
|
#### This repo has mirror for public issues on [GitHub](https://github.com/Lisoveliy/totpfit)
|
||||||
|
|
||||||
|
#### To contribute on this repo you must use my [Gitea](https://git.lisoveliy.su/Lisoveliy/totpfit)
|
||||||
|
|
||||||
|
Don't be shy to contact me for access!
|
@ -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
@ -7,5 +7,5 @@ App(
|
|||||||
},
|
},
|
||||||
onCreate() {},
|
onCreate() {},
|
||||||
onDestroy() {},
|
onDestroy() {},
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
103
app.json
@ -1,56 +1,55 @@
|
|||||||
{
|
{
|
||||||
"configVersion": "v3",
|
"configVersion": "v3",
|
||||||
"app": {
|
"app": {
|
||||||
"appId": 25087,
|
"appId": 25087,
|
||||||
"appName": "totpfit",
|
"appName": "totpfit",
|
||||||
"appType": "app",
|
"appType": "app",
|
||||||
"version": {
|
"version": {
|
||||||
"code": 1,
|
"code": 1,
|
||||||
"name": "1.3"
|
"name": "1.4.0"
|
||||||
|
},
|
||||||
|
"icon": "icon.png",
|
||||||
|
"vender": "zepp",
|
||||||
|
"description": "Another 2FAuthenticator based on TOTP for Zepp OS"
|
||||||
},
|
},
|
||||||
"icon": "icon.png",
|
"permissions": ["data:os.device.info", "device:os.local_storage"],
|
||||||
"vender": "zepp",
|
"runtime": {
|
||||||
"description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4"
|
"apiVersion": {
|
||||||
},
|
"compatible": "3.0.0",
|
||||||
"permissions": [
|
"target": "3.0.0",
|
||||||
"data:os.device.info",
|
"minVersion": "3.0"
|
||||||
"device:os.local_storage"
|
|
||||||
],
|
|
||||||
"runtime": {
|
|
||||||
"apiVersion": {
|
|
||||||
"compatible": "3.0.0",
|
|
||||||
"target": "3.0.0",
|
|
||||||
"minVersion": "3.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"targets": {
|
|
||||||
"default": {
|
|
||||||
"module": {
|
|
||||||
"page": {
|
|
||||||
"pages": [
|
|
||||||
"page/index",
|
|
||||||
"page/tip"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"app-side": {
|
|
||||||
"path": "app-side/index"
|
|
||||||
},
|
|
||||||
"setting": {
|
|
||||||
"path": "setting/index"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"platforms": [
|
"targets": {
|
||||||
{
|
"default": {
|
||||||
"st": "s",
|
"module": {
|
||||||
"dw": 390
|
"page": {
|
||||||
|
"pages": ["page/index", "page/tip"]
|
||||||
|
},
|
||||||
|
"app-side": {
|
||||||
|
"path": "app-side/index"
|
||||||
|
},
|
||||||
|
"setting": {
|
||||||
|
"path": "setting/index"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"platforms": [
|
||||||
|
{
|
||||||
|
"st": "s",
|
||||||
|
"dw": 390
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"st": "r",
|
||||||
|
"dw": 390
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
}
|
"i18n": {
|
||||||
},
|
"en-US": {
|
||||||
"i18n": {
|
"appName": "TOTPFit"
|
||||||
"en-US": {
|
}
|
||||||
"appName": "TOTPFit"
|
},
|
||||||
}
|
"defaultLanguage": "en-US"
|
||||||
},
|
}
|
||||||
"defaultLanguage": "en-US"
|
|
||||||
}
|
|
||||||
|
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 21 KiB |
BIN
docs/assets/image.jpg
Normal file
After Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 27 KiB |
@ -6,7 +6,7 @@ To add 2FA TOTP records using 2FA TOTP QR-Codes, you must scan QR-Code of servic
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
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"_:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@ -14,24 +14,41 @@ Then press OK, record will appear on page
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
### 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 records 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:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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"_:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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.
|
### If you use Proton Authenticator
|
||||||
|
|
||||||
|
To add 2FA TOTP records from Proton Authenticator you must go to settings and press "Export":
|
||||||
|

|
||||||
|
|
||||||
|
After this save file and open it on text editor:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Copy all stuff from file at clipboard and import in application:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Then press OK, records will appear on page:
|
||||||
|
|
||||||
|

|
||||||
|
BIN
docs/guides/how-to-add-totps/photo_2025-08-08_17-10-27.jpg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
docs/guides/how-to-add-totps/photo_2025-08-08_17-10-35.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
docs/guides/how-to-add-totps/photo_2025-08-08_17-10-38.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
docs/guides/how-to-add-totps/photo_2025-08-08_17-10-41.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
docs/guides/how-to-add-totps/Снимок экрана_20250808_170854.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
docs/guides/how-to-add-totps/Снимок экрана_20250808_170912.png
Normal file
After Width: | Height: | Size: 37 KiB |
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 21 KiB |
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"checkJs": true
|
"checkJs": true
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules", "**/node_modules/*"],
|
"exclude": ["node_modules", "**/node_modules/*"],
|
||||||
"files": ["node_modules/@zeppos/device-types/dist/index.d.ts"]
|
"files": ["node_modules/@zeppos/device-types/dist/index.d.ts"]
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,37 @@
|
|||||||
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, "");
|
||||||
const normalizedHexInput = normalizedInput.replace(/0x/g, "").toLowerCase();
|
const normalizedHexInput = normalizedInput.replace(/0x/g, "").toLowerCase();
|
||||||
if (isHex(normalizedHexInput)) {
|
if (isHex(normalizedHexInput)) {
|
||||||
return Buffer.from(normalizedHexInput, "hex");
|
return Buffer.from(normalizedHexInput, "hex");
|
||||||
} else {
|
} else {
|
||||||
return Buffer.from(normalizedInput, "base64");
|
return Buffer.from(normalizedInput, "base64");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isHex(string) {
|
export function isHex(string) {
|
||||||
let result = true;
|
let result = true;
|
||||||
for (const char of string) {
|
for (const char of string) {
|
||||||
if (!((char >= "a" && char <= "f") || (char >= "0" && char <= "9"))) {
|
if (!((char >= "a" && char <= "f") || (char >= "0" && char <= "9"))) {
|
||||||
result = false;
|
result = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bufferLeToBeHex(buffer) {
|
export function bufferLeToBeHex(buffer) {
|
||||||
let output = "";
|
let output = "";
|
||||||
for (const v of buffer) {
|
for (const v of buffer) {
|
||||||
const hex = v.toString(16);
|
const hex = v.toString(16);
|
||||||
if (hex.length === 1) {
|
if (hex.length === 1) {
|
||||||
output = "0" + hex + output;
|
output = "0" + hex + output;
|
||||||
} else {
|
} else {
|
||||||
output = hex + output;
|
output = hex + output;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
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(" ");
|
||||||
|
@ -1,132 +1,132 @@
|
|||||||
import { decodeVarint } from "./varintUtils";
|
import { decodeVarint } from "./varintUtils";
|
||||||
|
|
||||||
export class BufferReader {
|
export class BufferReader {
|
||||||
constructor(buffer) {
|
constructor(buffer) {
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
}
|
|
||||||
|
|
||||||
readVarInt() {
|
|
||||||
const result = decodeVarint(this.buffer, this.offset);
|
|
||||||
this.offset += result.length;
|
|
||||||
|
|
||||||
return result.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
readBuffer(length) {
|
|
||||||
this.checkByte(length);
|
|
||||||
const result = this.buffer.slice(this.offset, this.offset + length);
|
|
||||||
this.offset += length;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// gRPC has some additional header - remove it
|
|
||||||
trySkipGrpcHeader() {
|
|
||||||
const backupOffset = this.offset;
|
|
||||||
|
|
||||||
if (this.buffer[this.offset] === 0 && this.leftBytes() >= 5) {
|
|
||||||
this.offset++;
|
|
||||||
const length = this.buffer.readInt32BE(this.offset);
|
|
||||||
this.offset += 4;
|
|
||||||
|
|
||||||
if (length > this.leftBytes()) {
|
|
||||||
// Something is wrong, revert
|
|
||||||
this.offset = backupOffset;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
leftBytes() {
|
readVarInt() {
|
||||||
return this.buffer.length - this.offset;
|
const result = decodeVarint(this.buffer, this.offset);
|
||||||
}
|
this.offset += result.length;
|
||||||
|
|
||||||
checkByte(length) {
|
return result.value;
|
||||||
const bytesAvailable = this.leftBytes();
|
|
||||||
if (length > bytesAvailable) {
|
|
||||||
throw new Error(
|
|
||||||
"Not enough bytes left. Requested: " +
|
|
||||||
length +
|
|
||||||
" left: " +
|
|
||||||
bytesAvailable
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
checkpoint() {
|
readBuffer(length) {
|
||||||
this.savedOffset = this.offset;
|
this.checkByte(length);
|
||||||
}
|
const result = this.buffer.slice(this.offset, this.offset + length);
|
||||||
|
this.offset += length;
|
||||||
|
|
||||||
resetToCheckpoint() {
|
return result;
|
||||||
this.offset = this.savedOffset;
|
}
|
||||||
}
|
|
||||||
|
// gRPC has some additional header - remove it
|
||||||
|
trySkipGrpcHeader() {
|
||||||
|
const backupOffset = this.offset;
|
||||||
|
|
||||||
|
if (this.buffer[this.offset] === 0 && this.leftBytes() >= 5) {
|
||||||
|
this.offset++;
|
||||||
|
const length = this.buffer.readInt32BE(this.offset);
|
||||||
|
this.offset += 4;
|
||||||
|
|
||||||
|
if (length > this.leftBytes()) {
|
||||||
|
// Something is wrong, revert
|
||||||
|
this.offset = backupOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
leftBytes() {
|
||||||
|
return this.buffer.length - this.offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkByte(length) {
|
||||||
|
const bytesAvailable = this.leftBytes();
|
||||||
|
if (length > bytesAvailable) {
|
||||||
|
throw new Error(
|
||||||
|
"Not enough bytes left. Requested: " +
|
||||||
|
length +
|
||||||
|
" left: " +
|
||||||
|
bytesAvailable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkpoint() {
|
||||||
|
this.savedOffset = this.offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
resetToCheckpoint() {
|
||||||
|
this.offset = this.savedOffset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TYPES = {
|
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) {
|
||||||
const reader = new BufferReader(buffer);
|
const reader = new BufferReader(buffer);
|
||||||
const parts = [];
|
const parts = [];
|
||||||
reader.trySkipGrpcHeader();
|
reader.trySkipGrpcHeader();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (reader.leftBytes() > 0) {
|
while (reader.leftBytes() > 0) {
|
||||||
reader.checkpoint();
|
reader.checkpoint();
|
||||||
|
|
||||||
const byteRange = [reader.offset];
|
const byteRange = [reader.offset];
|
||||||
const indexType = parseInt(reader.readVarInt().toString());
|
const indexType = parseInt(reader.readVarInt().toString());
|
||||||
const type = indexType & 0b111;
|
const type = indexType & 0b111;
|
||||||
const index = indexType >> 3;
|
const index = indexType >> 3;
|
||||||
|
|
||||||
let value;
|
let value;
|
||||||
if (type === TYPES.VARINT) {
|
if (type === TYPES.VARINT) {
|
||||||
value = reader.readVarInt().toString();
|
value = reader.readVarInt().toString();
|
||||||
} else if (type === TYPES.LENDELIM) {
|
} else if (type === TYPES.LENDELIM) {
|
||||||
const length = parseInt(reader.readVarInt().toString());
|
const length = parseInt(reader.readVarInt().toString());
|
||||||
value = reader.readBuffer(length);
|
value = reader.readBuffer(length);
|
||||||
} else if (type === TYPES.FIXED32) {
|
} else if (type === TYPES.FIXED32) {
|
||||||
value = reader.readBuffer(4);
|
value = reader.readBuffer(4);
|
||||||
} else if (type === TYPES.FIXED64) {
|
} else if (type === TYPES.FIXED64) {
|
||||||
value = reader.readBuffer(8);
|
value = reader.readBuffer(8);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Unknown type: " + type);
|
throw new Error("Unknown type: " + type);
|
||||||
}
|
}
|
||||||
byteRange.push(reader.offset);
|
byteRange.push(reader.offset);
|
||||||
|
|
||||||
parts.push({
|
parts.push({
|
||||||
byteRange,
|
byteRange,
|
||||||
index,
|
index,
|
||||||
type,
|
type,
|
||||||
value
|
value,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
reader.resetToCheckpoint();
|
||||||
|
console.log(err);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
reader.resetToCheckpoint();
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
parts,
|
parts,
|
||||||
leftOver: reader.readBuffer(reader.leftBytes())
|
leftOver: reader.readBuffer(reader.leftBytes()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function typeToString(type, subType) {
|
export function typeToString(type, subType) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TYPES.VARINT:
|
case TYPES.VARINT:
|
||||||
return "varint";
|
return "varint";
|
||||||
case TYPES.LENDELIM:
|
case TYPES.LENDELIM:
|
||||||
return subType || "len_delim";
|
return subType || "len_delim";
|
||||||
case TYPES.FIXED32:
|
case TYPES.FIXED32:
|
||||||
return "fixed32";
|
return "fixed32";
|
||||||
case TYPES.FIXED64:
|
case TYPES.FIXED64:
|
||||||
return "fixed64";
|
return "fixed64";
|
||||||
default:
|
default:
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
export function decodeVarint(buffer, offset) {
|
export function decodeVarint(buffer, offset) {
|
||||||
let res = 0;
|
let res = 0;
|
||||||
let shift = 0;
|
let shift = 0;
|
||||||
let byte = 0;
|
let byte = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (offset >= buffer.length) {
|
if (offset >= buffer.length) {
|
||||||
throw new RangeError("Index out of bound decoding varint");
|
throw new RangeError("Index out of bound decoding varint");
|
||||||
}
|
}
|
||||||
|
|
||||||
byte = buffer[offset++];
|
byte = buffer[offset++];
|
||||||
|
|
||||||
const multiplier = 2 ** shift;
|
const multiplier = 2 ** shift;
|
||||||
const thisByteValue = (byte & 0x7f) * multiplier;
|
const thisByteValue = (byte & 0x7f) * multiplier;
|
||||||
shift += 7;
|
shift += 7;
|
||||||
res = res + thisByteValue;
|
res = res + thisByteValue;
|
||||||
} while (byte >= 0x80);
|
} while (byte >= 0x80);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
value: res,
|
value: res,
|
||||||
length: shift / 7
|
length: shift / 7,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,59 @@
|
|||||||
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
|
||||||
* @param {string} secret base32 encoded string
|
* @param {string} secret base32 encoded string
|
||||||
* @param {number} [digits=6] number of digits in OTP token
|
* @param {number} [digits=6] number of digits in OTP token
|
||||||
* @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(
|
||||||
const bSecret = new Uint8Array(decode(secret).match(/.{1,2}/g).map(chunk => parseInt(chunk, 16))); //confirmed
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get OTP based on current time
|
* get OTP based on current time
|
||||||
* @param {string} secret base32 encoded string
|
* @param {string} secret base32 encoded string
|
||||||
* @param {number} [digits=6] digits in OTP
|
* @param {number} [digits=6] digits in OTP
|
||||||
* @param {number} [time=Date.now()] time for counter (default unix time epoch)
|
* @param {number} [time=Date.now()] time for counter (default unix time epoch)
|
||||||
* @param {number} [fetchTime=30] period of token in seconds
|
* @param {number} [fetchTime=30] period of token in seconds
|
||||||
* @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)
|
||||||
* @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);
|
||||||
|
}
|
||||||
|
@ -1,60 +1,62 @@
|
|||||||
export function decode(base32) {
|
export function decode(base32) {
|
||||||
for (
|
for (
|
||||||
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
|
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
|
||||||
bits = "",
|
bits = "",
|
||||||
hex = "",
|
hex = "",
|
||||||
i = 0;
|
i = 0;
|
||||||
i < base32.length;
|
i < base32.length;
|
||||||
i++
|
i++
|
||||||
) {
|
) {
|
||||||
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
|
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
|
||||||
bits += leftpad(val.toString(2), 5, "0");
|
bits += leftpad(val.toString(2), 5, "0");
|
||||||
}
|
}
|
||||||
for (i = 0; i + 4 <= bits.length; i += 4) {
|
for (i = 0; i + 4 <= bits.length; i += 4) {
|
||||||
var chunk = bits.substr(i, 4);
|
var chunk = bits.substr(i, 4);
|
||||||
hex += parseInt(chunk, 2).toString(16);
|
hex += parseInt(chunk, 2).toString(16);
|
||||||
}
|
}
|
||||||
return hex;
|
return hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
function leftpad(str, len, pad) {
|
function leftpad(str, len, pad) {
|
||||||
return (
|
return (
|
||||||
len + 1 >= str.length &&
|
len + 1 >= str.length &&
|
||||||
(str = new Array(len + 1 - str.length).join(pad) + str),
|
(str = new Array(len + 1 - str.length).join(pad) + str),
|
||||||
str
|
str
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@ -77,4 +79,4 @@ export function base64decode(base64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result.slice(0, j);
|
return result.slice(0, j);
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,68 @@
|
|||||||
import { getHOTP } from "./OTPGenerator.js"
|
import { getHOTP } from "./OTPGenerator.js";
|
||||||
/**
|
/**
|
||||||
* TOTP instance
|
* TOTP instance
|
||||||
*/
|
*/
|
||||||
export class TOTP {
|
export class TOTP {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {string} secret base32 encoded string
|
* @param {string} secret base32 encoded string
|
||||||
* @param {string} issuer issuer of TOTP
|
* @param {string} issuer issuer of TOTP
|
||||||
* @param {string} client client of TOTP
|
* @param {string} client client of TOTP
|
||||||
* @param {number} [digits=6] number of digits in OTP token
|
* @param {number} [digits=6] number of digits in OTP token
|
||||||
* @param {number} [fetchTime=30] period of token in seconds
|
* @param {number} [fetchTime=30] period of token in seconds
|
||||||
* @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),
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {number} time time for counter (default unix time epoch)
|
* @param {number} time time for counter (default unix time epoch)
|
||||||
* @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(
|
||||||
const expireTime = time +
|
Math.floor(unixTime),
|
||||||
|
this.secret,
|
||||||
|
this.digits,
|
||||||
|
this.hashType,
|
||||||
|
);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,14 +71,14 @@ export class TOTP {
|
|||||||
*/
|
*/
|
||||||
export class OTP {
|
export class OTP {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {string} otp OTP string
|
* @param {string} otp OTP string
|
||||||
* @param {number} createdTime time in unix epoch created OTP
|
* @param {number} createdTime time in unix epoch created 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
package.json
@ -1,15 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "totpfit",
|
"name": "totpfit",
|
||||||
"version": "1.3",
|
"version": "1.4.0",
|
||||||
"description": "Another 2FAuthenticator based on TOTP for Zepp Amazfit GTS 4",
|
"description": "Another 2FAuthenticator based on TOTP for Zepp OS with Google Authenticator and Proton Authenticator support",
|
||||||
"main": "app.js",
|
"main": "app.js",
|
||||||
"author": "Lisoveliy",
|
"author": "Lisoveliy",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@zeppos/device-types": "^3.0.0"
|
"@zeppos/device-types": "^3.0.0",
|
||||||
},
|
"prettier": "3.6.2"
|
||||||
"dependencies": {
|
},
|
||||||
"@zeppos/zml": "^0.0.27",
|
"dependencies": {
|
||||||
"jssha": "^3.3.1"
|
"@zeppos/zml": "^0.0.27",
|
||||||
}
|
"jssha": "^3.3.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,34 +2,37 @@ import { RenderAddButton } from "./render/totpRenderer";
|
|||||||
import { initLoop } from "./render/index/renderer";
|
import { initLoop } from "./render/index/renderer";
|
||||||
import { BasePage } from "@zeppos/zml/base-page";
|
import { BasePage } from "@zeppos/zml/base-page";
|
||||||
import { LocalStorage } from "@zos/storage";
|
import { LocalStorage } from "@zos/storage";
|
||||||
|
import { setPageBrightTime } from "@zos/display";
|
||||||
|
|
||||||
const app = getApp();
|
const app = getApp();
|
||||||
|
|
||||||
|
const brightTimeMs = 300000;
|
||||||
|
|
||||||
let waitForFetch = true;
|
let waitForFetch = true;
|
||||||
|
|
||||||
Page(
|
Page(
|
||||||
BasePage({
|
BasePage({
|
||||||
onInit() {
|
onInit() {
|
||||||
|
setPageBrightTime({ brightTime: brightTimeMs });
|
||||||
this.getTOTPData()
|
this.getTOTPData()
|
||||||
.then((x) => {
|
.then((x) => {
|
||||||
app._options.globalData.TOTPS = JSON.parse(x) ?? [];
|
app._options.globalData.TOTPS = JSON.parse(x) ?? [];
|
||||||
|
|
||||||
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(`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 +59,5 @@ Page(
|
|||||||
method: "totps",
|
method: "totps",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
@ -22,15 +22,16 @@ function renderContainers(buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const renderData = [];
|
const renderData = [];
|
||||||
|
|
||||||
function renderTOTPs(buffer) {
|
function renderTOTPs(buffer) {
|
||||||
for (let i = 0; i < buffer.length; i++) {
|
for (let i = 0; i < buffer.length; i++) {
|
||||||
let otpData = TOTP.copy(buffer[i]).getOTP();
|
let otpData = TOTP.copy(buffer[i]).getOTP();
|
||||||
renderData[i] = {
|
renderData[i] = {
|
||||||
OTP: RenderOTPValue(i, otpData.otp),
|
OTP: RenderOTPValue(i, formatOTP(otpData.otp)),
|
||||||
expireBar: RenderExpireBar(
|
expireBar: RenderExpireBar(
|
||||||
i,
|
i,
|
||||||
otpData.createdTime,
|
otpData.createdTime,
|
||||||
buffer[i].fetchTime
|
buffer[i].fetchTime,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@ -38,7 +39,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, {
|
||||||
@ -49,9 +50,15 @@ function renderTOTPs(buffer) {
|
|||||||
if (otpData.expireTime < Date.now()) {
|
if (otpData.expireTime < Date.now()) {
|
||||||
otpData = TOTP.copy(buffer[i]).getOTP();
|
otpData = TOTP.copy(buffer[i]).getOTP();
|
||||||
renderData[i].OTP.setProperty(prop.MORE, {
|
renderData[i].OTP.setProperty(prop.MORE, {
|
||||||
text: otpData.otp,
|
text: formatOTP(otpData.otp),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatOTP(otp) {
|
||||||
|
if (otp.length === 6) return `${otp.substring(0, 3)} ${otp.substring(3)}`;
|
||||||
|
|
||||||
|
return otp;
|
||||||
|
}
|
||||||
|
@ -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,
|
||||||
|
@ -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",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
50
setting/consts.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
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 otpauth-migration://" +
|
||||||
|
"link from Google Authenticator Migration QR-Code or Proton authenticator Export JSON string." +
|
||||||
|
"If you have a questions - check instruction below!",
|
||||||
|
totpRecordsHint: "TOTP records:",
|
||||||
|
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)://",
|
||||||
|
},
|
||||||
|
renameButtons: {
|
||||||
|
rename: "Rename",
|
||||||
|
renameIssuer: "Rename Issuer",
|
||||||
|
renameClient: "Rename Client",
|
||||||
|
},
|
||||||
|
saveButton: {
|
||||||
|
label: "Save",
|
||||||
|
},
|
||||||
|
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`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
348
setting/index.js
@ -1,75 +1,178 @@
|
|||||||
import { getTOTPByLink } from "./utils/queryParser.js";
|
import { getTOTPByLink } from "./utils/queryParser.js";
|
||||||
|
import { createTOTPCard } from "./ui/card.js";
|
||||||
|
import { colors, content } from "./consts.js";
|
||||||
|
|
||||||
let _props = null;
|
let _props = null;
|
||||||
|
let editingIndex = -1;
|
||||||
|
let tempIssuer = "";
|
||||||
|
let tempClient = "";
|
||||||
|
let errorMessage = "";
|
||||||
|
|
||||||
const colors = {
|
function updateStorage(storage) {
|
||||||
bg: "#101010",
|
_props.settingsStorage.setItem("TOTPs", JSON.stringify(storage));
|
||||||
linkBg: "#ffffffc0",
|
}
|
||||||
secondaryBg: "#282828",
|
|
||||||
text: "#fafafa",
|
function GetTOTPList(storage) {
|
||||||
alert: "#ad3c23",
|
return storage.map((element, index) => {
|
||||||
notify: "#555555",
|
return createTOTPCard({
|
||||||
bigText: "#fafafa"
|
element,
|
||||||
};
|
index,
|
||||||
|
storage,
|
||||||
|
isEditing: editingIndex === index,
|
||||||
|
tempIssuer,
|
||||||
|
tempClient,
|
||||||
|
onIssuerChange: (val) => {
|
||||||
|
tempIssuer = val;
|
||||||
|
},
|
||||||
|
onClientChange: (val) => {
|
||||||
|
tempClient = val;
|
||||||
|
},
|
||||||
|
onRename: () => {
|
||||||
|
editingIndex = index;
|
||||||
|
tempIssuer = element.issuer;
|
||||||
|
tempClient = element.client;
|
||||||
|
_props.settingsStorage.setItem("requestUpdate", Math.random());
|
||||||
|
},
|
||||||
|
onSave: () => {
|
||||||
|
storage[index].issuer = tempIssuer;
|
||||||
|
storage[index].client = tempClient;
|
||||||
|
editingIndex = -1;
|
||||||
|
updateStorage(storage);
|
||||||
|
},
|
||||||
|
onDelete: () => {
|
||||||
|
storage.splice(index, 1);
|
||||||
|
updateStorage(storage);
|
||||||
|
},
|
||||||
|
onMoveUp: () => {
|
||||||
|
if (index > 0) {
|
||||||
|
[storage[index], storage[index - 1]] = [
|
||||||
|
storage[index - 1],
|
||||||
|
storage[index],
|
||||||
|
];
|
||||||
|
updateStorage(storage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onMoveDown: () => {
|
||||||
|
if (index < storage.length - 1) {
|
||||||
|
[storage[index], storage[index + 1]] = [
|
||||||
|
storage[index + 1],
|
||||||
|
storage[index],
|
||||||
|
];
|
||||||
|
updateStorage(storage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
isEditInProgress: editingIndex !== -1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
paragraph: true,
|
? Text(
|
||||||
align: "center",
|
{
|
||||||
style: {
|
paragraph: true,
|
||||||
paddingTop: "10px",
|
align: "center",
|
||||||
marginBottom: "10px",
|
style: {
|
||||||
color: colors.text,
|
paddingTop: "10px",
|
||||||
fontSize: 16,
|
marginBottom: "10px",
|
||||||
verticalAlign: "middle",
|
color: colors.text,
|
||||||
},
|
fontSize: 16,
|
||||||
},
|
verticalAlign: "middle",
|
||||||
"For add a 2FA TOTP record you must have otpauth:// link or otpauth-migration:// link from Google Authenticator Migration QR-Code"
|
},
|
||||||
) : null;
|
},
|
||||||
|
content.addTotpsHint,
|
||||||
|
)
|
||||||
|
: 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);
|
try {
|
||||||
if (link == null) {
|
errorMessage = "";
|
||||||
console.log("link is invalid");
|
let link = getTOTPByLink(changes);
|
||||||
return;
|
|
||||||
|
if (Array.isArray(link)) {
|
||||||
|
storage.push(...link);
|
||||||
|
} else {
|
||||||
|
storage.push(link);
|
||||||
|
}
|
||||||
|
updateStorage(storage);
|
||||||
|
} catch (e) {
|
||||||
|
errorMessage = e.message;
|
||||||
|
updateStorage(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(link))
|
|
||||||
storage.push(...link);
|
|
||||||
else storage.push(link);
|
|
||||||
|
|
||||||
updateStorage(storage);
|
|
||||||
},
|
},
|
||||||
labelStyle: {
|
labelStyle: {
|
||||||
backgroundColor: colors.notify,
|
backgroundColor: colors.notify,
|
||||||
display: "flex",
|
display: "flex",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
margin: "10px",
|
marginBottom: "10px",
|
||||||
|
marginLeft: "10px",
|
||||||
|
marginRight: "10px",
|
||||||
fontSize: "20px",
|
fontSize: "20px",
|
||||||
color: colors.text,
|
color: colors.text,
|
||||||
borderRadius: "5px",
|
borderRadius: "5px",
|
||||||
position: storage.length < 1 ? "absolute" : null, //TODO: Сделать что-то с этим кошмаром
|
height: "45px",
|
||||||
bottom: storage.length < 1 ? "0px" : null,
|
|
||||||
left: storage.length < 1 ? "0px" : null,
|
|
||||||
right: storage.length < 1 ? "0px" : null
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
var body = Section(
|
const errorText = errorMessage
|
||||||
|
? Text(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
color: colors.alert,
|
||||||
|
textAlign: "center",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
errorMessage,
|
||||||
|
)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const bottomContainer = View(
|
||||||
{
|
{
|
||||||
style: {
|
style: {
|
||||||
backgroundColor: colors.bg,
|
backgroundColor: colors.bg,
|
||||||
minHeight: "100vh",
|
},
|
||||||
|
},
|
||||||
|
[
|
||||||
|
View(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
marginTop: "10px",
|
||||||
|
marginBottom: "10px",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Link(
|
||||||
|
{
|
||||||
|
source: content.instructionLink.source,
|
||||||
|
},
|
||||||
|
content.instructionLink.label,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
errorText,
|
||||||
|
createButton,
|
||||||
|
].filter(Boolean),
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageContainer = View(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
backgroundColor: colors.bg,
|
||||||
|
height: "100vh",
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
justifyContent: "space-between",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
@ -79,142 +182,43 @@ AppSettingsPage({
|
|||||||
textAlign: "center",
|
textAlign: "center",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
storage.length < 1 ? addTOTPsHint : Text(
|
[
|
||||||
{
|
storage.length < 1
|
||||||
align: "center",
|
? addTOTPsHint
|
||||||
paragraph: true,
|
: Text(
|
||||||
style: {
|
{
|
||||||
marginBottom: "10px",
|
align: "center",
|
||||||
color: colors.bigText,
|
paragraph: true,
|
||||||
fontSize: 23,
|
style: {
|
||||||
fontWeight: "500",
|
marginTop: "10px",
|
||||||
verticalAlign: "middle",
|
marginBottom: "10px",
|
||||||
},
|
color: colors.bigText,
|
||||||
},
|
fontSize: 23,
|
||||||
"TOTP records:"
|
fontWeight: "500",
|
||||||
)
|
verticalAlign: "middle",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
content.totpRecordsHint,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
...totpEntrys,
|
|
||||||
createButton,
|
|
||||||
View({
|
|
||||||
style: {
|
|
||||||
display: "flex",
|
|
||||||
justifyContent: "center"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Link({
|
|
||||||
source: "https://github.com/Lisoveliy/totpfit/blob/main/docs/guides/how-to-add-totps/README.md"
|
|
||||||
},
|
|
||||||
"Instruction | Report issue (GitHub)")
|
|
||||||
),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
return body;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
function GetTOTPList(storage) {
|
|
||||||
let totpEntrys = [];
|
|
||||||
let counter = 0;
|
|
||||||
storage.forEach((element) => {
|
|
||||||
const elementId = counter;
|
|
||||||
const textInput = TextInput({
|
|
||||||
placeholder: "otpauth(-migration)://",
|
|
||||||
label: "Change TOTP link",
|
|
||||||
onChange: (changes) => {
|
|
||||||
try {
|
|
||||||
let link = getTOTPByLink(changes);
|
|
||||||
if (Array.isArray(link))
|
|
||||||
return;
|
|
||||||
|
|
||||||
storage[elementId] = link;
|
|
||||||
updateStorage(storage);
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
labelStyle: {
|
|
||||||
backgroundColor: colors.notify,
|
|
||||||
textAlign: "center",
|
|
||||||
display: "flex",
|
|
||||||
alignItems: "center",
|
|
||||||
justifyContent: "center",
|
|
||||||
margin: "10px",
|
|
||||||
flexGrow: 1,
|
|
||||||
fontSize: "20px",
|
|
||||||
color: colors.text,
|
|
||||||
borderRadius: "5px",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const textBig = Text(
|
|
||||||
{
|
|
||||||
align: "center",
|
|
||||||
style: {
|
|
||||||
color: colors.text,
|
|
||||||
fontSize: "18px",
|
|
||||||
fontWeight: "500"
|
|
||||||
},
|
|
||||||
paragraph: true,
|
|
||||||
},
|
|
||||||
`${element.issuer}: ${element.client}`
|
|
||||||
);
|
|
||||||
const delButton = Button({
|
|
||||||
onClick: () => {
|
|
||||||
storage = storage.filter(
|
|
||||||
(x) => storage.indexOf(x) != elementId
|
|
||||||
);
|
|
||||||
updateStorage(storage);
|
|
||||||
},
|
|
||||||
style: {
|
|
||||||
backgroundColor: colors.alert,
|
|
||||||
fontSize: "18px",
|
|
||||||
color: colors.text,
|
|
||||||
height: "fit-content",
|
|
||||||
margin: "10px",
|
|
||||||
},
|
|
||||||
label: "Delete",
|
|
||||||
});
|
|
||||||
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(
|
|
||||||
{
|
|
||||||
style: {
|
|
||||||
textAlign: "center",
|
|
||||||
backgroundColor: colors.secondaryBg,
|
|
||||||
//border: "2px solid white",
|
|
||||||
borderRadius: "5px",
|
|
||||||
margin: "10px",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
[
|
|
||||||
textBig,
|
|
||||||
text,
|
|
||||||
View(
|
View(
|
||||||
{
|
{
|
||||||
style: {
|
style: {
|
||||||
display: "grid",
|
height: "100%",
|
||||||
gridTemplateColumns: "1fr 100px",
|
overflowX: "hidden",
|
||||||
|
overflowY: "auto",
|
||||||
|
backgroundColor: colors.bg,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
[textInput, delButton]
|
[...totpEntrys],
|
||||||
),
|
),
|
||||||
]
|
|
||||||
|
bottomContainer,
|
||||||
|
],
|
||||||
);
|
);
|
||||||
totpEntrys.push({ text: text, view: view });
|
|
||||||
counter++;
|
|
||||||
});
|
|
||||||
|
|
||||||
return totpEntrys.map((x) => x.view);
|
return pageContainer;
|
||||||
}
|
},
|
||||||
|
});
|
||||||
function updateStorage(storage) {
|
|
||||||
_props.settingsStorage.setItem("TOTPs", JSON.stringify(storage));
|
|
||||||
}
|
|
||||||
|
187
setting/ui/card.js
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
import { colors, content } from "../consts";
|
||||||
|
|
||||||
|
export function createTOTPCard({
|
||||||
|
element,
|
||||||
|
index,
|
||||||
|
storage,
|
||||||
|
isEditing,
|
||||||
|
tempIssuer,
|
||||||
|
tempClient,
|
||||||
|
onRename,
|
||||||
|
onSave,
|
||||||
|
onDelete,
|
||||||
|
onMoveUp,
|
||||||
|
onMoveDown,
|
||||||
|
onIssuerChange,
|
||||||
|
onClientChange,
|
||||||
|
isEditInProgress,
|
||||||
|
}) {
|
||||||
|
const infoView = View(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
alignItems: "flex-start",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isEditing
|
||||||
|
? [
|
||||||
|
TextInput({
|
||||||
|
label: content.renameButtons.renameIssuer,
|
||||||
|
value: tempIssuer,
|
||||||
|
onChange: onIssuerChange,
|
||||||
|
labelStyle: {
|
||||||
|
backgroundColor: colors.notify,
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
margin: "10px",
|
||||||
|
fontSize: "20px",
|
||||||
|
color: colors.text,
|
||||||
|
borderRadius: "5px",
|
||||||
|
height: "40px",
|
||||||
|
width: "200px",
|
||||||
|
},
|
||||||
|
subStyle: {
|
||||||
|
display: "none",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
TextInput({
|
||||||
|
label: content.renameButtons.renameClient,
|
||||||
|
value: tempClient,
|
||||||
|
onChange: onClientChange,
|
||||||
|
labelStyle: {
|
||||||
|
backgroundColor: colors.notify,
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
margin: "10px",
|
||||||
|
fontSize: "20px",
|
||||||
|
color: colors.text,
|
||||||
|
borderRadius: "5px",
|
||||||
|
height: "40px",
|
||||||
|
width: "200px",
|
||||||
|
},
|
||||||
|
subStyle: {
|
||||||
|
display: "none",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
Text(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
color: colors.text,
|
||||||
|
marginBottom: "2px",
|
||||||
|
fontWeight: "600",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
`Issuer: ${element.issuer}`,
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
{ style: { color: colors.text, fontWeight: "600" } },
|
||||||
|
`Client: ${element.client}`,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
const buttonsView = View(
|
||||||
|
{ style: { display: "flex", flexDirection: "row" } },
|
||||||
|
isEditing
|
||||||
|
? [
|
||||||
|
Button({
|
||||||
|
label: content.saveButton.label,
|
||||||
|
style: {
|
||||||
|
margin: "5px",
|
||||||
|
backgroundColor: "#28a745",
|
||||||
|
color: colors.text,
|
||||||
|
},
|
||||||
|
onClick: onSave,
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
Button({
|
||||||
|
label: content.renameButtons.rename,
|
||||||
|
style: {
|
||||||
|
margin: "5px",
|
||||||
|
backgroundColor: colors.notify,
|
||||||
|
color: colors.text,
|
||||||
|
},
|
||||||
|
onClick: onRename,
|
||||||
|
}),
|
||||||
|
!isEditInProgress
|
||||||
|
? Button({
|
||||||
|
label: content.deleteButton.label,
|
||||||
|
style: {
|
||||||
|
margin: "5px",
|
||||||
|
backgroundColor: colors.alert,
|
||||||
|
color: colors.text,
|
||||||
|
},
|
||||||
|
onClick: onDelete,
|
||||||
|
})
|
||||||
|
: null,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
const reorderView = View(
|
||||||
|
{ style: { display: "flex", flexDirection: "column" } },
|
||||||
|
[
|
||||||
|
Button({
|
||||||
|
label: "⬆",
|
||||||
|
disabled: index === 0,
|
||||||
|
style: {
|
||||||
|
margin: "2px",
|
||||||
|
color: colors.text,
|
||||||
|
backgroundColor: colors.notify,
|
||||||
|
},
|
||||||
|
onClick: onMoveUp,
|
||||||
|
}),
|
||||||
|
Button({
|
||||||
|
label: "⬇",
|
||||||
|
disabled: index === storage.length - 1,
|
||||||
|
style: {
|
||||||
|
margin: "2px",
|
||||||
|
color: colors.text,
|
||||||
|
backgroundColor: colors.notify,
|
||||||
|
},
|
||||||
|
onClick: onMoveDown,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
const mainContent = View({ style: { flexGrow: 1, padding: "5px" } }, [
|
||||||
|
infoView,
|
||||||
|
Text(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
color: colors.text,
|
||||||
|
fontSize: "14px",
|
||||||
|
marginTop: "5px",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
content.totpDescText.eval(
|
||||||
|
element.hashType,
|
||||||
|
element.digits,
|
||||||
|
element.fetchTime,
|
||||||
|
element.timeOffset,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
buttonsView,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return View(
|
||||||
|
{
|
||||||
|
style: {
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
backgroundColor: colors.secondaryBg,
|
||||||
|
borderRadius: "5px",
|
||||||
|
margin: "10px",
|
||||||
|
padding: "5px",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[mainContent, reorderView],
|
||||||
|
);
|
||||||
|
}
|
11
setting/utils/protonBackupExport.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export class ProtonBackupExport {
|
||||||
|
version;
|
||||||
|
entries = Array.of(ProtonTotpRecord);
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ProtonTotpRecord {
|
||||||
|
content = {
|
||||||
|
uri,
|
||||||
|
entry_type,
|
||||||
|
};
|
||||||
|
}
|
@ -1,114 +1,201 @@
|
|||||||
import { decodeProto, TYPES } from "../../lib/protobuf-decoder/protobufDecoder";
|
import { decodeProto, TYPES } from "../../lib/protobuf-decoder/protobufDecoder";
|
||||||
import { TOTP } from "../../lib/totp-quickjs";
|
import { TOTP } from "../../lib/totp-quickjs";
|
||||||
import { base64decode, encode } from "../../lib/totp-quickjs/base32decoder";
|
import { base64decode, encode } from "../../lib/totp-quickjs/base32decoder";
|
||||||
|
import { ProtonBackupExport } from "./protonBackupExport";
|
||||||
|
|
||||||
const otpauthScheme = "otpauth:/";
|
const otpauthScheme = "otpauth://";
|
||||||
const googleMigrationScheme = "otpauth-migration:/";
|
const googleMigrationScheme = "otpauth-migration://";
|
||||||
|
|
||||||
export function getTOTPByLink(link) {
|
export function getTOTPByLink(link) {
|
||||||
if (link.includes(otpauthScheme))
|
try {
|
||||||
return getByOtpauthScheme(link)
|
//proton export
|
||||||
if (link.includes(googleMigrationScheme))
|
const json = JSON.parse(link);
|
||||||
return getByGoogleMigrationScheme(link)
|
console.log(json);
|
||||||
|
return getByProtonBackup(json);
|
||||||
return null;
|
} catch (e) {
|
||||||
|
if (link.startsWith(googleMigrationScheme)) {
|
||||||
|
//google migration export
|
||||||
|
return getByGoogleMigrationScheme(link);
|
||||||
|
}
|
||||||
|
if (link.startsWith(otpauthScheme)) {
|
||||||
|
//otpauth export
|
||||||
|
return getByOtpauthScheme(link);
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
`Unsupported link type. Please use an otpauth:// or otpauth-migration:// link\n ERR: ${e}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHashType(algorithm) {
|
function getHashType(algorithm) {
|
||||||
if (algorithm == "SHA1") return "SHA-1";
|
if (algorithm == "SHA1") return "SHA-1";
|
||||||
if (algorithm == "SHA256") return "SHA-256";
|
if (algorithm == "SHA256") return "SHA-256";
|
||||||
if (algorithm == "SHA512") return "SHA-512";
|
if (algorithm == "SHA512") return "SHA-512";
|
||||||
else return "SHA-1";
|
else return "SHA-1";
|
||||||
|
}
|
||||||
|
|
||||||
|
function getByProtonBackup(protonjson) {
|
||||||
|
try {
|
||||||
|
if ("entries" in protonjson && protonjson.version == 1) {
|
||||||
|
//Is proton export?
|
||||||
|
console.log(1);
|
||||||
|
const protonBE = Object.assign(
|
||||||
|
new ProtonBackupExport(),
|
||||||
|
protonjson,
|
||||||
|
);
|
||||||
|
const res = protonBE.entries.map((x) => {
|
||||||
|
return getByOtpauthScheme(x.content.uri);
|
||||||
|
});
|
||||||
|
console.log(res);
|
||||||
|
return res;
|
||||||
|
} else throw new Error("use proton export backup with version: 1");
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
throw new Error(`Unsupported JSON type: ${e}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getByOtpauthScheme(link) {
|
function getByOtpauthScheme(link) {
|
||||||
try {
|
try {
|
||||||
let args = link.split("/", otpauthScheme.length);
|
let args = link.split("?");
|
||||||
let type = args[2]; //Returns 'hotp' or 'totp'
|
let path = args[0];
|
||||||
let issuer = args[3].split(":")[0]?.split("?")[0]; //Returns issuer
|
let params = args[1];
|
||||||
let client =
|
|
||||||
args[3].split(":")[1]?.split("?")[0] ??
|
|
||||||
args[3].split(":")[0]?.split("?")[0]; //Returns client
|
|
||||||
let secret = args[3].split("secret=")[1]?.split("&")[0]; //Returns secret
|
|
||||||
let period = args[3].split("period=")[1]?.split("&")[0]; //Returns period
|
|
||||||
let digits = args[3].split("digit=")[1]?.split("&")[0]; //Returns digits
|
|
||||||
let algorithm = args[3].split("algorithm=")[1]?.split("&")[0]; //Returns algorithm
|
|
||||||
let offset = args[3].split("offset=")[1]?.split("&")[0] ?? 0; //Returns offset
|
|
||||||
|
|
||||||
if (type.toLowerCase() != "totp")
|
let pathParts = path.split("/");
|
||||||
throw new Error("Type is not valid, requires 'TOTP'");
|
let type = pathParts[2]; //hotp or totp
|
||||||
|
let label = decodeURIComponent(pathParts[3]);
|
||||||
|
|
||||||
if (secret === undefined) throw new Error("Secret not defined");
|
let issuerFromLabel = label.includes(":") ? label.split(":")[0] : null;
|
||||||
|
let client = label.includes(":") ? label.split(":")[1].trim() : label;
|
||||||
|
client = decodeURIComponent(client);
|
||||||
|
|
||||||
if (issuer == client) {
|
let secret = params.match(/secret=([^&]*)/)?.[1];
|
||||||
issuer = args[3].split("issuer=")[1]?.split("&")[0];
|
let issuerFromParams = params.match(/issuer=([^&]*)/)?.[1];
|
||||||
}
|
|
||||||
|
|
||||||
issuer = decodeURIComponent(issuer);
|
let issuer = issuerFromParams
|
||||||
client = decodeURIComponent(client);
|
? decodeURIComponent(issuerFromParams)
|
||||||
|
: decodeURIComponent(issuerFromLabel);
|
||||||
|
if (!issuer) issuer = client;
|
||||||
|
|
||||||
return new TOTP(
|
let period = params.match(/period=([^&]*)/)?.[1];
|
||||||
secret,
|
let digits = params.match(/digits=([^&]*)/)?.[1];
|
||||||
issuer,
|
let algorithm = params.match(/algorithm=([^&]*)/)?.[1];
|
||||||
client,
|
let offset = params.match(/offset=([^&]*)/)?.[1] ?? 0;
|
||||||
Number(digits),
|
|
||||||
Number(period),
|
if (type.toLowerCase() != "totp")
|
||||||
Number(offset),
|
throw new Error("Type is not valid, requires 'TOTP'");
|
||||||
getHashType(algorithm)
|
|
||||||
);
|
if (!secret) throw new Error("Secret not defined");
|
||||||
} catch (err) {
|
|
||||||
console.log(err)
|
return new TOTP(
|
||||||
return null;
|
secret,
|
||||||
}
|
issuer,
|
||||||
|
client,
|
||||||
|
Number(digits) || 6,
|
||||||
|
Number(period) || 30,
|
||||||
|
Number(offset),
|
||||||
|
getHashType(algorithm),
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Failed to parse otpauth scheme:", err);
|
||||||
|
throw new Error(
|
||||||
|
`Invalid otpauth:// link. Please check the link and try again. ERR: ${err}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getByGoogleMigrationScheme(link) {
|
function getByGoogleMigrationScheme(link) {
|
||||||
|
try {
|
||||||
|
const data = link.split("data=")[1];
|
||||||
|
if (!data) return null;
|
||||||
|
|
||||||
let data = link.split("data=")[1]; //Returns base64 encoded data
|
const decodedData = decodeURIComponent(data);
|
||||||
data = decodeURIComponent(data);
|
const buffer = base64decode(decodedData);
|
||||||
let decode = base64decode(data);
|
const proto = decodeProto(buffer);
|
||||||
let proto = decodeProto(decode);
|
|
||||||
|
|
||||||
let protoTotps = [];
|
const totps = [];
|
||||||
|
const otpParameters = proto.parts.filter(
|
||||||
|
(p) => p.index === 1 && p.type === TYPES.LENDELIM,
|
||||||
|
);
|
||||||
|
|
||||||
proto.parts.forEach(part => {
|
otpParameters.forEach((part) => {
|
||||||
if (part.type == TYPES.LENDELIM) {
|
const totp = parseSingleMigrationEntry(part);
|
||||||
protoTotps.push(decodeProto(part.value));
|
if (totp) {
|
||||||
}
|
totps.push(totp);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let totps = [];
|
return totps.length > 0 ? totps : null;
|
||||||
protoTotps.forEach(x => {
|
} catch (err) {
|
||||||
let type = x.parts.filter(x => x.index == 6)[0]; //find type of OTP
|
console.log("Failed to parse Google Migration scheme:", err);
|
||||||
if (type.value !== '2') {
|
throw new Error(
|
||||||
console.log("ERR: it's a not TOTP record")
|
"Invalid otpauth-migration:// link. Failed to parse migration data.",
|
||||||
return;
|
);
|
||||||
}
|
}
|
||||||
let secret = x.parts.filter(x => x.index == 1)[0].value;
|
}
|
||||||
secret = encode(secret);
|
|
||||||
|
|
||||||
let name = bytesToString(x.parts.filter(x => x.index == 2)[0].value);
|
function parseSingleMigrationEntry(part) {
|
||||||
let issuer = bytesToString(x.parts.filter(x => x.index == 3)[0].value);
|
const totpProto = decodeProto(part.value);
|
||||||
|
const otpData = {};
|
||||||
|
|
||||||
totps.push(new TOTP(
|
const protoPartHandlers = {
|
||||||
secret,
|
1: (p) => {
|
||||||
issuer,
|
otpData.secret = encode(p.value);
|
||||||
name,
|
},
|
||||||
6,
|
2: (p) => {
|
||||||
30,
|
otpData.name = bytesToString(p.value);
|
||||||
0,
|
},
|
||||||
"SHA-1"
|
3: (p) => {
|
||||||
));
|
otpData.issuer = bytesToString(p.value);
|
||||||
});
|
},
|
||||||
|
4: (p) => {
|
||||||
|
otpData.algorithm = p.value;
|
||||||
|
},
|
||||||
|
5: (p) => {
|
||||||
|
otpData.digits = p.value;
|
||||||
|
},
|
||||||
|
6: (p) => {
|
||||||
|
otpData.type = p.value;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
return totps;
|
totpProto.parts.forEach((p) => {
|
||||||
|
const handler = protoPartHandlers[p.index];
|
||||||
|
if (handler) {
|
||||||
|
handler(p);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (otpData.type !== "2") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const digitsMap = { 1: 6, 2: 8 };
|
||||||
|
const algoMap = { 1: "SHA-1", 2: "SHA-256", 3: "SHA-512" };
|
||||||
|
|
||||||
|
const finalDigits = digitsMap[otpData.digits] || 6;
|
||||||
|
const finalAlgo = algoMap[otpData.algorithm] || "SHA-1";
|
||||||
|
const finalIssuer = otpData.issuer || otpData.name;
|
||||||
|
const finalName = otpData.name;
|
||||||
|
|
||||||
|
if (!otpData.secret || !finalName) {
|
||||||
|
throw new Error("Skipping record with missing secret or name.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TOTP(
|
||||||
|
otpData.secret,
|
||||||
|
finalIssuer,
|
||||||
|
finalName,
|
||||||
|
finalDigits,
|
||||||
|
30,
|
||||||
|
0,
|
||||||
|
finalAlgo,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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]);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|