Skip to content

Commit 5ac8942

Browse files
committed
init
1 parent fd0fc58 commit 5ac8942

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
4141
env:
4242
POSTGRES_STRING: ${{ secrets.POSTGRES_STRING }}
43+
PROJECT_ID: ${{ secrets.PROJECT_ID }}
4344
with:
4445
app_yaml_path: app.yaml
4546

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This API provides endpoints for interacting with two main tables: `faucet.solana
1919
3. Set up your `.env` file with the following
2020
```env
2121
POSTGRES_STRING=postgresql://<user>:<password>@<host>:<port>/<database>
22+
PROJECT_ID=<GCP Project ID>
2223
```
2324

2425
4. Start the server

app.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// Importing Express and Routes using ES Module syntax
21
import express from 'express';
3-
import routes from './src/routes/index.js'; // Must include the .js extension in ES Modules
2+
import routes from './src/routes/index.js';
3+
import { validateGoogleToken } from './src/routes/middleware/authorization.js';
44

55
// Initialize Express
66
const app = express();
77

88
// Middleware
99
app.use(express.json()); // Parse JSON request bodies
10-
11-
// Routes
12-
app.use('/api', routes); // Use routes from the /routes folder
10+
app.use('/api', validateGoogleToken, routes); // Secure all API routes
1311

1412
// Global Error Handling Middleware
1513
app.use((err, req, res, next) => {

app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ entrypoint: yarn start
1212
# Environment variables (replace with your actual values or secrets)
1313
env_variables:
1414
POSTGRES_STRING: $POSTGRES_STRING
15+
PROJECT_ID: $PROJECT_ID
1516

1617
# Automatic scaling configuration
1718
automatic_scaling:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"dotenv": "^16.4.5",
1616
"express": "^4.21.1",
17+
"google-auth-library": "^9.14.2",
1718
"pg": "^8.13.1"
1819
}
1920
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { OAuth2Client } from 'google-auth-library';
2+
3+
const oAuth2Client = new OAuth2Client();
4+
5+
const validateGoogleToken = async (req, res, next) => {
6+
const authHeader = req.header('Authorization');
7+
if (!authHeader) {
8+
return res.status(401).json({ message: 'Unauthorized' });
9+
}
10+
11+
const token = authHeader.split(' ')[1]; // Bearer <token>
12+
try {
13+
// Verify the access token's payload:
14+
const tokenInfo = await oAuth2Client.getTokenInfo(token);
15+
16+
if (tokenInfo.email !== `solana-devnet-faucet-fe@${process.env.PROJECT_ID}.iam.gserviceaccount.com`) {
17+
return res.status(403).json({ message: 'Forbidden: Invalid audience' });
18+
}
19+
20+
// Proceed if valid token
21+
req.user = tokenInfo; // Attach tokenInfo data (like subject) to req.user
22+
next();
23+
} catch (error) {
24+
console.log("Error with Auth", error);
25+
res.status(403).json({ message: 'Forbidden' });
26+
}
27+
};
28+
29+
export { validateGoogleToken };

0 commit comments

Comments
 (0)