Avoid explicit instantiation of the SamlClientProvider

Bind Provider<SAML2Client> to SamlClientProvider in a Guice module and
make sure to install that module using gerrit.installModule option.

Once Guice knows Provider<SAML2Client> binding we can also inject just
the SAML2Client.

Change-Id: Id16e9f55cd88a4eae5b0558d8ecb121c1ce32d2e
diff --git a/README.md b/README.md
index 84275f3..f7df7d7 100644
--- a/README.md
+++ b/README.md
@@ -75,9 +75,12 @@
 How to build the Gerrit Plugin API is described in the [Gerrit documentation](../../../Documentation/dev-bazel.html#_extension_and_plugin_api_jar_files).
 
 ### Configure Gerrit to use the SAML filter:
-In `$site_path/etc/gerrit.config` file, the `[httpd]` section should contain
+In `$site_path/etc/gerrit.config` file, the `[httpd]` and `[gerrit]` sections should
+contain:
 
 ```
+[gerrit]
+    installModule = com.googlesource.gerrit.plugins.saml.Module
 [httpd]
     filterClass = com.googlesource.gerrit.plugins.saml.SamlWebFilter
 ```
diff --git a/src/main/java/com/googlesource/gerrit/plugins/saml/Module.java b/src/main/java/com/googlesource/gerrit/plugins/saml/Module.java
new file mode 100644
index 0000000..4e43958
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/saml/Module.java
@@ -0,0 +1,25 @@
+// Copyright (C) 2024 The Android Open Source Project
+//
+// Licensed 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 com.googlesource.gerrit.plugins.saml;
+
+import com.google.inject.AbstractModule;
+import org.pac4j.saml.client.SAML2Client;
+
+public class Module extends AbstractModule {
+  @Override
+  protected void configure() {
+    bind(SAML2Client.class).toProvider(SamlClientProvider.class);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java b/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
index 044d8db..ecc1de5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
@@ -17,15 +17,12 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.Sets;
-import com.google.gerrit.common.Nullable;
 import com.google.gerrit.entities.Account;
 import com.google.gerrit.extensions.api.GerritApi;
 import com.google.gerrit.extensions.api.accounts.Accounts;
 import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.gerrit.extensions.restapi.Url;
 import com.google.gerrit.server.config.AuthConfig;
-import com.google.gerrit.server.config.CanonicalWebUrl;
-import com.google.gerrit.server.config.SitePaths;
 import com.google.gerrit.server.util.ManualRequestContext;
 import com.google.gerrit.server.util.OneOffRequestContext;
 import com.google.inject.Inject;
@@ -80,19 +77,18 @@
 
   @Inject
   SamlWebFilter(
-      @CanonicalWebUrl @Nullable String canonicalUrl,
-      SitePaths sitePaths,
       AuthConfig auth,
       SamlConfig samlConfig,
       SamlMembership samlMembership,
       GerritApi gApi,
       Accounts accounts,
+      SAML2Client saml2Client,
       OneOffRequestContext oneOffRequestContext) {
     this.auth = auth;
     this.samlConfig = samlConfig;
     this.samlMembership = samlMembership;
     log.debug("Max Authentication Lifetime: " + samlConfig.getMaxAuthLifetimeAttr());
-    this.saml2Client = new SamlClientProvider(canonicalUrl, sitePaths, samlConfig).get();
+    this.saml2Client = saml2Client;
 
     this.authHeaders =
         Sets.newHashSet(