diff options
-rw-r--r-- | src/server/ApiManagers/GeneralGoogleManager.ts | 9 | ||||
-rw-r--r-- | src/server/apis/google/GoogleApiServerUtils.ts | 24 | ||||
-rw-r--r-- | src/server/authentication/DashUserModel.ts | 14 | ||||
-rw-r--r-- | src/server/authentication/Passport.ts | 6 |
4 files changed, 26 insertions, 27 deletions
diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts index 7581eec13..4f0b8c02b 100644 --- a/src/server/ApiManagers/GeneralGoogleManager.ts +++ b/src/server/ApiManagers/GeneralGoogleManager.ts @@ -17,10 +17,9 @@ export default class GeneralGoogleManager extends ApiManager { method: Method.GET, subscription: '/readGoogleAccessToken', secureHandler: async ({ user, res }) => { - const { credentials } = await GoogleApiServerUtils.retrieveCredentials(user); + const { credentials } = await GoogleApiServerUtils.retrieveCredentials(user.id); if (!credentials?.access_token) { - const url = GoogleApiServerUtils.generateAuthenticationUrl(); - return res.send(url); + return res.send(GoogleApiServerUtils.generateAuthenticationUrl()); } return res.send(credentials); }, @@ -49,7 +48,7 @@ export default class GeneralGoogleManager extends ApiManager { secureHandler: async ({ req, res, user }) => { const sector: GoogleApiServerUtils.Service = req.params.sector as GoogleApiServerUtils.Service; const action: GoogleApiServerUtils.Action = req.params.action as GoogleApiServerUtils.Action; - const endpoint = await GoogleApiServerUtils.GetEndpoint(GoogleApiServerUtils.Service[sector], user); + const endpoint = await GoogleApiServerUtils.GetEndpoint(GoogleApiServerUtils.Service[sector], user.id); const handler = EndpointHandlerMap.get(action); if (endpoint && handler) { try { @@ -71,7 +70,7 @@ export default class GeneralGoogleManager extends ApiManager { subscription: new RouteSubscriber('googleTasks').add('create'), secureHandler: async ({ req, res, user }) => { try { - const auth = await GoogleApiServerUtils.retrieveOAuthClient(user); + const auth = await GoogleApiServerUtils.retrieveOAuthClient(user.id); if (!auth) { return res.status(401).send('Google credentials missing or invalid.'); diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts index 24905896d..ad0f0e580 100644 --- a/src/server/apis/google/GoogleApiServerUtils.ts +++ b/src/server/apis/google/GoogleApiServerUtils.ts @@ -6,7 +6,6 @@ import * as request from 'request-promise'; import { Opt } from '../../../fields/Doc'; import { Database } from '../../database'; import { GoogleCredentialsLoader } from './CredentialsLoader'; -import { DashUserModel } from '../../authentication/DashUserModel'; /** * Scopes give Google users fine granularity of control @@ -59,7 +58,6 @@ export namespace GoogleApiServerUtils { */ export function processProjectCredentials(): void { const { client_secret: clientSecret, client_id: clientId, redirect_uris: redirectUris } = GoogleCredentialsLoader.ProjectCredentials; - console.log('Loaded Google redirect URIs:', redirectUris); // initialize the global authorization client oAuthOptions = { clientId, @@ -120,8 +118,8 @@ export namespace GoogleApiServerUtils { * @param userId the id of the Dash user making the request to the API * @returns the relevant 'googleapis' wrapper, if any */ - export async function GetEndpoint(sector: string, user: DashUserModel): Promise<Endpoint | void> { - const auth = await retrieveOAuthClient(user); + export async function GetEndpoint(sector: string, userId: string): Promise<Endpoint | void> { + const auth = await retrieveOAuthClient(userId); if (!auth) { return; } @@ -147,14 +145,14 @@ export namespace GoogleApiServerUtils { * npm-installed API wrappers that use authenticated client instances rather than access codes for * security. */ - export async function retrieveOAuthClient(user: DashUserModel): Promise<OAuth2Client | void> { - const { credentials, refreshed } = await retrieveCredentials(user); + export async function retrieveOAuthClient(userId: string): Promise<OAuth2Client | void> { + const { credentials, refreshed } = await retrieveCredentials(userId); if (!credentials) { return; } - let client = authenticationClients.get(user.id); + let client = authenticationClients.get(userId); if (!client) { - authenticationClients.set(user.id, (client = generateClient(credentials))); + authenticationClients.set(userId, (client = generateClient(credentials))); } else if (refreshed) { client.setCredentials(credentials); } @@ -269,15 +267,15 @@ export namespace GoogleApiServerUtils { * @returns the credentials, or undefined if the user has no stored associated credentials, * and a flag indicating whether or not they were refreshed during retrieval */ - export async function retrieveCredentials(user: DashUserModel): Promise<{ credentials: Opt<EnrichedCredentials>; refreshed: boolean }> { - let credentials = await Database.Auxiliary.GoogleAccessToken.Fetch(user.id); + export async function retrieveCredentials(userId: string): Promise<{ credentials: Opt<EnrichedCredentials>; refreshed: boolean }> { + let credentials = await Database.Auxiliary.GoogleAccessToken.Fetch(userId); let refreshed = false; if (!credentials) { return { credentials: undefined, refreshed }; } // check for token expiry if (credentials.expiry_date! <= new Date().getTime()) { - credentials = { ...credentials, ...(await refreshAccessToken(credentials, user)) }; + credentials = { ...credentials, ...(await refreshAccessToken(credentials, userId)) }; refreshed = true; } return { credentials, refreshed }; @@ -293,7 +291,7 @@ export namespace GoogleApiServerUtils { * his/her credentials be refreshed * @returns the updated credentials */ - async function refreshAccessToken(credentials: Credentials, user: DashUserModel): Promise<Credentials> { + async function refreshAccessToken(credentials: Credentials, userId: string): Promise<Credentials> { const headerParameters = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; const { client_id, client_secret } = GoogleCredentialsLoader.ProjectCredentials; const params = new URLSearchParams({ @@ -309,7 +307,7 @@ export namespace GoogleApiServerUtils { // expires_in is in seconds, but we're building the new expiry date in milliseconds const expiry_date = new Date().getTime() + expires_in * 1000; - await Database.Auxiliary.GoogleAccessToken.Update(user.id, access_token, expiry_date); + await Database.Auxiliary.GoogleAccessToken.Update(userId, access_token, expiry_date); // update the relevant properties credentials.access_token = access_token; credentials.expiry_date = expiry_date; diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts index debeef60c..6fd8dd593 100644 --- a/src/server/authentication/DashUserModel.ts +++ b/src/server/authentication/DashUserModel.ts @@ -2,9 +2,10 @@ import * as bcrypt from 'bcrypt-nodejs'; import * as mongoose from 'mongoose'; import { Utils } from '../../Utils'; -type comparePasswordFunction = (candidatePassword: string, cb: (err: any, isMatch: any) => void) => void; -export type DashUserModel = mongoose.Document & { - email: String; +type comparePasswordFunction = (candidatePassword: string, cb: (err: Error, isMatch: boolean) => void) => void; +type mongooseDocument = { id: string }; // & mongoose.Document; +export type DashUserModel = mongooseDocument & { + email: string; password: string; passwordResetToken?: string; passwordResetExpires?: Date; @@ -65,12 +66,13 @@ const userSchema = new mongoose.Schema( /** * Password hash middleware. */ -userSchema.pre('save', function save(next) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +userSchema.pre('save', function save(next: any) { const user = this; if (!user.isModified('password')) { return next(); } - bcrypt.genSalt(10, (err: any, salt: string) => { + bcrypt.genSalt(10, (err: Error, salt: string) => { if (err) { return next(err); } @@ -102,7 +104,7 @@ const comparePassword: comparePasswordFunction = function (this: DashUserModel, userSchema.methods.comparePassword = comparePassword; -const User: any = mongoose.model('User', userSchema); +const User = mongoose.model('User', userSchema); export function initializeGuest() { new User({ email: 'guest', diff --git a/src/server/authentication/Passport.ts b/src/server/authentication/Passport.ts index ca9e3058e..a62d38e3e 100644 --- a/src/server/authentication/Passport.ts +++ b/src/server/authentication/Passport.ts @@ -5,13 +5,13 @@ import User, { DashUserModel } from './DashUserModel'; const LocalStrategy = passportLocal.Strategy; passport.serializeUser<any, any>((req, user, done) => { - done(undefined, (user as any)?.id); + done(undefined, (user as DashUserModel)?.id); }); passport.deserializeUser<any, any>((id, done) => { User.findById(id) .exec() - .then((user: any) => done(undefined, user)); + .then((user: DashUserModel) => done(undefined, user)); }); // AUTHENTICATE JUST WITH EMAIL AND PASSWORD @@ -30,6 +30,6 @@ passport.use( }); } }) - .catch((error: any) => done(error)); + .catch((error: Error) => done(error)); }) ); |