Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

cleaner code

daniel31x13655115016efc
diff --git a/apps/web/lib/api/apple.ts b/apps/web/lib/api/apple.ts
index 4ed71116..15dc8e3d 100644
--- a/apps/web/lib/api/apple.ts
+++ b/apps/web/lib/api/apple.ts
@@ -1,7 +1,7 @@
import { createSign } from "crypto";
-import { readFileSync } from "fs";
import { createRemoteJWKSet, jwtVerify } from "jose";
import * as process from "process";
+import readSecret from "./readSecret";
const appleIssuer = "https://appleid.apple.com";
const appleJwks = createRemoteJWKSet(new URL(`${appleIssuer}/auth/keys`));
@@ -27,10 +27,11 @@ export function getAppleClientId() {
function getApplePrivateKey() {
if (applePrivateKey) return applePrivateKey;
- if (!process.env.APPLE_PRIVATE_KEY_PATH)
- throw Error("Apple private key path is not configured.");
+ const privateKey = readSecret(process.env.APPLE_PRIVATE_KEY_PATH);
- applePrivateKey = readFileSync(process.env.APPLE_PRIVATE_KEY_PATH, "utf8");
+ if (!privateKey) throw Error("Apple private key is not configured.");
+
+ applePrivateKey = privateKey;
return applePrivateKey;
}
@@ -65,12 +66,10 @@ function createAppleClientSecret() {
const unsignedToken = `${base64UrlEncode(header)}.${base64UrlEncode(
payload
)}`;
- const signature = createSign("SHA256")
- .update(unsignedToken)
- .sign({
- key: getApplePrivateKey(),
- dsaEncoding: "ieee-p1363",
- });
+ const signature = createSign("SHA256").update(unsignedToken).sign({
+ key: getApplePrivateKey(),
+ dsaEncoding: "ieee-p1363",
+ });
return {
value: `${unsignedToken}.${base64UrlEncode(signature)}`,
diff --git a/apps/web/lib/api/billing/appStore.ts b/apps/web/lib/api/billing/appStore.ts
index 3514500e..940ab58d 100644
--- a/apps/web/lib/api/billing/appStore.ts
+++ b/apps/web/lib/api/billing/appStore.ts
@@ -8,6 +8,7 @@ import {
type LastTransactionsItem,
type StatusResponse,
} from "@apple/app-store-server-library";
+import readSecret from "../readSecret";
const BUNDLE_ID = process.env.APP_STORE_BUNDLE_ID || "app.linkwarden";
@@ -28,8 +29,7 @@ const getClient = (environment: Environment) => {
if (!clients[environment]) {
clients[environment] = new AppStoreServerAPIClient(
- // Deployment tooling often stores the .p8 with literal \n sequences
- (process.env.APP_STORE_PRIVATE_KEY as string).replace(/\\n/g, "\n"),
+ readSecret(process.env.APP_STORE_PRIVATE_KEY) as string,
process.env.APP_STORE_KEY_ID as string,
process.env.APP_STORE_ISSUER_ID as string,
BUNDLE_ID,
@@ -154,7 +154,8 @@ export default async function getAppleSubscriptionState(
!entry.transaction.revocationDate
) ??
decorated.sort(
- (a, b) => (b.transaction.expiresDate ?? 0) - (a.transaction.expiresDate ?? 0)
+ (a, b) =>
+ (b.transaction.expiresDate ?? 0) - (a.transaction.expiresDate ?? 0)
)[0];
const { item, transaction, renewalInfo } = best;
diff --git a/apps/web/lib/api/billing/googlePlay.ts b/apps/web/lib/api/billing/googlePlay.ts
index 96cb92cf..b1519be0 100644
--- a/apps/web/lib/api/billing/googlePlay.ts
+++ b/apps/web/lib/api/billing/googlePlay.ts
@@ -1,4 +1,5 @@
import { JWT } from "google-auth-library";
+import readSecret from "../readSecret";
const PACKAGE_NAME = process.env.GOOGLE_PLAY_PACKAGE_NAME || "app.linkwarden";
const API_BASE = `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${PACKAGE_NAME}`;
@@ -17,11 +18,7 @@ const getClient = () => {
);
if (!client) {
- // Accept both raw and base64-encoded service-account JSON
- const json = raw.trim().startsWith("{")
- ? raw
- : Buffer.from(raw, "base64").toString("utf8");
- const credentials = JSON.parse(json) as {
+ const credentials = JSON.parse(readSecret(raw) as string) as {
client_email: string;
private_key: string;
};
@@ -129,7 +126,9 @@ export default async function getGoogleSubscriptionState(
purchase.outOfAppPurchaseContext?.expiredPurchaseToken ?? null,
needsAcknowledgement:
purchase.acknowledgementState === "ACKNOWLEDGEMENT_STATE_PENDING",
- currentPeriodStart: purchase.startTime ? new Date(purchase.startTime) : null,
+ currentPeriodStart: purchase.startTime
+ ? new Date(purchase.startTime)
+ : null,
currentPeriodEnd: expiryTime,
raw: purchase,
};
diff --git a/apps/web/lib/api/readSecret.ts b/apps/web/lib/api/readSecret.ts
new file mode 100644
index 00000000..c130a6e7
--- /dev/null
+++ b/apps/web/lib/api/readSecret.ts
@@ -0,0 +1,29 @@
+import { existsSync, readFileSync } from "fs";
+
+const isInline = (value: string) =>
+ value.startsWith("{") || value.includes("-----BEGIN");
+
+// Single-line PEMs carry literal \n sequences that must become real newlines;
+// JSON must keep its escape sequences intact for JSON.parse.
+const normalize = (value: string) =>
+ value.startsWith("{") ? value : value.replace(/\\n/g, "\n");
+
+// Reads a secret (PEM key or JSON blob) from an env value that may be:
+// - the inline value itself,
+// - a base64 encoding of it, or
+// - a path to a file containing it.
+export default function readSecret(value?: string | null): string | null {
+ if (!value?.trim()) return null;
+
+ const raw = value.trim();
+
+ if (isInline(raw)) return normalize(raw);
+
+ if (existsSync(raw)) return readFileSync(raw, "utf8");
+
+ const decoded = Buffer.from(raw, "base64").toString("utf8");
+ if (isInline(decoded)) return normalize(decoded);
+
+ // Unrecognized — hand it back so the consumer fails with its own clear error
+ return raw;
+}