chore: change type for adminSdkJson parameter
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { GoogleToken } from "gtoken";
|
import { GoogleToken } from "gtoken";
|
||||||
|
|
||||||
export async function getGoogleAuthToken(adminSdkJson: AdminSdkKey) {
|
export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
|
||||||
const { privateKey, clientEmail } = adminSdkJson;
|
const { privateKey, clientEmail } = adminSdkJson;
|
||||||
|
|
||||||
const gToken = new GoogleToken({
|
const gToken = new GoogleToken({
|
||||||
@@ -11,7 +11,7 @@ export async function getGoogleAuthToken(adminSdkJson: AdminSdkKey) {
|
|||||||
return gToken.getToken().then((token) => token.access_token);
|
return gToken.getToken().then((token) => token.access_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AdminSdkKey {
|
export interface AdminSdkCredentials {
|
||||||
type: string;
|
type: string;
|
||||||
projectID: string;
|
projectID: string;
|
||||||
privateKeyID: string;
|
privateKeyID: string;
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { getGoogleAuthToken } from "./getGoogleAuthToken";
|
import {
|
||||||
|
type AdminSdkCredentials,
|
||||||
|
getGoogleAuthToken,
|
||||||
|
} from "./getGoogleAuthToken";
|
||||||
|
|
||||||
export async function sendFcmMessage(
|
export async function sendFcmMessage(
|
||||||
adminSdkJson: string,
|
adminSdkJson: AdminSdkCredentials,
|
||||||
message: FcmMessagePayload,
|
message: FcmMessagePayload,
|
||||||
isOnlyValidatingFcmMessage?: boolean,
|
isOnlyValidatingFcmMessage?: boolean,
|
||||||
) {
|
) {
|
||||||
@@ -14,7 +17,7 @@ export async function sendFcmMessage(
|
|||||||
validate_only: isOnlyValidatingFcmMessage,
|
validate_only: isOnlyValidatingFcmMessage,
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${await getGoogleAuthToken(JSON.parse(adminSdkJson))}`,
|
Authorization: `Bearer ${await getGoogleAuthToken(adminSdkJson)}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
).then((res) => res.json<SendFcmMessageResponse>());
|
).then((res) => res.json<SendFcmMessageResponse>());
|
||||||
|
|||||||
@@ -7,18 +7,33 @@ import { verifyFcmToken } from "./verifyFcmToken";
|
|||||||
|
|
||||||
server.listen();
|
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", () => {
|
describe("verifyFcmToken", () => {
|
||||||
it("valid token, returns true", async () => {
|
it("valid token, returns true", async () => {
|
||||||
const token =
|
const token =
|
||||||
"7v8sy43aq0re4r8xe7rmr0cn1fsmh6phehnfla2pa73z899zmhyarivmkt4sj6pyv0py43u6p2sim6wz2vg9ypjp9rug1keoth7f6ll3gdvas4q020u3ah51r6bjgn51j6bd92ztmtof3ljpcm8q31njvndy65enm68";
|
"7v8sy43aq0re4r8xe7rmr0cn1fsmh6phehnfla2pa73z899zmhyarivmkt4sj6pyv0py43u6p2sim6wz2vg9ypjp9rug1keoth7f6ll3gdvas4q020u3ah51r6bjgn51j6bd92ztmtof3ljpcm8q31njvndy65enm68";
|
||||||
const res = await verifyFcmToken(token, '{"clientEmail": "test@test.com"}');
|
const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
|
||||||
|
|
||||||
expect(res).toBeTrue();
|
expect(res).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("invalid token, returns false", async () => {
|
it("invalid token, returns false", async () => {
|
||||||
const token = "abc123";
|
const token = "abc123";
|
||||||
const res = await verifyFcmToken(token, '{"clientEmail": "test@test.com"}');
|
const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
|
||||||
|
|
||||||
expect(res).toBeFalse();
|
expect(res).toBeFalse();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
|
||||||
import { sendFcmMessage } from "./sendFcmMessage";
|
import { sendFcmMessage } from "./sendFcmMessage";
|
||||||
|
|
||||||
export async function verifyFcmToken(
|
export async function verifyFcmToken(
|
||||||
token: string,
|
token: string,
|
||||||
adminSdkJson: string,
|
adminSdkJson: AdminSdkCredentials,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return sendFcmMessage(
|
return sendFcmMessage(
|
||||||
adminSdkJson,
|
adminSdkJson,
|
||||||
|
|||||||
Reference in New Issue
Block a user