-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbfa576
commit 6dfd993
Showing
43 changed files
with
1,107 additions
and
1,327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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) { | ||
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() | ||
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.")) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.