@@ -280,6 +280,67 @@ public BinderChannelBuilder preAuthorizeServers(boolean preAuthorize) {
280280 return this ;
281281 }
282282
283+ /**
284+ * Specifies how and when to authorize a server against this Channel's {@link SecurityPolicy}.
285+ *
286+ * <p>This method selects the original "legacy" authorization strategy, which is no longer
287+ * preferred for two reasons: First, the legacy strategy considers the UID of the server *process*
288+ * we connect to. This is problematic for services using the `android:isolatedProcess` attribute,
289+ * which runs them under a different "ephemeral" UID. This UID lacks all the privileges of the
290+ * hosting app -- any non-trivial SecurityPolicy would fail to authorize it. Second, the legacy
291+ * authorization strategy performs SecurityPolicy checks later in the connection handshake, which
292+ * means the calling UID must be rechecked on every subsequent RPC. For these reasons, prefer
293+ * {@link #useV2AuthStrategy} instead.
294+ *
295+ * <p>The server does not know which authorization strategy a client is using. Both strategies
296+ * work with all versions of the grpc-binder server.
297+ *
298+ * <p>Callers need not specify an authorization strategy, but the default is unspecified and will
299+ * eventually become {@link #useV2AuthStrategy()}. Clients that require the legacy strategy should
300+ * configure it explicitly using this method. Eventually, however, legacy support will be
301+ * deprecated and removed.
302+ *
303+ * @return this
304+ */
305+ @ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/12397" )
306+ public BinderChannelBuilder useLegacyAuthStrategy () {
307+ transportFactoryBuilder .setUseLegacyAuthStrategy (true );
308+ return this ;
309+ }
310+
311+ /**
312+ * Specifies how and when to authorize a server against this Channel's {@link SecurityPolicy}.
313+ *
314+ * <p>This method selects the v2 authorization strategy. It improves on the original strategy
315+ * ({@link #useLegacyAuthStrategy}), by considering the UID of the server *app* we connect to,
316+ * rather than the server *process*. This allows clients to connect to services configured with
317+ * the `android:isolatedProcess` attribute, which run with the same authority as the hosting app,
318+ * but under a different "ephemeral" UID that any non-trivial SecurityPolicy would fail to
319+ * authorize.
320+ *
321+ * <p>Furthermore, the v2 authorization strategy performs SecurityPolicy checks earlier in the
322+ * connection handshake, which allows subsequent RPCs over that connection to proceed securely
323+ * without further UID checks. For these reasons, clients should prefer the v2 strategy.
324+ *
325+ * <p>The server does not know which authorization strategy a client is using. Both strategies
326+ * work with all versions of the grpc-binder server.
327+ *
328+ * <p>Callers need not specify an authorization strategy, but the default is unspecified and can
329+ * change over time. Clients that require the v2 strategy should configure it explicitly using
330+ * this method. Eventually, this strategy will become the default and legacy support will be
331+ * removed.
332+ *
333+ * <p>If moving to the new authorization strategy causes a robolectric test to fail, ensure your
334+ * fake Service component is registered with `ShadowPackageManager` using `addOrUpdateService()`.
335+ *
336+ * @return this
337+ */
338+ @ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/12397" )
339+ public BinderChannelBuilder useV2AuthStrategy () {
340+ transportFactoryBuilder .setUseLegacyAuthStrategy (false );
341+ return this ;
342+ }
343+
283344 @ Override
284345 public BinderChannelBuilder idleTimeout (long value , TimeUnit unit ) {
285346 checkState (
0 commit comments