refactor: create function to read admin SDK JSON

This commit is contained in:
2024-10-05 11:21:20 -04:00
parent 62217ae75b
commit 44d579ee6d
9 changed files with 63 additions and 72 deletions

View File

@@ -1,16 +1,13 @@
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { env } from "hono/adapter";
import mapKeys from "lodash.mapkeys";
import { z } from "zod";
import { fetchEpisodeUrlFromAllProviders } from "~/controllers/episodes/getEpisodeUrl";
import { Case, changeStringCase } from "~/libs/changeStringCase";
import type { AdminSdkCredentials } from "~/libs/gcloud/getGoogleAuthToken";
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
import { sendFcmMessage } from "~/libs/gcloud/sendFcmMessage";
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader";
import { readEnvVariable } from "~/libs/readEnvVariable";
import { getTokensSubscribedToTitle } from "~/models/token";
import { isWatchingTitle } from "~/models/watchStatus";
import type { Env } from "~/types/env";
@@ -83,13 +80,7 @@ app.post(
await Promise.allSettled(
tokens.map(async (token) => {
return sendFcmMessage(
mapKeys(
readEnvVariable<AdminSdkCredentials>(
env<Env, typeof c>(c, "workerd"),
"ADMIN_SDK_JSON",
),
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
) as unknown as AdminSdkCredentials,
getAdminSdkCredentials(env<Env, typeof c>(c, "workerd")),
{
token,
data: {

View File

@@ -1,13 +1,10 @@
import { Hono } from "hono";
import { env } from "hono/adapter";
import mapKeys from "lodash.mapkeys";
import { DateTime } from "luxon";
import { Case, changeStringCase } from "~/libs/changeStringCase";
import type { AdminSdkCredentials } from "~/libs/gcloud/getGoogleAuthToken";
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
import { sendFcmMessage } from "~/libs/gcloud/sendFcmMessage";
import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader";
import { readEnvVariable } from "~/libs/readEnvVariable";
import type { Env } from "~/types/env";
import { ErrorResponse, SuccessResponse } from "~/types/schema";
@@ -32,12 +29,7 @@ app.post("/", async (c) => {
title.media.title?.english ??
"Unknown Title";
return sendFcmMessage(
mapKeys(
readEnvVariable<AdminSdkCredentials>(c.env, "ADMIN_SDK_JSON"),
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
) as unknown as AdminSdkCredentials,
{
return sendFcmMessage(getAdminSdkCredentials(c.env), {
topic: "newTitles",
data: {
type: "new_title",
@@ -59,8 +51,7 @@ app.post("/", async (c) => {
click_action: "HANDLE_FCM_NOTIFICATION",
},
},
},
);
});
}),
);

View File

@@ -1,11 +1,8 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { env } from "hono/adapter";
import mapKeys from "lodash.mapkeys";
import { Case, changeStringCase } from "~/libs/changeStringCase";
import type { AdminSdkCredentials } from "~/libs/gcloud/getGoogleAuthToken";
import { getAdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
import { verifyFcmToken } from "~/libs/gcloud/verifyFcmToken";
import { readEnvVariable } from "~/libs/readEnvVariable";
import { saveToken } from "~/models/token";
import type { Env } from "~/types/env";
import {
@@ -73,10 +70,7 @@ app.openapi(route, async (c) => {
try {
const isValidToken = await verifyFcmToken(
token,
mapKeys(
readEnvVariable<AdminSdkCredentials>(c.env, "ADMIN_SDK_JSON"),
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
) as unknown as AdminSdkCredentials,
getAdminSdkCredentials(c.env),
);
if (!isValidToken) {
return c.json(ErrorResponse, 401);

View File

@@ -0,0 +1,27 @@
import mapKeys from "lodash.mapkeys";
import type { Env } from "~/types/env";
import { Case, changeStringCase } from "../changeStringCase";
import { readEnvVariable } from "../readEnvVariable";
export function getAdminSdkCredentials(env: Env) {
return mapKeys(
readEnvVariable<AdminSdkCredentials>(env, "ADMIN_SDK_JSON"),
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
) as unknown as AdminSdkCredentials;
}
export interface AdminSdkCredentials {
type: string;
projectId: string;
privateKeyId: string;
privateKey: string;
clientEmail: string;
clientID: string;
authURI: string;
tokenURI: string;
authProviderX509CertUrl: string;
clientX509CertUrl: string;
universeDomain: string;
}

View File

@@ -3,6 +3,7 @@ import { SignJWT, importPKCS8 } from "jose";
import { DateTime } from "luxon";
import { lazy } from "../lazy";
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
const { privateKey, clientEmail } = adminSdkJson;
@@ -11,7 +12,10 @@ export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
{
key: privateKey,
email: clientEmail,
scope: ["https://www.googleapis.com/auth/firebase.messaging"],
scope: [
"https://www.googleapis.com/auth/firebase.messaging",
"https://www.googleapis.com/auth/cloud-tasks",
],
},
adminSdkJson,
);
@@ -155,17 +159,3 @@ interface TokenData {
token: GoogleTokenData;
expiresAt: DateTime;
}
export interface AdminSdkCredentials {
type: string;
projectId: string;
privateKeyId: string;
privateKey: string;
clientEmail: string;
clientID: string;
authURI: string;
tokenURI: string;
authProviderX509CertUrl: string;
clientX509CertUrl: string;
universeDomain: string;
}

View File

@@ -1,7 +1,5 @@
import {
type AdminSdkCredentials,
getGoogleAuthToken,
} from "./getGoogleAuthToken";
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
import { getGoogleAuthToken } from "./getGoogleAuthToken";
export async function sendFcmMessage(
adminSdkJson: AdminSdkCredentials,
@@ -20,7 +18,7 @@ export async function sendFcmMessage(
Authorization: `Bearer ${await getGoogleAuthToken(adminSdkJson)}`,
},
},
).then((res) => res.json<SendFcmMessageResponse>());
).then((res) => res.json() as Promise<SendFcmMessageResponse>);
}
type SendFcmMessageResponse =

View File

@@ -2,7 +2,7 @@ import { describe, expect, it } from "bun:test";
import { server } from "~/mocks";
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
import { verifyFcmToken } from "./verifyFcmToken";
server.listen();

View File

@@ -1,4 +1,4 @@
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
import { sendFcmMessage } from "./sendFcmMessage";
export async function verifyFcmToken(

View File

@@ -2,7 +2,7 @@ import type { TokenOptions } from "gtoken";
import { mock } from "bun:test";
import type { AdminSdkCredentials } from "~/libs/gcloud/getGoogleAuthToken";
import type { AdminSdkCredentials } from "~/libs/gcloud/getAdminSdkCredentials";
const emailRegex =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;