Skip to content

Commit

Permalink
Fetch and update password
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisommore committed Mar 25, 2023
1 parent e598a15 commit f3fb2a8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typings/
.yarn-integrity

.yarn
.yarnrc.yml

# dotenv environment variables file
.env
Expand Down
55 changes: 44 additions & 11 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get_passwords } from "./polybase";
import { createRecord, getAllRecords, getRecordByUrl } from "./polybase";
import {
CurrentParams,
GetPasswords,
Expand All @@ -13,7 +13,7 @@ const current_params: CurrentParams = {
website: "",
};

let passwords_for_requested_website: Password[];
let passwords_for_requested_website: Data[];

chrome.tabs.onUpdated.addListener((id, change, tab) => {
const change_url = change.url?.replace(/\/+$/, "");
Expand All @@ -37,7 +37,7 @@ chrome.tabs.onUpdated.addListener((id, change, tab) => {
return (
e.password == current_params.password &&
e.username == current_params.username &&
e.website == params_website
e.url == params_website
);
});

Expand All @@ -47,23 +47,36 @@ chrome.tabs.onUpdated.addListener((id, change, tab) => {
{ active: true, currentWindow: true },
function (tabs) {
chrome.tabs.sendMessage(tabs?.[0].id ?? 0, new_message);
current_params.website = "";
current_params.username = "";
current_params.password = "";
}
);
}, 300);
}
}
});
export interface Data {
id: string;
password: string;
publicKey: PublicKey;
url: string;
username: string;
}

