scrt-link-core
Advanced tools
Comparing version 0.1.3 to 0.1.4
import { encryptMessage, decryptMessage, generateAlias, generateEncryptionKey } from "./utils"; | ||
import { Options } from "./types"; | ||
export declare const createSecret: (message: string, options?: Options, baseUrl?: string) => Promise<{ | ||
import { CreateSecretOptions, SecretUrlFields } from "./types"; | ||
export declare const createSecret: (message: string, options?: CreateSecretOptions, baseUrl?: string) => Promise<{ | ||
alias: string; | ||
@@ -8,2 +8,3 @@ encryptionKey: string; | ||
}>; | ||
export declare const retrieveSecret: (alias: string, decryptionKey: string, baseUrl?: string) => Promise<Partial<SecretUrlFields>>; | ||
export { encryptMessage, decryptMessage, generateAlias, generateEncryptionKey }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateEncryptionKey = exports.generateAlias = exports.decryptMessage = exports.encryptMessage = exports.createSecret = void 0; | ||
exports.generateEncryptionKey = exports.generateAlias = exports.decryptMessage = exports.encryptMessage = exports.retrieveSecret = exports.createSecret = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -29,2 +29,20 @@ const constants_1 = require("./constants"); | ||
exports.createSecret = createSecret; | ||
const retrieveSecret = (alias, decryptionKey, baseUrl = constants_1.baseUrl) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const secretRaw = yield utils_1.api(`${baseUrl}/api/secrets/${alias}`, { | ||
method: "DELETE", | ||
}); | ||
// Handle legacy implementation | ||
if (alias.length === 12) { | ||
throw new Error(`Oops, this shouldn't have happened. This link appears to be in a legacy format that is no longer supported. Please ask to the sender to re-send the secret. Sorry for the inconvenience.`); | ||
} | ||
if (!secretRaw.message) { | ||
throw new Error(`Couldn't retrieve secret message.`); | ||
} | ||
const result = utils_1.decryptMessage(secretRaw.message, decryptionKey); | ||
if (!result) { | ||
throw new Error("Decryption failed."); | ||
} | ||
return Object.assign(Object.assign({}, secretRaw), { message: result }); | ||
}); | ||
exports.retrieveSecret = retrieveSecret; | ||
//# sourceMappingURL=main.js.map |
export declare type Maybe<T> = T | undefined | null; | ||
declare type SecretType = "text" | "url" | "neogram"; | ||
export interface Options { | ||
export interface CreateSecretOptions { | ||
alias?: string; | ||
secretType?: SecretType; | ||
encryptionKey?: string; | ||
secretType?: SecretType; | ||
neogramDestructionMessage?: string; | ||
@@ -11,6 +11,12 @@ neogramDestructionTimeout?: number; | ||
} | ||
export declare type Response = Maybe<{ | ||
export interface SecretUrlFields { | ||
alias: string; | ||
secretType: SecretType; | ||
isEncryptedWithUserPassword: boolean; | ||
message: string; | ||
}>; | ||
neogramDestructionMessage?: string; | ||
neogramDestructionTimeout?: number; | ||
receiptEmail?: string; | ||
receiptPhoneNumber?: string; | ||
} | ||
export {}; |
@@ -5,3 +5,3 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const unfetch_1 = tslib_1.__importDefault(require("unfetch")); | ||
const cross_fetch_1 = tslib_1.__importDefault(require("cross-fetch")); | ||
const crypto_js_1 = require("crypto-js"); | ||
@@ -16,3 +16,3 @@ const nanoid_1 = require("nanoid"); | ||
// Default options are marked with * | ||
const response = yield unfetch_1.default(url, Object.assign(Object.assign({ method: "GET", headers: { | ||
const response = yield cross_fetch_1.default(url, Object.assign(Object.assign({ method: "GET", headers: { | ||
"Content-Type": "application/json", | ||
@@ -19,0 +19,0 @@ } }, options), (data ? { body: JSON.stringify(data) } : {}))); |
{ | ||
"name": "scrt-link-core", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Core for scrt.link", | ||
@@ -49,5 +49,4 @@ "author": "Chris Chiller <kingchiller@gmail.com>", | ||
"nanoid": "^3.1.23", | ||
"nanoid-dictionary": "^4.3.0", | ||
"unfetch": "^4.2.0" | ||
"nanoid-dictionary": "^4.3.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # scrt-link-core | ||
There is only one function that you will ever need. | ||
The examples are based on the assumption that you use [scrt.link](https://scrt.link) as your backend - however, you may use this package with your own backend. You can use the repo [scrt-link](https://gitlab.com/kingchiller/scrt-link) as a reference. | ||
@@ -17,7 +17,7 @@ ### Basic usage | ||
const { secretLink } = await createSecret("Your secret"); | ||
// -> https://scrt-link/l/CWmbcLtxzFRad8JJ#ReCMTkJkAtUqFF9ydBAWdYaz} | ||
const { secretLink } = await createSecret("Some confidential information…"); | ||
// secretLink: https://scrt.link/l/CWmbcLtxzFRad8JJ#ReCMTkJkAtUqFF9ydBAWdYaz} | ||
``` | ||
### With Options | ||
### With options | ||
@@ -27,3 +27,3 @@ ```ts | ||
const { secretLink } = await createSecret("Your secret", { | ||
const { secretLink, alias, encryptionKey } = await createSecret("Some confidential information…", { | ||
password: "some-passphrase", | ||
@@ -35,3 +35,7 @@ secretType: "neogram", // "text" | "url" | "neogram" | ||
}); | ||
// -> https://scrt-link/l/CWmbcLtxzFRad8JJ#ReCMTkJkAtUqFF9ydBAWdYaz} | ||
/* | ||
alias: CWmbcLtxzFRad8JJ | ||
encryptionKey: ReCMTkJkAtUqFF9ydBAWdYaz | ||
secretLink: https://scrt.link/l/CWmbcLtxzFRad8JJ#ReCMTkJkAtUqFF9ydBAWdYaz} | ||
/* | ||
``` | ||
@@ -41,2 +45,12 @@ | ||
### Retrieve secret | ||
```ts | ||
import { retrieveSecret } from "scrt-link-core"; | ||
const { message } = await retrieveSecret("CWmbcLtxzFRad8JJ", "ReCMTkJkAtUqFF9ydBAWdYaz") | ||
}); | ||
// message: Some confidential information… | ||
``` | ||
[Full documentation](https://kingchiller.gitlab.io/scrt-link-core/) | ||
@@ -43,0 +57,0 @@ |
@@ -1,2 +0,7 @@ | ||
import { createSecret, generateAlias, generateEncryptionKey } from "./main"; | ||
import { | ||
createSecret, | ||
retrieveSecret, | ||
generateAlias, | ||
generateEncryptionKey, | ||
} from "./main"; | ||
import { urlAliasLength, encryptionKeyLength } from "./constants"; | ||
@@ -12,5 +17,22 @@ | ||
test("Create Secret", async () => { | ||
const secret = await createSecret("Foo"); | ||
test("Create and retrieve secret", async () => { | ||
const secretMessage = "I love you!"; | ||
const secret = await createSecret(secretMessage); | ||
expect(secret).toHaveProperty("secretLink"); | ||
expect(secret).toHaveProperty("alias"); | ||
expect(secret).toHaveProperty("encryptionKey"); | ||
const { alias, encryptionKey } = secret; | ||
const { message } = await retrieveSecret(alias, encryptionKey); | ||
expect(message).toBe(secretMessage); | ||
// Second attempt fails since secret is gone. | ||
try { | ||
await retrieveSecret(alias, encryptionKey); | ||
} catch (error) { | ||
expect(error.message).toContain("Secret not found"); | ||
} | ||
}); |
@@ -9,7 +9,7 @@ import { baseUrl as defaultBaseUrl } from "./constants"; | ||
} from "./utils"; | ||
import { Response, Options } from "./types"; | ||
import { CreateSecretOptions, SecretUrlFields } from "./types"; | ||
export const createSecret = async ( | ||
message: string, | ||
options: Options = {}, | ||
options: CreateSecretOptions = {}, | ||
baseUrl: string = defaultBaseUrl | ||
@@ -48,11 +48,40 @@ ): Promise<{ | ||
return api<Response>(`${baseUrl}/api/secrets`, { method: "POST" }, data).then( | ||
() => ({ | ||
alias, | ||
encryptionKey, | ||
secretLink: `${baseUrl}/l/${alias}#${encryptionKey}`, | ||
}) | ||
return api(`${baseUrl}/api/secrets`, { method: "POST" }, data).then(() => ({ | ||
alias, | ||
encryptionKey, | ||
secretLink: `${baseUrl}/l/${alias}#${encryptionKey}`, | ||
})); | ||
}; | ||
export const retrieveSecret = async ( | ||
alias: string, | ||
decryptionKey: string, | ||
baseUrl: string = defaultBaseUrl | ||
): Promise<Partial<SecretUrlFields>> => { | ||
const secretRaw = await api<Partial<SecretUrlFields>>( | ||
`${baseUrl}/api/secrets/${alias}`, | ||
{ | ||
method: "DELETE", | ||
} | ||
); | ||
// Handle legacy implementation | ||
if (alias.length === 12) { | ||
throw new Error( | ||
`Oops, this shouldn't have happened. This link appears to be in a legacy format that is no longer supported. Please ask to the sender to re-send the secret. Sorry for the inconvenience.` | ||
); | ||
} | ||
if (!secretRaw.message) { | ||
throw new Error(`Couldn't retrieve secret message.`); | ||
} | ||
const result = decryptMessage(secretRaw.message, decryptionKey); | ||
if (!result) { | ||
throw new Error("Decryption failed."); | ||
} | ||
return { ...secretRaw, message: result }; | ||
}; | ||
export { encryptMessage, decryptMessage, generateAlias, generateEncryptionKey }; |
export type Maybe<T> = T | undefined | null; | ||
type SecretType = "text" | "url" | "neogram"; | ||
export interface Options { | ||
export interface CreateSecretOptions { | ||
alias?: string; | ||
secretType?: SecretType; | ||
encryptionKey?: string; | ||
secretType?: SecretType; | ||
neogramDestructionMessage?: string; | ||
@@ -13,5 +13,11 @@ neogramDestructionTimeout?: number; | ||
export type Response = Maybe<{ | ||
export interface SecretUrlFields { | ||
alias: string; | ||
secretType: SecretType; | ||
isEncryptedWithUserPassword: boolean; | ||
message: string; | ||
}>; | ||
neogramDestructionMessage?: string; | ||
neogramDestructionTimeout?: number; | ||
receiptEmail?: string; | ||
receiptPhoneNumber?: string; | ||
} |
@@ -1,2 +0,2 @@ | ||
import fetch from "unfetch"; | ||
import fetch from "cross-fetch"; | ||
import { AES, enc, SHA256 } from "crypto-js"; | ||
@@ -3,0 +3,0 @@ import { customAlphabet } from "nanoid"; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
467150
4
710
72
2
- Removedunfetch@^4.2.0
- Removedunfetch@4.2.0(transitive)