Skip to content

Commit 9932872

Browse files
committed
fix(functions): add app check verification
1 parent c1f497d commit 9932872

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

functions/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ firestore-debug.log
1717
coverage/
1818
.env
1919
.runtimeconfig.json
20+
21+
package-lock.json
22+
bun.lockb

functions/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"main": "lib/index.js",
2121
"dependencies": {
2222
"@firebase/database-types": "1.0.0",
23-
"@google-cloud/storage": "7.3.1",
23+
"@google-cloud/storage": "7.4.0",
2424
"@sign-mt/browsermt": "0.2.1",
2525
"cors": "2.8.5",
2626
"express": "4.18.2",
@@ -29,16 +29,16 @@
2929
"firebase-functions": "4.4.1",
3030
"http-errors": "2.0.0",
3131
"node-fetch": "2.6.7",
32-
"openai": "4.12.4"
32+
"openai": "4.14.0"
3333
},
3434
"devDependencies": {
3535
"@firebase/firestore-types": "3.0.0",
3636
"@firebase/rules-unit-testing": "3.0.1",
3737
"@types/http-errors": "2.0.3",
3838
"@types/jest": "29.5.6",
3939
"@types/node-fetch": "2.6.7",
40-
"@typescript-eslint/eslint-plugin": "6.8.0",
41-
"@typescript-eslint/parser": "6.8.0",
40+
"@typescript-eslint/eslint-plugin": "6.9.0",
41+
"@typescript-eslint/parser": "6.9.0",
4242
"eslint": "8.52.0",
4343
"firebase-functions-test": "3.1.0",
4444
"firebase-tools": "12.7.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {RequestHandler} from 'express';
2+
3+
import {getAppCheck} from 'firebase-admin/app-check';
4+
import * as httpErrors from 'http-errors';
5+
6+
export const appCheckVerification: RequestHandler = async (req, res, next) => {
7+
const appCheckToken = req.header('X-Firebase-AppCheck');
8+
9+
if (!appCheckToken) {
10+
throw new httpErrors.Unauthorized('Missing App Check token');
11+
}
12+
13+
try {
14+
await getAppCheck().verifyToken(appCheckToken);
15+
return next();
16+
} catch (err) {
17+
throw new httpErrors.Unauthorized('Invalid App Check token');
18+
}
19+
};

functions/src/text-normalization/controller.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {FirebaseDatabase, Reference} from '@firebase/database-types';
77
import {TextNormalizationModel} from './model';
88
import {onRequest} from 'firebase-functions/v2/https';
99
import {defineString} from 'firebase-functions/params';
10+
import {appCheckVerification} from '../middlewares/appcheck.middleware';
1011
import type {StringParam} from 'firebase-functions/lib/params/types';
1112

1213
export class TextNormalizationEndpoint {
@@ -104,9 +105,19 @@ export const textNormalizationFunctions = (database: FirebaseDatabase) => {
104105

105106
const app = express();
106107
app.use(cors());
108+
app.use(appCheckVerification);
107109
app.options('*', (req, res) => res.status(200).end());
108110
app.get('/', request);
109111
app.get('/api/text-normalization', request); // Hosting redirect
110112
app.use(errorMiddleware);
111-
return onRequest({enforceAppCheck: true}, app);
113+
114+
return onRequest(
115+
{
116+
invoker: 'public',
117+
cpu: 'gcf_gen1',
118+
concurrency: 1,
119+
timeoutSeconds: 30,
120+
},
121+
app
122+
);
112123
};

0 commit comments

Comments
 (0)