mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-07 18:27:44 +02:00
When a user's name has no spaces (common for CJK names like "西门吹雪"),
name.split(' ') returns a single-element array so the last name
destructures as undefined, causing a PostgreSQL NOT NULL constraint
violation on the last_name column.
- openid: prefer standard OIDC given_name/family_name claims when
available, fall back to splitting name with safe slice(1).join()
- github: same split fix using displayName ?? username
- loginOrSignUpWithProvider: add ?? '' defensive fallback at insertOne
to guard against any undefined last name reaching the DB
Fixes #10628
Signed-off-by: SaiVaraprasad Medapati <varaprasadreddy9676@gmail.com>
Co-authored-by: SaiVaraprasad Medapati <varaprasadreddy9676@gmail.com>
120 lines
3.9 KiB
TypeScript
120 lines
3.9 KiB
TypeScript
//
|
|
// Copyright © 2024 Hardcore Engineering Inc.
|
|
//
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License. You may
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
//
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
import { type AccountDB } from '@hcengineering/account'
|
|
import { type ProviderInfo } from '@hcengineering/account-client'
|
|
import { BrandingMap, concatLink, MeasureContext, getBranding, SocialIdType } from '@hcengineering/core'
|
|
import Router from 'koa-router'
|
|
import { Issuer, Strategy } from 'openid-client'
|
|
|
|
import { Passport } from '.'
|
|
import { encodeState, handleProviderAuth, safeParseAuthState } from './utils'
|
|
|
|
export function registerOpenid (
|
|
measureCtx: MeasureContext,
|
|
passport: Passport,
|
|
router: Router<any, any>,
|
|
accountsUrl: string,
|
|
dbPromise: Promise<AccountDB>,
|
|
frontUrl: string,
|
|
brandings: BrandingMap,
|
|
signUpDisabled?: boolean
|
|
): ProviderInfo | undefined {
|
|
const openidClientId = process.env.OPENID_CLIENT_ID
|
|
const openidClientSecret = process.env.OPENID_CLIENT_SECRET
|
|
const issuer = process.env.OPENID_ISSUER
|
|
const name = 'openid'
|
|
const displayName = process.env.OPENID_DISPLAY_NAME
|
|
|
|
const redirectURL = '/auth/openid/callback'
|
|
if (openidClientId === undefined || openidClientSecret === undefined || issuer === undefined) return
|
|
|
|
Issuer.discover(issuer)
|
|
.then((issuerObj) => {
|
|
measureCtx.info('Discovered issuer', { issuer: issuerObj })
|
|
|
|
const client = new issuerObj.Client({
|
|
client_id: openidClientId,
|
|
client_secret: openidClientSecret,
|
|
redirect_uris: [concatLink(accountsUrl, redirectURL)],
|
|
response_types: ['code']
|
|
})
|
|
measureCtx.info('Created OIDC client')
|
|
|
|
passport.use(
|
|
'oidc',
|
|
new Strategy({ client, passReqToCallback: true }, (req: any, tokenSet: any, userinfo: any, done: any) => {
|
|
return done(null, userinfo)
|
|
})
|
|
)
|
|
measureCtx.info('Registered OIDC strategy')
|
|
})
|
|
.catch((err) => {
|
|
measureCtx.error('Failed to create OIDC client for IdP with the provided configuration', { err })
|
|
})
|
|
|
|
router.get('/auth/openid', async (ctx, next) => {
|
|
measureCtx.info('try auth via', { provider: 'openid' })
|
|
const state = encodeState(ctx, brandings)
|
|
|
|
await passport.authenticate('oidc', {
|
|
scope: 'openid profile email',
|
|
state
|
|
})(ctx, next)
|
|
})
|
|
|
|
router.get(
|
|
redirectURL,
|
|
async (ctx, next) => {
|
|
const state = safeParseAuthState(ctx.query?.state)
|
|
const branding = getBranding(brandings, state?.branding)
|
|
|
|
await passport.authenticate('oidc', {
|
|
failureRedirect: concatLink(branding?.front ?? frontUrl, '/login')
|
|
})(ctx, next)
|
|
},
|
|
async (ctx, next) => {
|
|
const email = ctx.state.user.email
|
|
const verifiedEmail = (ctx.state.user.email_verified as boolean) ? email : ''
|
|
const nameParts = (ctx.state.user.name ?? ctx.state.user.username ?? '').split(' ')
|
|
const first: string = ctx.state.user.given_name ?? nameParts[0] ?? ''
|
|
const last: string = ctx.state.user.family_name ?? nameParts.slice(1).join(' ')
|
|
|
|
const db = await dbPromise
|
|
const redirectUrl = await handleProviderAuth(
|
|
measureCtx,
|
|
db,
|
|
brandings,
|
|
frontUrl,
|
|
'openid',
|
|
ctx.query?.state,
|
|
ctx.state?.user,
|
|
verifiedEmail,
|
|
first,
|
|
last,
|
|
{ type: SocialIdType.OIDC, value: ctx.state.user.sub },
|
|
signUpDisabled
|
|
)
|
|
|
|
if (redirectUrl !== '') {
|
|
ctx.redirect(redirectUrl)
|
|
}
|
|
|
|
await next()
|
|
}
|
|
)
|
|
|
|
return { name, displayName }
|
|
}
|