chore: change type for adminSdkJson parameter

This commit is contained in:
2024-06-15 06:11:09 -04:00
parent fd61974740
commit a9e363e814
4 changed files with 27 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import { GoogleToken } from "gtoken";
export async function getGoogleAuthToken(adminSdkJson: AdminSdkKey) {
export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
const { privateKey, clientEmail } = adminSdkJson;
const gToken = new GoogleToken({
@@ -11,7 +11,7 @@ export async function getGoogleAuthToken(adminSdkJson: AdminSdkKey) {
return gToken.getToken().then((token) => token.access_token);
}
interface AdminSdkKey {
export interface AdminSdkCredentials {
type: string;
projectID: string;
privateKeyID: string;

View File

@@ -1,7 +1,10 @@
import { getGoogleAuthToken } from "./getGoogleAuthToken";
import {
type AdminSdkCredentials,
getGoogleAuthToken,
} from "./getGoogleAuthToken";
export async function sendFcmMessage(
adminSdkJson: string,
adminSdkJson: AdminSdkCredentials,
message: FcmMessagePayload,
isOnlyValidatingFcmMessage?: boolean,
) {
@@ -14,7 +17,7 @@ export async function sendFcmMessage(
validate_only: isOnlyValidatingFcmMessage,
}),
headers: {
Authorization: `Bearer ${await getGoogleAuthToken(JSON.parse(adminSdkJson))}`,
Authorization: `Bearer ${await getGoogleAuthToken(adminSdkJson)}`,
},
},
).then((res) => res.json<SendFcmMessageResponse>());

View File

@@ -7,18 +7,33 @@ import { verifyFcmToken } from "./verifyFcmToken";
server.listen();
const FAKE_ADMIN_SDK_JSON = {
type: "service_account",
projectID: "test-26g38",
privateKeyID: "privateKeyId",
privateKey: "privateKey",
clientEmail: "test@test.com",
clientID: "clientId",
authURI: "https://accounts.google.com/o/oauth2/auth",
tokenURI: "https://oauth2.googleapis.com/token",
authProviderX509CERTURL: "https://www.googleapis.com/oauth2/v1/certs",
clientX509CERTURL:
"https://www.googleapis.com/robot/v1/metadata/x509/test%40test.com",
universeDomain: "aniplay.com",
};
describe("verifyFcmToken", () => {
it("valid token, returns true", async () => {
const token =
"7v8sy43aq0re4r8xe7rmr0cn1fsmh6phehnfla2pa73z899zmhyarivmkt4sj6pyv0py43u6p2sim6wz2vg9ypjp9rug1keoth7f6ll3gdvas4q020u3ah51r6bjgn51j6bd92ztmtof3ljpcm8q31njvndy65enm68";
const res = await verifyFcmToken(token, '{"clientEmail": "test@test.com"}');
const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
expect(res).toBeTrue();
});
it("invalid token, returns false", async () => {
const token = "abc123";
const res = await verifyFcmToken(token, '{"clientEmail": "test@test.com"}');
const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
expect(res).toBeFalse();
});

View File

@@ -1,8 +1,9 @@
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
import { sendFcmMessage } from "./sendFcmMessage";
export async function verifyFcmToken(
token: string,
adminSdkJson: string,
adminSdkJson: AdminSdkCredentials,
): Promise<boolean> {
return sendFcmMessage(
adminSdkJson,