The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no createPrivateKey. |
| 2 | import * as NodeCrypto from "node:crypto"; |
| 3 | |
| 4 | import { p256 } from "@noble/curves/nist"; |
| 5 | import { sha256 } from "@noble/hashes/sha2"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Base64Url from "effect/encoding/Base64Url"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Result from "effect/Result"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | |
| 12 | import type { ApnsCredentials } from "../Config.ts"; |
| 13 | |
| 14 | export class ApnsJwtEncodingError extends Schema.TaggedError<ApnsJwtEncodingError>()( |
| 15 | "ApnsJwtEncodingError", |
| 16 | { |
| 17 | component: Schema.Literals(["header", "payload"]), |
| 18 | teamId: Schema.String, |
| 19 | keyId: Schema.String, |
| 20 | issuedAtUnixSeconds: Schema.Number, |
| 21 | cause: Schema.Defect(), |
| 22 | }, |
| 23 | ) { |
| 24 | override get message(): string { |
| 25 | return `Failed to encode APNs JWT ${this.component} for key ${this.keyId}.`; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | export class ApnsJwtSigningError extends Schema.TaggedError<ApnsJwtSigningError>()( |
| 30 | "ApnsJwtSigningError", |
| 31 | { |
| 32 | teamId: Schema.String, |
| 33 | keyId: Schema.String, |
| 34 | issuedAtUnixSeconds: Schema.Number, |
| 35 | cause: Schema.Defect(), |
| 36 | }, |
| 37 | ) { |
| 38 | override get message(): string { |
| 39 | return `Failed to sign APNs JWT for key ${this.keyId}.`; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | export type ApnsJwtError = ApnsJwtEncodingError | ApnsJwtSigningError; |
| 44 | |
| 45 | const encodeApnsJwtHeaderJson = Schema.encodeEffect( |
| 46 | Schema.fromJsonString( |
| 47 | Schema.Struct({ |
| 48 | alg: Schema.Literal("ES256"), |
| 49 | kid: Schema.String, |
| 50 | }), |
| 51 | ), |
| 52 | ); |
| 53 | const encodeApnsJwtPayloadJson = Schema.encodeEffect( |
| 54 | Schema.fromJsonString( |
| 55 | Schema.Struct({ |
| 56 | iss: Schema.String, |
| 57 | iat: Schema.Number, |
| 58 | }), |
| 59 | ), |
| 60 | ); |
| 61 | |
| 62 | export interface ApnsJwtSigningInput { |
| 63 | readonly teamId: ApnsCredentials["teamId"]; |
| 64 | readonly keyId: ApnsCredentials["keyId"]; |
| 65 | readonly privateKey: ApnsCredentials["privateKey"]; |
| 66 | readonly issuedAtUnixSeconds: number; |
| 67 | } |
| 68 | |
| 69 | export const makeApnsJwt = Effect.fn("relay.apns.make_jwt")(function* (input: ApnsJwtSigningInput) { |
| 70 | const headerJson = yield* encodeApnsJwtHeaderJson({ alg: "ES256", kid: input.keyId }).pipe( |
| 71 | Effect.mapError( |
| 72 | (cause) => |
| 73 | new ApnsJwtEncodingError({ |
| 74 | component: "header", |
| 75 | teamId: input.teamId, |
| 76 | keyId: input.keyId, |
| 77 | issuedAtUnixSeconds: input.issuedAtUnixSeconds, |
| 78 | cause, |
| 79 | }), |
| 80 | ), |
| 81 | ); |
| 82 | const payloadJson = yield* encodeApnsJwtPayloadJson({ |
| 83 | iss: input.teamId, |
| 84 | iat: input.issuedAtUnixSeconds, |
| 85 | }).pipe( |
| 86 | Effect.mapError( |
| 87 | (cause) => |
| 88 | new ApnsJwtEncodingError({ |
| 89 | component: "payload", |
| 90 | teamId: input.teamId, |
| 91 | keyId: input.keyId, |
| 92 | issuedAtUnixSeconds: input.issuedAtUnixSeconds, |
| 93 | cause, |
| 94 | }), |
| 95 | ), |
| 96 | ); |
| 97 | |
| 98 | const privateKey = Redacted.value(input.privateKey); |
| 99 | const header = Base64Url.encode(headerJson); |
| 100 | const payload = Base64Url.encode(payloadJson); |
| 101 | const signingInput = `${header}.${payload}`; |
| 102 | |
| 103 | return yield* Effect.try({ |
| 104 | try: () => { |
| 105 | // RFC 6979 ES256 gives isolates identical JWTs at the same iat; Node crypto only extracts the scalar. |
| 106 | const scalar = apnsSigningScalar(privateKey); |
| 107 | const signature = p256 |
| 108 | .sign(sha256(new TextEncoder().encode(signingInput)), scalar, { prehash: false }) |
| 109 | .toCompactRawBytes(); |
| 110 | return `${signingInput}.${Base64Url.encode(signature)}`; |
| 111 | }, |
| 112 | catch: (cause) => |
| 113 | new ApnsJwtSigningError({ |
| 114 | teamId: input.teamId, |
| 115 | keyId: input.keyId, |
| 116 | issuedAtUnixSeconds: input.issuedAtUnixSeconds, |
| 117 | cause, |
| 118 | }), |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | const signingScalarCache = new Map<string, Uint8Array>(); |
| 123 | |
| 124 | function apnsSigningScalar(privateKeyPem: string): Uint8Array { |
| 125 | const cached = signingScalarCache.get(privateKeyPem); |
| 126 | if (cached) { |
| 127 | return cached; |
| 128 | } |
| 129 | const jwk = NodeCrypto.createPrivateKey(privateKeyPem.replace(/\\n/g, "\n")).export({ |
| 130 | format: "jwk", |
| 131 | }); |
| 132 | if (jwk.crv !== "P-256" || typeof jwk.d !== "string") { |
| 133 | throw new Error("APNs signing key is not a P-256 private key."); |
| 134 | } |
| 135 | const scalar = Result.getOrThrowWith( |
| 136 | Base64Url.decode(jwk.d), |
| 137 | () => new Error("APNs signing key scalar is not valid base64url."), |
| 138 | ); |
| 139 | signingScalarCache.set(privateKeyPem, scalar); |
| 140 | return scalar; |
| 141 | } |
| 142 | |
| 143 | export function apnsProviderTokenCacheKey(input: { |
| 144 | readonly teamId: string; |
| 145 | readonly keyId: string; |
| 146 | readonly privateKey: ApnsCredentials["privateKey"]; |
| 147 | }): string { |
| 148 | const keyFingerprint = NodeCrypto.createHash("sha256") |
| 149 | .update(Redacted.value(input.privateKey)) |
| 150 | .digest("hex") |
| 151 | .slice(0, 16); |
| 152 | return `${input.teamId}:${input.keyId}:${keyFingerprint}`; |
| 153 | } |