From 645df1d00f953524c6da22103d26c38ae4331cd6 Mon Sep 17 00:00:00 2001 From: Skitty1238 <157652284+Skitty1238@users.noreply.github.com> Date: Wed, 4 Jun 2025 13:59:34 -0400 Subject: partial google calendar task integration + fix with dark mode colors for task nodes so that text is still visible --- src/server/authentication/DashUserModel.ts | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/server/authentication/DashUserModel.ts') diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts index debeef60c..4397e2bd4 100644 --- a/src/server/authentication/DashUserModel.ts +++ b/src/server/authentication/DashUserModel.ts @@ -9,9 +9,14 @@ export type DashUserModel = mongoose.Document & { passwordResetToken?: string; passwordResetExpires?: Date; + + // AARAV ADD + googleToken?: string; + dropboxRefresh?: string; dropboxToken?: string; + userDocumentId: string; sharingDocumentId: string; linkDatabaseId: string; @@ -40,6 +45,7 @@ const userSchema = new mongoose.Schema( passwordResetToken: String, passwordResetExpires: Date, + googleToken: String, dropboxRefresh: String, dropboxToken: String, userDocumentId: String, // id that identifies a document which hosts all of a user's account data -- cgit v1.2.3-70-g09d2 From 59689fe94c27986674dd6ecb7f0e6073861a98a6 Mon Sep 17 00:00:00 2001 From: bobzel Date: Wed, 4 Jun 2025 22:07:36 -0400 Subject: more cleanup of google authorization --- src/server/ApiManagers/GeneralGoogleManager.ts | 12 +----------- src/server/apis/google/GoogleApiServerUtils.ts | 7 +++---- src/server/authentication/DashUserModel.ts | 6 ------ 3 files changed, 4 insertions(+), 21 deletions(-) (limited to 'src/server/authentication/DashUserModel.ts') diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts index 693b17779..7581eec13 100644 --- a/src/server/ApiManagers/GeneralGoogleManager.ts +++ b/src/server/ApiManagers/GeneralGoogleManager.ts @@ -99,17 +99,7 @@ export default class GeneralGoogleManager extends ApiManager { secureHandler: async ({ user, req, res }) => new Promise(resolve => GoogleApiServerUtils.processNewUser(user.id, req.query.code as string) - .then(enriched => { - if (enriched.refresh_token) { - if (enriched.refresh_token) { - user.googleToken = enriched.refresh_token; - user.save(); - } else { - console.warn('No refresh token returned'); - } - } - res.status(200).send('Google account linked successfully!'); - }) + .then(() => res.status(200).send('Google account linked successfully!')) .catch(err => { console.error('Failed to process Google code:', err); res.status(500).send('Error linking Google account'); diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts index 45c661730..24905896d 100644 --- a/src/server/apis/google/GoogleApiServerUtils.ts +++ b/src/server/apis/google/GoogleApiServerUtils.ts @@ -14,7 +14,6 @@ import { DashUserModel } from '../../authentication/DashUserModel'; * This is the somewhat overkill list of what Dash requests * from the user. */ -// 'https://www.googleapis.com/auth/tasks', 'openid', 'profile' const scope = ['tasks', 'documents.readonly', 'documents', 'presentations', 'presentations.readonly', 'drive', 'drive.file', 'photoslibrary', 'photoslibrary.appendonly', 'photoslibrary.sharing', 'userinfo.profile'].map( relative => `https://www.googleapis.com/auth/${relative}` ); @@ -122,8 +121,7 @@ export namespace GoogleApiServerUtils { * @returns the relevant 'googleapis' wrapper, if any */ export async function GetEndpoint(sector: string, user: DashUserModel): Promise { - if (!user.googleToken) await retrieveOAuthClient(user); - const auth = user.googleToken; // await retrieveOAuthClient(user); + const auth = await retrieveOAuthClient(user); if (!auth) { return; } @@ -299,7 +297,7 @@ export namespace GoogleApiServerUtils { const headerParameters = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; const { client_id, client_secret } = GoogleCredentialsLoader.ProjectCredentials; const params = new URLSearchParams({ - refresh_token: credentials.refresh_token!, // AARAV use user.googleToken + refresh_token: credentials.refresh_token!, client_id, client_secret, grant_type: 'refresh_token', @@ -308,6 +306,7 @@ export namespace GoogleApiServerUtils { const { access_token, expires_in } = await new Promise<{ access_token: string; expires_in: number }>(resolve => { request.post(url, headerParameters).then(response => resolve(JSON.parse(response))); }); + // 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); diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts index 4397e2bd4..debeef60c 100644 --- a/src/server/authentication/DashUserModel.ts +++ b/src/server/authentication/DashUserModel.ts @@ -9,14 +9,9 @@ export type DashUserModel = mongoose.Document & { passwordResetToken?: string; passwordResetExpires?: Date; - - // AARAV ADD - googleToken?: string; - dropboxRefresh?: string; dropboxToken?: string; - userDocumentId: string; sharingDocumentId: string; linkDatabaseId: string; @@ -45,7 +40,6 @@ const userSchema = new mongoose.Schema( passwordResetToken: String, passwordResetExpires: Date, - googleToken: String, dropboxRefresh: String, dropboxToken: String, userDocumentId: String, // id that identifies a document which hosts all of a user's account data -- cgit v1.2.3-70-g09d2 From d917449cb195fd151f6c3558a476a95e6675e2f3 Mon Sep 17 00:00:00 2001 From: bobzel Date: Wed, 4 Jun 2025 22:30:58 -0400 Subject: more typing/cleanup for google authentication and dashUserModel --- src/server/ApiManagers/GeneralGoogleManager.ts | 6 +++--- src/server/apis/google/GoogleApiServerUtils.ts | 24 +++++++++++------------- src/server/authentication/DashUserModel.ts | 14 ++++++++------ src/server/authentication/Passport.ts | 6 +++--- 4 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src/server/authentication/DashUserModel.ts') diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts index 7581eec13..81efc3eb5 100644 --- a/src/server/ApiManagers/GeneralGoogleManager.ts +++ b/src/server/ApiManagers/GeneralGoogleManager.ts @@ -17,7 +17,7 @@ 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 as string); if (!credentials?.access_token) { const url = GoogleApiServerUtils.generateAuthenticationUrl(); return res.send(url); @@ -49,7 +49,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 +71,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 { - const auth = await retrieveOAuthClient(user); + export async function GetEndpoint(sector: string, userId: string): Promise { + 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 { - const { credentials, refreshed } = await retrieveCredentials(user); + export async function retrieveOAuthClient(userId: string): Promise { + 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; refreshed: boolean }> { - let credentials = await Database.Auxiliary.GoogleAccessToken.Fetch(user.id); + export async function retrieveCredentials(userId: string): Promise<{ credentials: Opt; 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 { + async function refreshAccessToken(credentials: Credentials, userId: string): Promise { 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((req, user, done) => { - done(undefined, (user as any)?.id); + done(undefined, (user as DashUserModel)?.id); }); passport.deserializeUser((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)); }) ); -- cgit v1.2.3-70-g09d2