Merge pull request #2175 from Budibase/feature/sso-sync-picture

Sync profile picture when using SSO
This commit is contained in:
Rory Powell 2021-07-30 11:56:42 +01:00 committed by GitHub
commit 5a83b177d2
3 changed files with 31 additions and 4 deletions

View File

@ -6,6 +6,7 @@ const { authError } = require("./utils")
const { newid } = require("../../hashing") const { newid } = require("../../hashing")
const { createASession } = require("../../security/sessions") const { createASession } = require("../../security/sessions")
const { getGlobalUserByEmail } = require("../../utils") const { getGlobalUserByEmail } = require("../../utils")
const fetch = require("node-fetch")
/** /**
* Common authentication logic for third parties. e.g. OAuth, OIDC. * 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 // create or sync the user
const response = await db.post(dbUser) const response = await db.post(dbUser)
@ -86,10 +87,26 @@ exports.authenticateThirdParty = async function (
return done(null, dbUser) 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 * @returns a user that has been sync'd with third party information
*/ */
function syncUser(user, thirdPartyUser) { async function syncUser(user, thirdPartyUser) {
// provider // provider
user.provider = thirdPartyUser.provider user.provider = thirdPartyUser.provider
user.providerType = thirdPartyUser.providerType user.providerType = thirdPartyUser.providerType
@ -112,6 +129,8 @@ function syncUser(user, thirdPartyUser) {
} }
} }
user = await syncProfilePicture(user, thirdPartyUser)
// profile // profile
user.thirdPartyProfile = { user.thirdPartyProfile = {
...profile._json, ...profile._json,

View File

@ -54,7 +54,11 @@
</Layout> </Layout>
<ActionMenu align="right"> <ActionMenu align="right">
<div slot="control" class="avatar"> <div slot="control" class="avatar">
<Avatar size="M" initials={$auth.initials} /> <Avatar
size="M"
initials={$auth.initials}
url={$auth.user.pictureUrl}
/>
<Icon size="XL" name="ChevronDown" /> <Icon size="XL" name="ChevronDown" />
</div> </div>
<MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}> <MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}>

View File

@ -100,7 +100,11 @@
<div /> <div />
<ActionMenu align="right"> <ActionMenu align="right">
<div slot="control" class="avatar"> <div slot="control" class="avatar">
<Avatar size="M" initials={$auth.initials} /> <Avatar
size="M"
initials={$auth.initials}
url={$auth.user.pictureUrl}
/>
<Icon size="XL" name="ChevronDown" /> <Icon size="XL" name="ChevronDown" />
</div> </div>
<MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}> <MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}>