Skip to content

Commit

Permalink
run format:write
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Jan 29, 2024
1 parent bbfa576 commit 6dfd993
Show file tree
Hide file tree
Showing 43 changed files with 1,107 additions and 1,327 deletions.
23 changes: 11 additions & 12 deletions server/src/auth/csrf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Request, Response } from "express";
import config from "config";
import { doubleCsrf } from "csrf-csrf";
import { Request, Response } from "express"
import config from "config"
import { doubleCsrf } from "csrf-csrf"

import { double_csrf_options_schema } from "@server/common/schema/config";
import { double_csrf_options_schema } from "@server/common/schema/config"


const csrf_options = double_csrf_options_schema.parse(config.get("csrf.options"));
const csrf_options = double_csrf_options_schema.parse(config.get("csrf.options"))

function rollingSecret(request?: Request): string {

Check failure on line 9 in server/src/auth/csrf.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'request' is defined but never used. Allowed unused args must match /_.*/u
return config.get("csrf.secret");
return config.get("csrf.secret")
}

export const { generateToken, doubleCsrfProtection } = doubleCsrf({
Expand Down Expand Up @@ -36,11 +35,11 @@ export const { generateToken, doubleCsrfProtection } = doubleCsrf({
"/".
Particularly, in development, the URI scheme is not considered "secure" by the user agent.
*/
...csrf_options
});
...csrf_options,
})

export function handleGetCsrfToken(request: Request, response: Response): void {
const csrfToken = generateToken(request, response);
response.status(200);
response.json({ "status": 200, "message": "Token generation OK", "token": csrfToken });
const csrfToken = generateToken(request, response)
response.status(200)
response.json({ status: 200, message: "Token generation OK", token: csrfToken })
}
32 changes: 14 additions & 18 deletions server/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import passport from "passport";
import passport from "passport"

import SequelizeUser from "@server/database/tables/user";
import { NullError } from "@server/common/errors";
import SequelizeUser from "@server/database/tables/user"
import { NullError } from "@server/common/errors"

//import "./strategy/local";
import "./strategy/oauth2";
import "./strategy/oauth2"

declare global {
/* eslint-disable @typescript-eslint/no-namespace */
Expand All @@ -14,30 +14,26 @@ declare global {
}

interface SerializedUser {
id: number;
id: number
}

passport.serializeUser<SerializedUser>(async function (
user: Express.User,
callback
) {
return callback(null, { id: user.id });
});
passport.serializeUser<SerializedUser>(async function (user: Express.User, callback) {

Check failure on line 20 in server/src/auth/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'Express' is not defined
return callback(null, { id: user.id })
})

passport.deserializeUser<SerializedUser>(async function (identifier, callback) {
if (typeof identifier?.id !== "number") {
return callback(null, null);
return callback(null, null)
}
try {
return callback(
null,
await SequelizeUser.findByPk(identifier.id, {
rejectOnEmpty: new NullError(),
})
);
}),
)
} catch (error) {
if (error instanceof NullError) return callback(null, null);
return callback(error);
if (error instanceof NullError) return callback(null, null)
return callback(error)
}
});

})
37 changes: 18 additions & 19 deletions server/src/auth/session.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import config from "config";
import session, {MemoryStore, SessionOptions, Store} from "express-session";
import * as constructor_session from "express-session";
import MySQLStoreMeta, { MySQLStore as MySQLStoreType } from "express-mysql-session";
import * as process from "process";

import { mysql_options_schema, session_options_schema } from "@server/common/schema/config";
import config from "config"
import session, { MemoryStore, SessionOptions, Store } from "express-session"
import * as constructor_session from "express-session"
import MySQLStoreMeta, { MySQLStore as MySQLStoreType } from "express-mysql-session"
import * as process from "process"

import { mysql_options_schema, session_options_schema } from "@server/common/schema/config"

// Augment express-session with a custom SessionData object
declare module "express-session" {
interface SessionData {
generatedTeamName?: string;
redirect_to?: string;
generatedTeamName?: string
redirect_to?: string
}
}

function get_mysql_session_store(): MySQLStoreType {
const MySQLStore = MySQLStoreMeta(constructor_session);
const options = mysql_options_schema.parse(config.get("mysql.session"));
return new MySQLStore(options);
const MySQLStore = MySQLStoreMeta(constructor_session)
const options = mysql_options_schema.parse(config.get("mysql.session"))
return new MySQLStore(options)
}

function get_memory_session_store(): MemoryStore {
return new MemoryStore();
return new MemoryStore()
}

function get_session_store(): Store {
if (process.env.NODE_ENV !== "production") {
return get_memory_session_store();
return get_memory_session_store()
}

return get_mysql_session_store();
return get_mysql_session_store()
}

const sessionStore = get_session_store();
const sessionStore = get_session_store()

const session_options = session_options_schema.parse(config.get("session")) as SessionOptions;
session_options.store = sessionStore;
const session_options = session_options_schema.parse(config.get("session")) as SessionOptions
session_options.store = sessionStore

export default session(session_options);
export default session(session_options)
61 changes: 30 additions & 31 deletions server/src/auth/strategy/oauth2/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import config from "config";
import passport from "passport";
import { VerifyCallback } from "passport-oauth2";
import refresh from "passport-oauth2-refresh";
import config from "config"
import passport from "passport"
import { VerifyCallback } from "passport-oauth2"
import refresh from "passport-oauth2-refresh"