export interface PublicKey {
alg: string;
crv: string;
kty: string;
use: string;
x: string;
y: string;
}
chrome.runtime.onMessage.addListener(async (m: Message<any>, _, sendRes) => {
console.log("messag rec", m);

chrome.runtime.onMessage.addListener((m: Message<any>, _, sendRes) => {
if (m.key == MessageKey.GET_PASSWORDS) {
const body = m.body as GetPasswords;
const passwords = get_passwords();
passwords_for_requested_website = passwords.filter(
(e) => e.website == body.domain
);
const passwords = await getRecordByUrl(body.domain);
passwords_for_requested_website = passwords.data.map((e) => e.data);

sendRes(passwords_for_requested_website);
return true;
}
Expand All @@ -74,5 +87,25 @@ chrome.runtime.onMessage.addListener((m: Message<any>, _, sendRes) => {
current_params.website = body.website.replace(/\/+$/, "");
return true;
}
if (m.key == MessageKey.SAVE_PASSWORD) {
console.log(current_params.website);

const url_obj = new URL(current_params.website);
console.log("saving pasword", {
a: current_params.username,
b: current_params.password,
c: url_obj.hostname,
});

await createRecord(
current_params.username,
current_params.password,
url_obj.hostname
);
current_params.website = "";
current_params.username = "";
current_params.password = "";
return true;
}
return false;
});
21 changes: 20 additions & 1 deletion src/content_scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,27 @@ background-color:white;
-moz-box-shadow: 10px 10px 88px -8px rgba(0,0,0,1);
box-shadow: 10px 10px 88px -8px rgba(0,0,0,1);
">
Save to passwords? </div>`;
Save to passwords?
<div>
<button id="btn_yes">Yes</button>
<button id="btn_no">No</button>
</div>
</div>`;
chrome.runtime.onMessage.addListener((m: Message<any>, sender) => {
if (m.key == MessageKey.LOGIN_SUCCESS) {
document.getElementsByTagName("body")[0].appendChild(success_save);
console.log(document.getElementById("btn_yes"));

document.getElementById("btn_yes")?.addEventListener("click", () => {
const new_msg: Message<any> = {
key: MessageKey.SAVE_PASSWORD,
};
console.log("sending m", new_msg);
chrome.runtime.sendMessage(new_msg);
});
document.getElementById("btn_no")?.addEventListener("click", () => {
success_save.remove();
});
}
});

Expand All @@ -51,6 +68,8 @@ const on_load = () => {
let password_value = "";

const update_inputs = (e: Event) => {
console.log("update vale", "kek");

username_value = username_element?.value ?? "";
password_value = password_element?.value ?? "";
const new_msg: Message<CurrentParams> = {
Expand Down
55 changes: 20 additions & 35 deletions src/polybase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Password, WALLET_PRIV_KEY } from "./types";
import { Data, Password, WALLET_PRIV_KEY } from "./types";
import { Polybase } from "@polybase/client";
import { ethPersonalSign } from "@polybase/eth";
import {
Expand All @@ -14,25 +14,6 @@ chrome.storage.session.onChanged.addListener((e) => {
priv_key = e[WALLET_PRIV_KEY].newValue;
}
});
export const get_passwords = (): Password[] => {
return [
{
password: "1234",
username: "thisisommore",
website: "www.ommore.me",
},
{
password: "study karo",
username: "thisisommore",
website: "github.com",
},
{
password: "1234",
username: "thisisommoremax",
website: "github.com",
},
];
};

const hardKey = decodeFromString(
"95tUuJPAkFV5r60rV12uEYoFErTIEXG7am6tMWzcvcU=",
Expand All @@ -52,7 +33,7 @@ const setDb = (): Polybase => {
};
},
});
//const collectionReference = db.collection("passwords");
//const collectionReference = db.collection<Data>("passwords");
return db;
};

Expand Down Expand Up @@ -107,7 +88,7 @@ export const createRecord = async (
url: string
) => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

var firstPart: any = (Math.random() * 46656) | 0;
var secondPart: any = (Math.random() * 46656) | 0;
Expand All @@ -127,7 +108,7 @@ export const createRecord = async (

export const updatePassword = async (id: string, password: string) => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

var encryptedPass = await genPass(password);

Expand All @@ -138,7 +119,7 @@ export const updatePassword = async (id: string, password: string) => {

export const updateUsername = async (id: string, username: string) => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

const recordData = await collectionReference
.record(id)
Expand All @@ -147,36 +128,40 @@ export const updateUsername = async (id: string, username: string) => {

export const getRecordById = async (id: string) => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

const { data, block } = await collectionReference.record(id).get();
var pass = await decryptMergedPass(data["password"])
console.log(pass)
data["password"] = pass
var pass = await decryptMergedPass(data["password"]);
console.log(pass);
data["password"] = pass;
};

export const decryptMergedPass = async (encryptedPass : string) : Promise<string> => {
export const decryptMergedPass = async (
encryptedPass: string
): Promise<string> => {
var splitEncryptedPass = encryptedPass.split(" ");
var encryptedInterface: EncryptedDataAesCbc256 = {
version: "aes-cbc-256/symmetric",
nonce: decodeFromString(splitEncryptedPass[1], "base64"),
ciphertext: decodeFromString(splitEncryptedPass[0], "base64"),
};
var pass: string = await DecryptString(hardKey, encryptedInterface);
return pass
}
return pass;
};

export const getAllRecords = async () => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

const records = await collectionReference.get();
return records.data
return records.data;
};
export const getRecordByUrl = async (url: string) => {
var db = setDb();
const collectionReference = db.collection("passwords");
const collectionReference = db.collection<Data>("passwords");

const records = await collectionReference.where("url", "==", url).get();
console.log(records);
};

return records;
};
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export const WALLET_ENCRYPTED_KEY = "WALLET_ENCRYPTED";
export const WALLET_PRIV_KEY = "WALLET_PRIV_KEY";
export const ERR_WALLET_NOT_FOUND = new Error("Wallet not found");
export interface Data {
id: string;
password: string;
publicKey: PublicKey;
url: string;
username: string;
}

export interface PublicKey {
alg: string;
crv: string;
kty: string;
use: string;
x: string;
y: string;
}
export type Password = {
website: string;
username: string;
Expand All @@ -17,6 +32,7 @@ export enum MessageKey {
PARAMS_UPDATED,
LOGIN_SUCCESS,
GET_PASSWORDS,
SAVE_PASSWORD,
}

export type CurrentParams = {
Expand Down

0 comments on commit f3fb2a8

Please sign in to comment.