⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
init
daniel31x13a8b72900e963
diff --git a/.env.sample b/.env.sampleindex e6a9bf62..47aa31cc 100644--- a/.env.sample+++ b/.env.sample@@ -116,8 +116,11 @@ FORTYTWO_CLIENT_SECRET=# AppleNEXT_PUBLIC_APPLE_ENABLED=APPLE_CUSTOM_NAME=-APPLE_ID=-APPLE_SECRET=+APPLE_CLIENT_ID=+APPLE_TEAM_ID=+APPLE_KEY_ID=+APPLE_PRIVATE_KEY_PATH=+APPLE_CLIENT_SECRET=# AtlassianNEXT_PUBLIC_ATLASSIAN_ENABLED=diff --git a/apps/web/lib/api/apple.ts b/apps/web/lib/api/apple.tsnew file mode 100644index 00000000..3dc24883--- /dev/null+++ b/apps/web/lib/api/apple.ts@@ -0,0 +1,101 @@+import { createSign } from "crypto";+import { readFileSync } from "fs";+import * as process from "process";++const appleClientSecretMaxAge = 60 * 60 * 24 * 30; // 30 days+const appleClientSecretRenewBuffer = 60 * 60; // 1 hour+let appleClientSecret:+ | {+ value: string;+ expiresAt: number;+ }+ | undefined;+let applePrivateKey: string | undefined;++export function getAppleClientId() {+ const clientId = process.env.APPLE_CLIENT_ID || process.env.APPLE_ID;++ if (!clientId) throw Error("Apple client ID is not configured.");++ return clientId;+}++function getApplePrivateKey() {+ if (applePrivateKey) return applePrivateKey;++ if (!process.env.APPLE_PRIVATE_KEY_PATH)+ throw Error("Apple private key path is not configured.");++ applePrivateKey = readFileSync(process.env.APPLE_PRIVATE_KEY_PATH, "utf8");++ return applePrivateKey;+}++function base64UrlEncode(value: Buffer | Record<string, any>) {+ const input = Buffer.isBuffer(value)+ ? value+ : Buffer.from(JSON.stringify(value));++ return input.toString("base64url");+}++function createAppleClientSecret() {+ if (!process.env.APPLE_TEAM_ID)+ throw Error("Apple team ID is not configured.");++ if (!process.env.APPLE_KEY_ID) throw Error("Apple key ID is not configured.");++ const now = Math.floor(Date.now() / 1000);+ const expiresAt = now + appleClientSecretMaxAge;+ const payload = {+ iss: process.env.APPLE_TEAM_ID,+ iat: now,+ exp: expiresAt,+ aud: "https://appleid.apple.com",+ sub: getAppleClientId(),+ };+ const header = {+ alg: "ES256",+ kid: process.env.APPLE_KEY_ID,+ };+ const unsignedToken = `${base64UrlEncode(header)}.${base64UrlEncode(+ payload+ )}`;+ const signature = createSign("SHA256")+ .update(unsignedToken)+ .sign({+ key: getApplePrivateKey(),+ dsaEncoding: "ieee-p1363",+ });++ return {+ value: `${unsignedToken}.${base64UrlEncode(signature)}`,+ expiresAt,+ };+}++export function getAppleClientSecret() {+ if (+ process.env.APPLE_TEAM_ID &&+ process.env.APPLE_KEY_ID &&+ process.env.APPLE_PRIVATE_KEY_PATH+ ) {+ const now = Math.floor(Date.now() / 1000);++ if (+ !appleClientSecret ||+ appleClientSecret.expiresAt - appleClientSecretRenewBuffer <= now+ ) {+ appleClientSecret = createAppleClientSecret();+ }++ return appleClientSecret.value;+ }++ const clientSecret =+ process.env.APPLE_CLIENT_SECRET || process.env.APPLE_SECRET;++ if (!clientSecret) throw Error("Apple client secret is not configured.");++ return clientSecret;+}diff --git a/apps/web/pages/api/v1/auth/[...nextauth].ts b/apps/web/pages/api/v1/auth/[...nextauth].tsindex 7623159e..af2ec07e 100644--- a/apps/web/pages/api/v1/auth/[...nextauth].ts+++ b/apps/web/pages/api/v1/auth/[...nextauth].ts@@ -3,6 +3,7 @@ import sendInvitationRequest from "@/lib/api/sendInvitationRequest";import sendVerificationRequest from "@/lib/api/sendVerificationRequest";import updateSeats from "@/lib/api/stripe/updateSeats";import verifySubscription from "@/lib/api/stripe/verifySubscription";+import { getAppleClientId, getAppleClientSecret } from "@/lib/api/apple";import { PrismaAdapter } from "@auth/prisma-adapter";import { User } from "@linkwarden/prisma/client";import bcrypt from "bcrypt";@@ -245,8 +246,10 @@ if (process.env.NEXT_PUBLIC_FORTYTWO_ENABLED === "true") {if (process.env.NEXT_PUBLIC_APPLE_ENABLED === "true") {providers.push(AppleProvider({- clientId: process.env.APPLE_CLIENT_ID!,- clientSecret: process.env.APPLE_CLIENT_SECRET!,+ clientId: getAppleClientId(),+ get clientSecret() {+ return getAppleClientSecret();+ },httpOptions: {timeout: 10000,},