import DurHackLiveOAuth2Strategy, { DurHackLiveProfile } from "@server/auth/strategy/oauth2/strategy";
import { oauth2_client_options_schema } from "@server/common/schema/config";
import User from "@server/database/tables/user";
import DurHackLiveOAuth2Strategy, { DurHackLiveProfile } from "@server/auth/strategy/oauth2/strategy"
import { oauth2_client_options_schema } from "@server/common/schema/config"
import User from "@server/database/tables/user"


const oauth2_options = oauth2_client_options_schema.parse(config.get("passport.oauth2"));
const oauth2_options = oauth2_client_options_schema.parse(config.get("passport.oauth2"))

/**
* Verify function for Passport.js 'oauth2' strategy (OAuth2 flow via DurHack Live)
Expand All @@ -18,37 +17,37 @@ const oauth2_options = oauth2_client_options_schema.parse(config.get("passport.o
* @param profile - profile obtained from OAuth2 provider by HTTP request
* @param done - callback function to call with (error, user) when user is found or error encountered
*/
async function oauth2VerifyFunction(accessToken: string, refreshToken: string, profile: DurHackLiveProfile, done: VerifyCallback) {
const timestamp = new Date();

let user: User;
let _is_new: boolean;
async function oauth2VerifyFunction(

Check failure on line 20 in server/src/auth/strategy/oauth2/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Missing return type on function
accessToken: string,
refreshToken: string,
profile: DurHackLiveProfile,
done: VerifyCallback,
) {
const timestamp = new Date()

let user: User
let _is_new: boolean
try {
[user, _is_new] = await User.findOrCreate(
{
where: { email: profile.email },
defaults: {
...profile,
initially_logged_in_at: timestamp,
}
;[user, _is_new] = await User.findOrCreate({

Check failure on line 31 in server/src/auth/strategy/oauth2/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unnecessary semicolon
where: { email: profile.email },
defaults: {
...profile,
initially_logged_in_at: timestamp,
},
);
})
} catch (error: any) {

Check failure on line 38 in server/src/auth/strategy/oauth2/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
return done(error);
return done(error)
}

await user.update({
...profile,
last_logged_in_at: timestamp,
});
})

return done(null, user);
return done(null, user)
}

const oauth2_strategy = new DurHackLiveOAuth2Strategy(
oauth2_options,
oauth2VerifyFunction,
);
const oauth2_strategy = new DurHackLiveOAuth2Strategy(oauth2_options, oauth2VerifyFunction)

passport.use("oauth2", oauth2_strategy);
refresh.use("oauth2", oauth2_strategy);
passport.use("oauth2", oauth2_strategy)
refresh.use("oauth2", oauth2_strategy)
53 changes: 24 additions & 29 deletions server/src/auth/strategy/oauth2/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
import OAuth2Strategy from "passport-oauth2";
import fetch, { Response } from "node-fetch";
import { z } from "zod";
import config from "config";
import createHttpError from "http-errors";

import { UserRole } from "@server/common/model_enums";
import OAuth2Strategy from "passport-oauth2"
import fetch, { Response } from "node-fetch"
import { z } from "zod"
import config from "config"
import createHttpError from "http-errors"

import { UserRole } from "@server/common/model_enums"

const DurHackLiveProfileSchema = z.object({
email: z.string(),
role: z.nativeEnum(UserRole),
preferred_name: z.string(),
});
})

export type DurHackLiveProfile = z.infer<typeof DurHackLiveProfileSchema>;
export type DurHackLiveProfile = z.infer<typeof DurHackLiveProfileSchema>

const profile_url = z.string().url().parse(config.get("passport.oauth2.profileURL"));
const profile_url = z.string().url().parse(config.get("passport.oauth2.profileURL"))

export default class DurHackLiveOAuth2Strategy extends OAuth2Strategy {
async userProfile(accessToken: string, done: (err?: (Error | null), profile?: DurHackLiveProfile) => void) {

let profileResponse: Response;
async userProfile(accessToken: string, done: (err?: Error | null, profile?: DurHackLiveProfile) => void) {

Check failure on line 20 in server/src/auth/strategy/oauth2/strategy.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Missing return type on function
let profileResponse: Response
try {
profileResponse = await fetch(
profile_url,
{
headers: {
Authorization: `Bearer ${accessToken}`
}
}
);
profileResponse = await fetch(profile_url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
} catch (error: any) {

Check failure on line 28 in server/src/auth/strategy/oauth2/strategy.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
return done(error);
return done(error)
}

if (!profileResponse.ok) {
return done(new createHttpError.BadGateway("Couldn't fetch user profile from DurHack Live."));
return done(new createHttpError.BadGateway("Couldn't fetch user profile from DurHack Live."))
}

try {
const profileJSON: any = await profileResponse.json();
const profile = DurHackLiveProfileSchema.parse(profileJSON?.data);
return done(null, profile);
} catch(error) {
return done(new createHttpError.BadGateway("Couldn't fetch user profile from DurHack Live."));
const profileJSON: any = await profileResponse.json()

Check failure on line 37 in server/src/auth/strategy/oauth2/strategy.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
const profile = DurHackLiveProfileSchema.parse(profileJSON?.data)
return done(null, profile)
} catch (error) {
return done(new createHttpError.BadGateway("Couldn't fetch user profile from DurHack Live."))
}
}
}
}
Loading

0 comments on commit 6dfd993

Please sign in to comment.