Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions backend/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,22 @@
}
}
},
"/users/": {
"/users/users": {
"get": {
"description": "",
"parameters": [],
"parameters": [
{
"name": "authorization",
"in": "header",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand All @@ -506,11 +515,19 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "authorization",
"in": "header",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand All @@ -524,11 +541,54 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "authorization",
"in": "header",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
}
}
}
},
"/users/modify/": {
"post": {
"description": "",
"parameters": [
{
"name": "authorization",
"in": "header",
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"uid": {
"example": "any"
},
"new_claim": {
"example": "any"
}
}
}
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand Down
1 change: 0 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import userRouter from "./users/views";
import swaggerUI from "swagger-ui-express";
import spec from "../api-spec.json";
import { dbConnect } from "./database";
import userRouter from "./users/views";

const app = express();

Expand Down
9 changes: 2 additions & 7 deletions backend/src/users/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import * as admin from 'firebase-admin'

const serviceAccount = require('../../serviceAccountKey.json')

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
})
import { admin_app } from './firebase-functions'

/**
* Modify the claim of a user given user ID and new claim
Expand All @@ -16,7 +11,7 @@ export function changeClaim(uid: string, new_claim: number) {
evaluator: new_claim == 2,
admin: new_claim == 3
};
admin.auth().setCustomUserClaims(uid, claims)
admin_app.auth().setCustomUserClaims(uid, claims)
.catch(
() => {
console.log("ERROR: User with uid " + uid + " not found.");
Expand Down
8 changes: 4 additions & 4 deletions backend/src/users/firebase-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var admin = require("firebase-admin");

var serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
export const admin_app = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});

Expand All @@ -29,13 +29,13 @@ export function createUser(number: string, claim: number) {
break;
}
}
admin
admin_app
.auth()
.createUser({
phoneNumber: number,
})
.then((userRecord: any) => {
admin.auth().setCustomUserClaims(userRecord.uid, customClaims);
admin_app.auth().setCustomUserClaims(userRecord.uid, customClaims);
console.log("Successfully created new user:", userRecord.uid);
})
.catch((error: any) => {
Expand All @@ -47,7 +47,7 @@ export function createUser(number: string, claim: number) {
* Delete a user given their phone number; number should have +(country code) prepended, e.g. +16071234567
*/
export function deleteUser(number: string) {
const auth = admin.auth();
const auth = admin_app.auth();
auth
.getUserByPhoneNumber(number)
.then((userRecord: any) => {
Expand Down
5 changes: 4 additions & 1 deletion backend/src/users/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { Router } from "express";
import { createUser, deleteUser } from "./firebase-functions";
import { successJson } from "../utils/jsonResponses";
import UserController from "./controllers";
import { verifyToken } from "../utils/admin";

const userRouter = Router();

userRouter.get("/", (req, res) => {
userRouter.use(verifyToken);

userRouter.get("/users", (req, res) => {
res.send("Hello from a subrouter");
});

Expand Down
25 changes: 25 additions & 0 deletions backend/src/utils/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response, NextFunction } from "express";
import { getAuth } from "firebase-admin/auth";

export const verifyToken = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { authorization } = req.headers
if (!authorization) {
console.log(req.headers)
return res.status(401).send({ message: 'Unauthorized' });
}

const token = authorization.split(" ")[1];

getAuth()
.verifyIdToken(token)
.then((claims) => {
if (claims.admin === true) {
next();
}
})
.catch((err) => res.sendStatus(403).send({ message: 'Forbidden' }))
};
25 changes: 25 additions & 0 deletions backend/src/utils/evaluator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response, NextFunction } from "express";
import { getAuth } from "firebase-admin/auth";

export const verifyToken = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { authorization } = req.headers
if (!authorization) {
console.log(req.headers)
return res.status(401).send({ message: 'Unauthorized' });
}

const token = authorization.split(" ")[1];

getAuth()
.verifyIdToken(token)
.then((claims) => {
if (claims.evaluator === true || claims.admin === true) {
next();
}
})
.catch((err) => res.sendStatus(403).send({ message: 'Forbidden' }))
};
25 changes: 25 additions & 0 deletions backend/src/utils/mobile-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response, NextFunction } from "express";
import { getAuth } from "firebase-admin/auth";

export const verifyToken = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { authorization } = req.headers
if (!authorization) {
console.log(req.headers)
return res.status(401).send({ message: 'Unauthorized' });
}

const token = authorization.split(" ")[1];

getAuth()
.verifyIdToken(token)
.then((claims) => {
if (claims.mobile_entry === true || claims.admin === true) {
next();
}
})
.catch((err) => res.sendStatus(403).send({ message: 'Forbidden' }))
};
25 changes: 25 additions & 0 deletions backend/src/utils/web-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response, NextFunction } from "express";
import { getAuth } from "firebase-admin/auth";

export const verifyToken = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { authorization } = req.headers
if (!authorization) {
console.log(req.headers)
return res.status(401).send({ message: 'Unauthorized' });
}

const token = authorization.split(" ")[1];

getAuth()
.verifyIdToken(token)
.then((claims) => {
if (claims.web_entry === true) {
next();
}
})
.catch((err) => res.sendStatus(403).send({ message: 'Forbidden' }))
};