style: fix linting issues met Prettier
This commit is contained in:
parent
b12c743440
commit
57cd8466fe
23 changed files with 4817 additions and 4248 deletions
|
@ -1,25 +1,25 @@
|
|||
import {EnvVars, getEnvVar} from "../../util/envvars.js";
|
||||
import {expressjwt} from 'express-jwt';
|
||||
import {JwtPayload} from 'jsonwebtoken'
|
||||
import { EnvVars, getEnvVar } from '../../util/envvars.js';
|
||||
import { expressjwt } from 'express-jwt';
|
||||
import { JwtPayload } from 'jsonwebtoken';
|
||||
import jwksClient from 'jwks-rsa';
|
||||
import * as express from "express";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import {AuthenticatedRequest} from "./authenticated-request.js";
|
||||
import {AuthenticationInfo} from "./authentication-info.js";
|
||||
import {ForbiddenException, UnauthorizedException} from "../../exceptions";
|
||||
import * as express from 'express';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { AuthenticatedRequest } from './authenticated-request.js';
|
||||
import { AuthenticationInfo } from './authentication-info.js';
|
||||
import { ForbiddenException, UnauthorizedException } from '../../exceptions';
|
||||
|
||||
const JWKS_CACHE = true;
|
||||
const JWKS_RATE_LIMIT = true;
|
||||
const REQUEST_PROPERTY_FOR_JWT_PAYLOAD = "jwtPayload";
|
||||
const JWT_ALGORITHM = "RS256"; // Not configurable via env vars since supporting other algorithms would
|
||||
// Require additional libraries to be added.
|
||||
const REQUEST_PROPERTY_FOR_JWT_PAYLOAD = 'jwtPayload';
|
||||
const JWT_ALGORITHM = 'RS256'; // Not configurable via env vars since supporting other algorithms would
|
||||
// Require additional libraries to be added.
|
||||
|
||||
const JWT_PROPERTY_NAMES = {
|
||||
username: "preferred_username",
|
||||
firstName: "given_name",
|
||||
lastName: "family_name",
|
||||
name: "name",
|
||||
email: "email"
|
||||
username: 'preferred_username',
|
||||
firstName: 'given_name',
|
||||
lastName: 'family_name',
|
||||
name: 'name',
|
||||
email: 'email',
|
||||
};
|
||||
|
||||
function createJwksClient(uri: string): jwksClient.JwksClient {
|
||||
|
@ -38,7 +38,7 @@ const idpConfigs = {
|
|||
teacher: {
|
||||
issuer: getEnvVar(EnvVars.IdpTeacherUrl),
|
||||
jwksClient: createJwksClient(getEnvVar(EnvVars.IdpTeacherJwksEndpoint)),
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -47,26 +47,26 @@ const idpConfigs = {
|
|||
const verifyJwtToken = expressjwt({
|
||||
secret: async (_: express.Request, token: jwt.Jwt | undefined) => {
|
||||
if (!token?.payload || !(token.payload as JwtPayload).iss) {
|
||||
throw new Error("Invalid token");
|
||||
throw new Error('Invalid token');
|
||||
}
|
||||
|
||||
const issuer = (token.payload as JwtPayload).iss;
|
||||
|
||||
const idpConfig = Object.values(idpConfigs).find(config => config.issuer === issuer);
|
||||
const idpConfig = Object.values(idpConfigs).find((config) => config.issuer === issuer);
|
||||
if (!idpConfig) {
|
||||
throw new Error("Issuer not accepted.");
|
||||
throw new Error('Issuer not accepted.');
|
||||
}
|
||||
|
||||
const signingKey = await idpConfig.jwksClient.getSigningKey(token.header.kid);
|
||||
if (!signingKey) {
|
||||
throw new Error("Signing key not found.");
|
||||
throw new Error('Signing key not found.');
|
||||
}
|
||||
return signingKey.getPublicKey();
|
||||
},
|
||||
audience: getEnvVar(EnvVars.IdpAudience),
|
||||
algorithms: [JWT_ALGORITHM],
|
||||
credentialsRequired: false,
|
||||
requestProperty: REQUEST_PROPERTY_FOR_JWT_PAYLOAD
|
||||
requestProperty: REQUEST_PROPERTY_FOR_JWT_PAYLOAD,
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -77,12 +77,12 @@ function getAuthenticationInfo(req: AuthenticatedRequest): AuthenticationInfo |
|
|||
return;
|
||||
}
|
||||
const issuer = req.jwtPayload.iss;
|
||||
let accountType: "student" | "teacher";
|
||||
let accountType: 'student' | 'teacher';
|
||||
|
||||
if (issuer === idpConfigs.student.issuer) {
|
||||
accountType = "student";
|
||||
accountType = 'student';
|
||||
} else if (issuer === idpConfigs.teacher.issuer) {
|
||||
accountType = "teacher";
|
||||
accountType = 'teacher';
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ function getAuthenticationInfo(req: AuthenticatedRequest): AuthenticationInfo |
|
|||
firstName: req.jwtPayload[JWT_PROPERTY_NAMES.firstName],
|
||||
lastName: req.jwtPayload[JWT_PROPERTY_NAMES.lastName],
|
||||
email: req.jwtPayload[JWT_PROPERTY_NAMES.email],
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,7 +113,9 @@ export const authenticateUser = [verifyJwtToken, addAuthenticationInfo];
|
|||
* @param accessCondition Predicate over the current AuthenticationInfo. Access is only granted when this evaluates
|
||||
* to true.
|
||||
*/
|
||||
export const authorize = (accessCondition: (auth: AuthenticationInfo) => boolean) => (req: AuthenticatedRequest, res: express.Response, next: express.NextFunction): void => {
|
||||
export const authorize =
|
||||
(accessCondition: (auth: AuthenticationInfo) => boolean) =>
|
||||
(req: AuthenticatedRequest, res: express.Response, next: express.NextFunction): void => {
|
||||
if (!req.auth) {
|
||||
throw new UnauthorizedException();
|
||||
} else if (!accessCondition(req.auth)) {
|
||||
|
@ -121,19 +123,19 @@ export const authorize = (accessCondition: (auth: AuthenticationInfo) => boolean
|
|||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Middleware which rejects all unauthenticated users, but accepts all authenticated users.
|
||||
*/
|
||||
export const authenticatedOnly = authorize(_ => true);
|
||||
export const authenticatedOnly = authorize((_) => true);
|
||||
|
||||
/**
|
||||
* Middleware which rejects requests from unauthenticated users or users that aren't students.
|
||||
*/
|
||||
export const studentsOnly = authorize(auth => auth.accountType === "student");
|
||||
export const studentsOnly = authorize((auth) => auth.accountType === 'student');
|
||||
|
||||
/**
|
||||
* Middleware which rejects requests from unauthenticated users or users that aren't teachers.
|
||||
*/
|
||||
export const teachersOnly = authorize(auth => auth.accountType === "teacher");
|
||||
export const teachersOnly = authorize((auth) => auth.accountType === 'teacher');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue