From 73d1cb41d87e12aa1a2b60b54424023c6eb5d8a7 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 29 Jul 2021 14:43:23 +0100 Subject: [PATCH 1/2] Sync profile picture when using SSO --- .../middleware/passport/third-party-common.js | 23 +++++++++++++++++-- .../src/pages/builder/apps/index.svelte | 6 ++++- .../src/pages/builder/portal/_layout.svelte | 6 ++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/auth/src/middleware/passport/third-party-common.js b/packages/auth/src/middleware/passport/third-party-common.js index 2ab2816391..01125dfebb 100644 --- a/packages/auth/src/middleware/passport/third-party-common.js +++ b/packages/auth/src/middleware/passport/third-party-common.js @@ -6,6 +6,7 @@ const { authError } = require("./utils") const { newid } = require("../../hashing") const { createASession } = require("../../security/sessions") const { getGlobalUserByEmail } = require("../../utils") +const { default: fetch } = require("node-fetch") /** * Common authentication logic for third parties. e.g. OAuth, OIDC. @@ -65,7 +66,7 @@ exports.authenticateThirdParty = async function ( } } - dbUser = syncUser(dbUser, thirdPartyUser) + dbUser = await syncUser(dbUser, thirdPartyUser) // create or sync the user const response = await db.post(dbUser) @@ -86,10 +87,26 @@ exports.authenticateThirdParty = async function ( return done(null, dbUser) } +async function syncProfilePicture(user, thirdPartyUser) { + const pictureUrl = thirdPartyUser.profile._json.picture + if (pictureUrl) { + const response = await fetch(pictureUrl) + + if (response.status === 200) { + const type = response.headers.get("content-type") + if (type.startsWith("image/")) { + user.pictureUrl = pictureUrl + } + } + } + + return user +} + /** * @returns a user that has been sync'd with third party information */ -function syncUser(user, thirdPartyUser) { +async function syncUser(user, thirdPartyUser) { // provider user.provider = thirdPartyUser.provider user.providerType = thirdPartyUser.providerType @@ -112,6 +129,8 @@ function syncUser(user, thirdPartyUser) { } } + user = await syncProfilePicture(user, thirdPartyUser) + // profile user.thirdPartyProfile = { ...profile._json, diff --git a/packages/builder/src/pages/builder/apps/index.svelte b/packages/builder/src/pages/builder/apps/index.svelte index 69b6e770f6..c1bdc31cd4 100644 --- a/packages/builder/src/pages/builder/apps/index.svelte +++ b/packages/builder/src/pages/builder/apps/index.svelte @@ -54,7 +54,11 @@
- +
userInfoModal.show()}> diff --git a/packages/builder/src/pages/builder/portal/_layout.svelte b/packages/builder/src/pages/builder/portal/_layout.svelte index 4bc0b46167..20bdc9cb54 100644 --- a/packages/builder/src/pages/builder/portal/_layout.svelte +++ b/packages/builder/src/pages/builder/portal/_layout.svelte @@ -100,7 +100,11 @@
- +
userInfoModal.show()}> From dd8b1361d82a907f86a7d507d9966d4655bb18c3 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 29 Jul 2021 14:51:35 +0100 Subject: [PATCH 2/2] Fix import --- packages/auth/src/middleware/passport/third-party-common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/auth/src/middleware/passport/third-party-common.js b/packages/auth/src/middleware/passport/third-party-common.js index 01125dfebb..54252b329d 100644 --- a/packages/auth/src/middleware/passport/third-party-common.js +++ b/packages/auth/src/middleware/passport/third-party-common.js @@ -6,7 +6,7 @@ const { authError } = require("./utils") const { newid } = require("../../hashing") const { createASession } = require("../../security/sessions") const { getGlobalUserByEmail } = require("../../utils") -const { default: fetch } = require("node-fetch") +const fetch = require("node-fetch") /** * Common authentication logic for third parties. e.g. OAuth, OIDC.