-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
84ab7b2
commit a54344e
Showing
3 changed files
with
61 additions
and
55 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Logger } from "../../../singleton/logger.js"; | ||
const log = Logger.getLogger().child({ from: "google-strategy" }); | ||
|
||
import { Strategy as GoogleStrategy, Profile } from "passport-google-oauth20"; | ||
|
||
import { Configuration } from "../../../singleton/configuration.js"; | ||
import UserModel from "../../../model/mongo/user.js"; | ||
|
||
const onVerify = async (_: string, __: string, profile: Profile, cb: (err: Error | null, user: any) => void) => { | ||
log.info("Google profile received: %o", profile); | ||
if (!profile.emails || !profile.emails[0] || !profile.name || !profile.name.givenName || !profile.name.familyName) { | ||
return cb(new Error("No email found in Google profile."), undefined); | ||
} | ||
const existingUser = await UserModel.findOne({ email: profile.emails[0].value }).lean(); | ||
if (existingUser) { | ||
log.info("User found from Google profile."); | ||
UserModel.updateOne( | ||
{ _id: existingUser._id }, | ||
{ ssoEnabled: true, ssoProvider: "google", googleProfileId: profile.id }, | ||
).exec(); | ||
return cb(null, existingUser); | ||
} | ||
log.info("Creating user from Google profile."); | ||
const role = Configuration.get("system.role.default"); | ||
const credits = Configuration.get("user.account-creation.initial-credit-count"); | ||
let customData = Configuration.get("user.account-creation.custom-data.default-value"); | ||
try { | ||
JSON.parse(customData); | ||
} catch { | ||
customData = "{}"; | ||
log.warn("Invalid JSON found in `user.account-creation.custom-data.default-value`."); | ||
} | ||
const newUser = new UserModel({ | ||
email: profile.emails[0].value, | ||
firstName: profile.name.givenName, | ||
lastName: profile.name.familyName, | ||
username: profile.emails[0].value, | ||
emailVerified: true, | ||
ssoEnabled: true, | ||
ssoProvider: "google", | ||
googleProfileId: profile.id, | ||
scope: Configuration.get("user.account-creation.default-scope"), | ||
creationIp: "0.0.0.0", | ||
role, | ||
credits, | ||
}); | ||
const savedUser = await newUser.save(); | ||
return cb(null, savedUser); | ||
}; | ||
|
||
export const googleStrategy = new GoogleStrategy( | ||
{ | ||
clientID: Configuration.get("user.account-creation.sso.google.client-id"), | ||
clientSecret: Configuration.get("user.account-creation.sso.google.client-secret"), | ||
callbackURL: `${Configuration.get("system.app-host")}/sso/google/callback`, | ||
scope: ["profile"], | ||
}, | ||
onVerify, | ||
); |