UNPKG

@pedwise/next-firebase-auth-edge

Version:

Next.js 13 Firebase Authentication for Edge and server runtimes. Dedicated for Next 13 server components. Compatible with Next.js middleware.

57 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CryptoSignerErrorCode = exports.CryptoSignerError = exports.ServiceAccountSigner = void 0; const consts_1 = require("./consts"); const utils_1 = require("./utils"); const ALGORITHM_RS256 = "RS256"; class ServiceAccountSigner { constructor(credential) { this.credential = credential; this.algorithm = ALGORITHM_RS256; if (!credential) { throw new CryptoSignerError({ code: CryptoSignerErrorCode.INVALID_CREDENTIAL, message: "INTERNAL ASSERT: Must provide a service account credential to initialize ServiceAccountSigner.", }); } } async sign(token) { const tokenBuffer = (0, utils_1.stringToArrayBuffer)(token); const keyData = (0, utils_1.pemToArrayBuffer)(this.credential.privateKey); const key = await crypto.subtle.importKey("pkcs8", keyData, consts_1.ALGORITHMS[ALGORITHM_RS256], false, ["sign"]); const signed = await crypto.subtle.sign(consts_1.ALGORITHMS[ALGORITHM_RS256], key, tokenBuffer); return (0, utils_1.arrayBufferToBase64)(signed); } /** * @inheritDoc */ getAccountId() { return Promise.resolve(this.credential.clientEmail); } } exports.ServiceAccountSigner = ServiceAccountSigner; class CryptoSignerError extends Error { constructor(errorInfo) { super(errorInfo.message); this.errorInfo = errorInfo; Object.setPrototypeOf(this, CryptoSignerError.prototype); } get code() { return this.errorInfo.code; } get message() { return this.errorInfo.message; } get cause() { return this.errorInfo.cause; } } exports.CryptoSignerError = CryptoSignerError; class CryptoSignerErrorCode { } exports.CryptoSignerErrorCode = CryptoSignerErrorCode; CryptoSignerErrorCode.INVALID_ARGUMENT = "invalid-argument"; CryptoSignerErrorCode.INTERNAL_ERROR = "internal-error"; CryptoSignerErrorCode.INVALID_CREDENTIAL = "invalid-credential"; CryptoSignerErrorCode.SERVER_ERROR = "server-error"; //# sourceMappingURL=crypto-signer.js.map