Yicong-Huang commented on code in PR #7055:
URL: https://github.com/apache/texera/pull/7055#discussion_r3718497131


##########
amber/src/main/scala/org/apache/texera/web/resource/auth/ExternalAuthProvisioner.scala:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.jooq.DSLContext
+
+import java.net.URI
+import java.time.OffsetDateTime
+import javax.ws.rs.NotAuthorizedException
+import scala.util.chaining.scalaUtilChainingOps
+import scala.util.Try
+
+/**
+  * A verified external identity (Google, Facebook, ...) reduced to the fields 
we
+  * persist. `avatar` is the complete URL the provider supplied, and is 
optional:
+  * `None` means the provider supplies no avatar, so the user's existing 
avatar column
+  * is left untouched rather than overwritten.
+  *
+  * `emailVerified` reports whether the provider itself vouches for `email`. 
It has no
+  * default on purpose: an email address is what links an external identity to 
an
+  * existing account, so treating an unverified one as trusted is an 
account-takeover
+  * path, and a defaulted flag is how that mistake comes back.
+  */
+final case class ExternalProfile(
+    providerType: ProviderTypeEnum,
+    providerId: String,
+    name: String,
+    email: String,
+    emailVerified: Boolean,
+    avatar: Option[String] = None
+)
+
+object ExternalAuthProvisioner extends LazyLogging {
+  // ── avatar host allowlist ──
+  private val ALLOWED_AVATAR_HOST_SUFFIXES: Set[String] = Set(
+    "googleusercontent.com"
+  )
+
+  /** Allow an exact host or any subdomain of an allowlisted suffix. */
+  private[auth] def isAllowedAvatarHost(host: String): Boolean = {
+    if (host == null || host.isEmpty) return false
+    val lower = host.toLowerCase
+    ALLOWED_AVATAR_HOST_SUFFIXES.exists(suffix => lower == suffix || 
lower.endsWith("." + suffix))
+  }
+
+  /**
+    * The avatar URL to persist, or `None` to leave the stored value alone. 
Anything that is not
+    * an http(s) URL on an allowlisted host is dropped rather than rejected: a 
surprising avatar
+    * is not a reason to deny someone a login, and treating it as "provider 
supplied no avatar"
+    * falls back to the initials avatar.
+    */
+  private[auth] def sanitizedAvatar(profile: ExternalProfile): Option[String] =

Review Comment:
   `sanitizedAvatar` bounds the scheme and the host but not the length, and 
`"user".avatar` is `VARCHAR(512)`. A longer URL becomes a 22001 
`DataAccessException`, and `refresh`'s `txUserDao.update` at `:118` has no 
catch at all (the create branch only handles 23505), so it surfaces as a 500 on 
login rather than the dropped avatar this method is otherwise careful to 
degrade to.
   
   Today's allowlist admits only `googleusercontent.com`, whose URLs run about 
100 chars, so it is not reachable yet. But this class exists precisely to take 
on more providers, and the repo already has the pattern: 
`DatasetResource.insertContributors` rejects fields over 256 chars explicitly. 
One `.filter(_.length <= 512)` in this single funnel closes it.
   
   Since `sanitizedAvatar` is part of the avatar-representation change I am 
asking to split out (see the review body), this belongs in that PR rather than 
here.



##########
amber/src/test/scala/org/apache/texera/web/resource/auth/GoogleAuthResourceSpec.scala:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.google.api.client.googleapis.auth.oauth2.GoogleIdToken
+import org.apache.texera.common.config.UserSystemConfig
+import org.apache.texera.dao.MockTexeraDB
+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.UserDao
+import org.apache.texera.dao.jooq.generated.tables.pojos.User
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
+
+import javax.ws.rs.NotAuthorizedException
+
+/**
+  * Integration spec for [[GoogleAuthResource]] against embedded Postgres.
+  *
+  * Token verification is the one part that cannot run here — it needs a 
Google-signed JWT and a
+  * network round trip — so the suite overrides `verifiedPayload` and drives 
the resource with
+  * payloads built by hand. What it pins down is everything downstream of 
verification: how a
+  * Google payload becomes an [[ExternalProfile]] (the name fallback and the 
avatar reduced to
+  * its last path segment) and that an unverifiable credential is a 401 rather 
than a crash.

Review Comment:
   This describes the behavior the PR removes, and the suite's own `it should 
"store the picture URL in full"` at `:153` asserts the opposite.
   
   ```suggestion
     * Google payload becomes an [[ExternalProfile]] (the name fallback and the 
picture URL kept
     * in full) and that an unverifiable credential is a 401 rather than a 
crash.
   ```



##########
amber/src/test/scala/org/apache/texera/web/resource/auth/AuthResourceSpec.scala:
##########
@@ -61,19 +62,22 @@ class AuthResourceSpec
 
   override protected def beforeEach(): Unit = {
     userDao = new UserDao(getDSLContext.configuration())
+    authDao = new AuthProviderDao(getDSLContext.configuration())
     resource = new AuthResource()
     cleanup()
   }
 
   override protected def afterEach(): Unit = cleanup()
 
+  // The auth_provider FK is ON DELETE CASCADE, so deleting the user clears 
its credential rows.
   private def cleanup(): Unit = {
     // startsWith escapes SQL LIKE wildcards, so the literal "authspec_" 
prefix is matched exactly.
     
getDSLContext.deleteFrom(USER).where(USER.NAME.startsWith("authspec_")).execute()
     // createAdminUser() seeds the configured admin — remove it too so the 
test starts clean.
     
getDSLContext.deleteFrom(USER).where(USER.NAME.eq(UserSystemConfig.adminUsername)).execute()
   }
 
+  /** Seed a user plus the LOCAL auth_provider row it logs in with, mirroring 
`insertLocalUser`. */

Review Comment:
   `insertLocalUser` no longer exists anywhere in the repo; it was renamed away 
by this PR's own refactor. The seam this mirrors is 
`LocalAuthProvisioner.createLocalAccount`.
   
   ```suggestion
     /**
       * Seed a user plus the LOCAL auth_provider row it logs in with, mirroring
       * `LocalAuthProvisioner.createLocalAccount`.
       */
   ```



##########
amber/src/main/scala/org/apache/texera/web/resource/auth/ExternalAuthProvisioner.scala:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.jooq.DSLContext
+
+import java.net.URI
+import java.time.OffsetDateTime
+import javax.ws.rs.NotAuthorizedException
+import scala.util.chaining.scalaUtilChainingOps
+import scala.util.Try
+
+/**
+  * A verified external identity (Google, Facebook, ...) reduced to the fields 
we
+  * persist. `avatar` is the complete URL the provider supplied, and is 
optional:
+  * `None` means the provider supplies no avatar, so the user's existing 
avatar column
+  * is left untouched rather than overwritten.
+  *
+  * `emailVerified` reports whether the provider itself vouches for `email`. 
It has no
+  * default on purpose: an email address is what links an external identity to 
an
+  * existing account, so treating an unverified one as trusted is an 
account-takeover
+  * path, and a defaulted flag is how that mistake comes back.
+  */
+final case class ExternalProfile(
+    providerType: ProviderTypeEnum,
+    providerId: String,
+    name: String,
+    email: String,
+    emailVerified: Boolean,
+    avatar: Option[String] = None
+)
+
+object ExternalAuthProvisioner extends LazyLogging {
+  // ── avatar host allowlist ──
+  private val ALLOWED_AVATAR_HOST_SUFFIXES: Set[String] = Set(
+    "googleusercontent.com"
+  )
+
+  /** Allow an exact host or any subdomain of an allowlisted suffix. */
+  private[auth] def isAllowedAvatarHost(host: String): Boolean = {
+    if (host == null || host.isEmpty) return false
+    val lower = host.toLowerCase
+    ALLOWED_AVATAR_HOST_SUFFIXES.exists(suffix => lower == suffix || 
lower.endsWith("." + suffix))
+  }
+
+  /**
+    * The avatar URL to persist, or `None` to leave the stored value alone. 
Anything that is not
+    * an http(s) URL on an allowlisted host is dropped rather than rejected: a 
surprising avatar
+    * is not a reason to deny someone a login, and treating it as "provider 
supplied no avatar"
+    * falls back to the initials avatar.
+    */
+  private[auth] def sanitizedAvatar(profile: ExternalProfile): Option[String] =
+    profile.avatar.filter { url =>
+      val host = Try(URI.create(url)).toOption
+        .filter { uri =>
+          val scheme = Option(uri.getScheme).map(_.toLowerCase)
+          scheme.contains("http") || scheme.contains("https")
+        }
+        .flatMap(uri => Option(uri.getHost))
+
+      val allowed = host.exists(isAllowedAvatarHost)
+      if (!allowed) {
+        logger.warn(
+          s"Ignoring avatar from ${profile.providerType} identity 
${profile.providerId}: " +
+            s"'$url' is not an http(s) URL on an allowlisted host."
+        )
+      }
+      allowed
+    }
+
+  /**
+    * 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) =>
+          // known identity: refresh the profile fields if they drifted
+          txUserDao.fetchOneByUid(record.get(USER.UID)).tap { user =>
+            if (refresh(user, profile)) txUserDao.update(user)
+          }
+
+        case None =>
+          // First time we have seen this identity, so the email address is 
the only thing
+          // tying it to an account. It is either an existing one to link 
onto, or a new row that
+          // claims the address. Trusting an unverified address for that lets 
anyone who can
+          // mint an `email` claim take over, or squat on, someone else's 
account. The error
+          // is deliberately the same one a bad credential yields, so this 
does not become an
+          // oracle for which addresses are registered.
+          if (!profile.emailVerified) {
+            logger.warn(
+              s"Refusing to provision ${profile.providerType} identity 
${profile.providerId}: " +
+                "the provider did not verify its email address."
+            )
+            throw new NotAuthorizedException("Login credentials are 
incorrect.")
+          }
+
+          val user = Option(txUserDao.fetchOneByEmail(profile.email)) match {

Review Comment:
   This link-by-email lookup is case-sensitive, but the code it replaces was 
not: the old path used `AuthResource.fetchUserByEmailIgnoreCase` 
(`DSL.lower(USER.EMAIL).eq(EmailUtil.normalize(email))`), while 
`fetchOneByEmail` emits `email = ?`.
   
   That matters because the mismatch does not fail loudly. `"user".email` is a 
plain `UNIQUE` on `VARCHAR` (`texera_ddl.sql:108`) and `idx_user_email_lower` 
is a non-unique index (`:137`), so `[email protected]` and `[email protected]` coexist 
happily. The lookup misses, the insert below succeeds, and the user silently 
gets a second, empty account with none of their workflows.
   
   The casing does disagree in practice. `/auth/register` stores the address as 
the user typed it (your own comment at `AuthResource.scala:81-83` says so), 
while `DatasetResource.resolveContributorUid` stores contributor placeholders 
lower-cased (`DatasetResource.scala:223`), so a Google login can also fail to 
claim the placeholder that was created for it.
   
   I would make this a tx-scoped `lower(email)` lookup so it reads its own 
writes:
   
   ```scala
   val user = Option(
     ctx
       .selectFrom(USER)
       .where(DSL.lower(USER.EMAIL).eq(EmailUtil.normalize(profile.email)))
       .fetchOneInto(classOf[User])
   ) match {
   ```
   
   No suggestion block because this needs two imports the file does not have 
yet: `org.jooq.impl.DSL` and `org.apache.texera.common.util.EmailUtil`.
   
   The 23505 fallback at `:159` needs the same treatment, otherwise it cannot 
recover from the race either.
   
   Worth a test too: `AuthResourceSpec.scala:320` already covers this hazard on 
the register path, but `ExternalAuthProvisionerSpec.scala:374` seeds and 
queries the same casing, so its `userCountByEmail("linkme") shouldBe 1` cannot 
observe it.



##########
common/auth/src/main/scala/org/apache/texera/auth/JwtAuth.scala:
##########
@@ -51,14 +51,28 @@ object JwtAuth {
     jws.getCompactSerialization
   }
 
-  def jwtClaims(user: User, expireInDays: Int): JwtClaims = {
+  /**
+    * Build the claim set for `user`. The claim names are a contract with the 
hand-written
+    * TypeScript reader in 
`frontend/src/app/common/service/user/auth.service.ts`, which is not
+    * compiled against this file — so renaming one here silently breaks the 
frontend. `avatar`
+    * now lives on `"user"` rather than a Google-specific column, but the 
claim keeps its
+    * `googleAvatar` name until the frontend is migrated in lockstep.
+    *
+    * `googleId` is passed in rather than read off `user`, because the GOOGLE 
provider id lives
+    * in `auth_provider` and this module must stay DB-free: the four specs in

Review Comment:
   There are three such spec files (`AccessControlResourceSpec`, 
`LiteLLMProxyAuthSpec`, `ConfigResourceSpec`), carrying four call sites between 
them. Dropping the count also keeps it from rotting on the next added spec.
   
   ```suggestion
       * in `auth_provider` and this module must stay DB-free: the specs in
   ```



##########
amber/src/main/scala/org/apache/texera/web/resource/dashboard/admin/user/AdminUserResource.scala:
##########
@@ -72,14 +77,24 @@ class AdminUserResource {
   @Path("/list")
   @Produces(Array(MediaType.APPLICATION_JSON))
   def list(): util.List[UserInfo] = {
+
+    val googleProvider = AUTH_PROVIDER.as("google_provider")
+    val localProvider = AUTH_PROVIDER.as("local_provider")
+
     AdminUserResource.context
       .select(
         USER.UID,
         USER.NAME,
         USER.EMAIL,
-        USER.GOOGLE_ID,
+        // fetchInto maps onto a Scala case class POSITIONALLY, not by name: a 
case class has no
+        // no-arg constructor, so jOOQ falls through to ImmutablePOJOMapper. 
So the column order
+        // below must track the UserInfo field order. `last_active_time` 
landing on `lastLogin` two
+        // entries down only works because of that. The aliases are 
documentation (both joins
+        // project a column called `provider_id`); they do not drive the 
mapping.
+        googleProvider.PROVIDER_ID.as("googleId"),
+        localProvider.PROVIDER_ID.as("localHandle"),

Review Comment:
   `localHandle` and the `local_provider` join that feeds it have no reader. 
`grep -rn "localHandle" frontend/src` comes back empty.
   
   What makes that worth raising rather than a nit: this field is exactly what 
the admin UI now needs. After this PR `"user".name` is no longer the login 
handle, yet `admin-user.component.html:197,209` still presents it as the user's 
identifier, and the real handle is unreachable. `/admin/user/update` writes 
only `USER.NAME` (`:126`), and nothing outside 
`ExternalAuthProvisioner.upsertProvider` (external identities only) ever writes 
`auth_provider.provider_id`. So an admin can neither see nor change what a 
local user actually types to log in.
   
   I would wire `localHandle` into the admin list. That is the change which 
makes the schema split legible to an admin, and the data is already in hand 
here. If you would rather defer it to a follow-up, drop the field and its join 
for now so the API does not carry an unread column.



##########
amber/src/main/scala/org/apache/texera/web/resource/auth/AuthResource.scala:
##########
@@ -169,9 +196,8 @@ class AuthResource {
         user.setName(username)
         user.setEmail(useremail)
         user.setRole(UserRoleEnum.RESTRICTED)
-        // hash the plain text password
-        user.setPassword(new 
StrongPasswordEncryptor().encryptPassword(userpassword))
-        userDao.insert(user)
+        // Loses the race to a concurrent registration of the same handle as a 
409.

Review Comment:
   This one does not quite parse: there is no subject, and one reports losing a 
race as a 409 rather than losing it "as a 409".
   
   ```suggestion
           // Reports losing the race to a concurrent registration of the same 
handle as a 409.
   ```



-- 
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]

Reply via email to