-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1244 from buttercup/feat/browser_api
Browser extension (v3) API
- Loading branch information
Showing
39 changed files
with
5,436 additions
and
3,250 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const fs = require("node:fs"); | ||
const path = require("node:path"); | ||
|
||
const packageJson = require("../../package.json"); | ||
|
||
fs.writeFileSync( | ||
path.resolve(__dirname, "../../source/main/library/build.ts"), | ||
`// This file updated automatically: changes made here will be overwritten! | ||
export const VERSION = "${packageJson.version}"; | ||
` | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file updated automatically: changes made here will be overwritten! | ||
|
||
export const VERSION = "2.20.3"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Entry, EntryPropertyValueType, EntryURLType, getEntryURLs, VaultSource } from "buttercup"; | ||
import { OTP } from "../types"; | ||
|
||
export function extractVaultOTPItems(source: VaultSource): Array<OTP> { | ||
return source.vault.getAllEntries().reduce((output: Array<OTP>, entry: Entry) => { | ||
const properties = entry.getProperties(); | ||
const loginURLs = getEntryURLs(properties, EntryURLType.Login); | ||
for (const key in properties) { | ||
if (entry.getPropertyValueType(key) !== EntryPropertyValueType.OTP) continue; | ||
output.push({ | ||
sourceID: source.id, | ||
entryID: entry.id, | ||
entryProperty: key, | ||
entryTitle: properties.title, | ||
loginURL: loginURLs.length > 0 ? loginURLs[0] : null, | ||
otpURL: properties[key] | ||
}); | ||
} | ||
return output; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import express, { Request, Response, NextFunction } from "express"; | ||
import createRouter from "express-promise-router"; | ||
import { VERSION } from "../../library/build"; | ||
import { handleAuthPing, processAuthRequest, processAuthResponse } from "./controllers/auth"; | ||
import { searchEntries } from "./controllers/entries"; | ||
import { getAllOTPs } from "./controllers/otp"; | ||
import { getVaults, getVaultsTree, promptVaultLock, promptVaultUnlock } from "./controllers/vaults"; | ||
import { handleError } from "./error"; | ||
import { requireClient, requireKeyAuth } from "./middleware"; | ||
import { saveExistingEntry, saveNewEntry } from "./controllers/save"; | ||
|
||
export function buildApplication(): express.Application { | ||
const app = express(); | ||
app.disable("x-powered-by"); | ||
app.use(express.text()); | ||
app.use(express.json()); | ||
app.use((req: Request, res: Response, next: NextFunction) => { | ||
res.set("Server", `ButtercupDesktop/${VERSION}`); | ||
next(); | ||
}); | ||
createRoutes(app); | ||
app.use(handleError); | ||
return app; | ||
} | ||
|
||
function createRoutes(app: express.Application): void { | ||
const router = createRouter(); | ||
router.post("/auth/request", processAuthRequest); | ||
router.post("/auth/response", processAuthResponse); | ||
router.post("/auth/test", requireClient, requireKeyAuth, handleAuthPing); | ||
router.get("/entries", requireClient, searchEntries); | ||
router.get("/otps", requireClient, getAllOTPs); | ||
router.get("/vaults", requireClient, getVaults); | ||
router.get("/vaults-tree", requireClient, getVaultsTree); | ||
router.patch( | ||
"/vaults/:id/group/:gid/entry/:eid", | ||
requireClient, | ||
requireKeyAuth, | ||
saveExistingEntry | ||
); | ||
router.post("/vaults/:id/group/:gid/entry", requireClient, requireKeyAuth, saveNewEntry); | ||
router.post("/vaults/:id/lock", requireClient, requireKeyAuth, promptVaultLock); | ||
router.post("/vaults/:id/unlock", requireClient, requireKeyAuth, promptVaultUnlock); | ||
app.use("/v1", router); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Layerr } from "layerr"; | ||
import { decryptPayload, encryptPayload, getBrowserPublicKeyString } from "../browserAuth"; | ||
import { getConfigValue, setConfigValue } from "../config"; | ||
import { BrowserAPIErrorType } from "../../types"; | ||
|
||
export async function decryptAPIPayload(clientID: string, payload: string): Promise<string> { | ||
// Check that the client is registered, we don't actually | ||
// use their key for decryption.. | ||
const clients = await getConfigValue("browserClients"); | ||
const clientConfig = clients[clientID]; | ||
if (!clientConfig) { | ||
throw new Layerr( | ||
{ | ||
info: { | ||
clientID, | ||
code: BrowserAPIErrorType.NoAPIKey | ||
} | ||
}, | ||
"No client key registered for decryption" | ||
); | ||
} | ||
// Private key for decryption | ||
const browserPrivateKey = await getConfigValue("browserPrivateKey"); | ||
// Decrypt | ||
return decryptPayload(payload, clientConfig.publicKey, browserPrivateKey); | ||
} | ||
|
||
export async function encryptAPIPayload(clientID: string, payload: string): Promise<string> { | ||
// Check that the client is registered, we don't actually | ||
// use their key for decryption.. | ||
const clients = await getConfigValue("browserClients"); | ||
const clientConfig = clients[clientID]; | ||
if (!clientConfig) { | ||
throw new Layerr( | ||
{ | ||
info: { | ||
clientID, | ||
code: BrowserAPIErrorType.NoAPIKey | ||
} | ||
}, | ||
"No client key registered for encryption" | ||
); | ||
} | ||
// Private key for decryption | ||
const browserPrivateKey = await getConfigValue("browserPrivateKey"); | ||
// Encrypt | ||
return encryptPayload(payload, browserPrivateKey, clientConfig.publicKey); | ||
} | ||
|
||
export async function registerPublicKey(id: string, publicKey: string): Promise<string> { | ||
const clients = await getConfigValue("browserClients"); | ||
await setConfigValue("browserClients", { | ||
...clients, | ||
[id]: { | ||
publicKey | ||
} | ||
}); | ||
const serverPublicKey = await getBrowserPublicKeyString(); | ||
return serverPublicKey; | ||
} |
Oops, something went wrong.