Neilk1021 commented on code in PR #7055: URL: https://github.com/apache/texera/pull/7055#discussion_r3746516037
########## amber/src/main/scala/org/apache/texera/web/resource/auth/ExternalAuthProvisioner.scala: ########## @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache 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 + * + * http://www.apache.org/licenses/LICENSE-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. + */ + +package org.apache.texera.web.resource.auth + +import com.typesafe.scalalogging.LazyLogging +import org.apache.texera.dao.SqlServer +import org.apache.texera.dao.jooq.generated.Tables.{AUTH_PROVIDER, USER} +import org.apache.texera.dao.jooq.generated.enums.{ProviderTypeEnum, UserRoleEnum} +import org.apache.texera.dao.jooq.generated.tables.daos.{AuthProviderDao, UserDao} +import org.apache.texera.dao.jooq.generated.tables.pojos.{AuthProvider, User} +import org.apache.texera.common.util.EmailUtil +import org.jooq.DSLContext +import org.jooq.impl.DSL + +import java.time.OffsetDateTime +import scala.util.chaining.scalaUtilChainingOps + +/** + * A verified external identity (Google, Facebook, ...) reduced to the fields we persist. + */ +final case class ExternalProfile( + providerType: ProviderTypeEnum, + providerId: String, + name: String, + email: String, + avatar: String +) + +object ExternalAuthProvisioner extends LazyLogging { + + /** + * The account owning `email`, matched case-insensitively and within the caller's transaction + * so it reads that transaction's own writes. + * + * Case-insensitivity is required, not a nicety: `"user".email` is a plain case-sensitive + * UNIQUE and `idx_user_email_lower` is not unique, so `[email protected]` and `[email protected]` can + * coexist. Registration stores the address as the user typed it while contributor + * placeholders are stored lower-cased, so the casings provably differ in practice. An + * exact-match lookup here would miss, insert a second account without violating any + * constraint, and silently strand the original account's data. + * + * Mirrors `AuthResource.fetchUserByEmailIgnoreCase`, which cannot be reused directly because + * it opens its own DSLContext. + */ + private def userByEmailIgnoreCase(ctx: DSLContext, email: String): Option[User] = + Option( + ctx + .selectFrom(USER) + .where(DSL.lower(USER.EMAIL).eq(EmailUtil.normalize(email))) + .fetchOneInto(classOf[User]) + ) + + /** + * Resolve the user behind an external identity, creating one if necessary, and + * ensure its auth-provider row is present and up to date. Runs in a single + * transaction and returns the (possibly newly created) user. + */ + def loginOrProvision(profile: ExternalProfile): User = + SqlServer.withTransaction(SqlServer.getInstance().createDSLContext()) { ctx => + val txUserDao = new UserDao(ctx.configuration()) + val txAuthDao = new AuthProviderDao(ctx.configuration()) + + Option( + ctx + .select() + .from(USER) + .join(AUTH_PROVIDER) + .on(USER.UID.eq(AUTH_PROVIDER.UID)) + .where(AUTH_PROVIDER.PROVIDER_TYPE.eq(profile.providerType)) + .and(AUTH_PROVIDER.PROVIDER_ID.eq(profile.providerId)) + .fetchOne() + ) match { + case Some(record) => + txUserDao.fetchOneByUid(record.get(USER.UID)).tap { user => + if (refresh(user, profile)) txUserDao.update(user) + } + + case None => + val user = userByEmailIgnoreCase(ctx, profile.email) match { + case Some(existing) => + existing.tap { user => + val claimed = user.getIsPlaceholder + if (claimed) AuthResource.claimPlaceholder(user) + val drifted = refresh(user, profile) + if (drifted || claimed) txUserDao.update(user) + } + case None => + val created = new User() + created.setName(profile.name) + created.setEmail(profile.email) + created.setAvatar(profile.avatar) + created.setRole(UserRoleEnum.INACTIVE) + try { + txUserDao.insert(created) + created + } catch { + case e: org.jooq.exception.DataAccessException if e.sqlState() == "23505" => + userByEmailIgnoreCase(ctx, profile.email).getOrElse(throw e) Review Comment: fixed! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
