LCOV - code coverage report
Current view: top level - server/auth - UniversalAuthBackend.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 3 22 13.6 %
Date: 2022-11-19 15:00:39 Functions: 1 4 25.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.auth;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      20             : import com.google.inject.Inject;
      21             : import com.google.inject.Singleton;
      22             : import java.util.ArrayList;
      23             : import java.util.List;
      24             : 
      25             : /** Universal implementation of the AuthBackend that works with the injected set of AuthBackends. */
      26             : @Singleton
      27             : public final class UniversalAuthBackend implements AuthBackend {
      28             :   private final PluginSetContext<AuthBackend> authBackends;
      29             : 
      30             :   @Inject
      31         138 :   UniversalAuthBackend(PluginSetContext<AuthBackend> authBackends) {
      32         138 :     this.authBackends = authBackends;
      33         138 :   }
      34             : 
      35             :   @Override
      36             :   public AuthUser authenticate(AuthRequest request) throws AuthException {
      37           0 :     List<AuthUser> authUsers = new ArrayList<>();
      38           0 :     List<AuthException> authExs = new ArrayList<>();
      39           0 :     authBackends.runEach(
      40             :         backend -> {
      41             :           try {
      42           0 :             authUsers.add(requireNonNull(backend.authenticate(request)));
      43           0 :           } catch (MissingCredentialsException ex) {
      44             :             // Not handled by this backend.
      45           0 :           } catch (AuthException ex) {
      46           0 :             authExs.add(ex);
      47           0 :           }
      48           0 :         });
      49             : 
      50             :     // Handle the valid responses
      51           0 :     if (authUsers.size() == 1) {
      52           0 :       return authUsers.get(0);
      53           0 :     } else if (authUsers.isEmpty() && authExs.size() == 1) {
      54           0 :       throw authExs.get(0);
      55           0 :     } else if (authExs.isEmpty() && authUsers.isEmpty()) {
      56           0 :       throw new MissingCredentialsException();
      57             :     }
      58             : 
      59           0 :     String msg =
      60           0 :         String.format(
      61             :             "Multiple AuthBackends attempted to handle request: authUsers=%s authExs=%s",
      62             :             authUsers, authExs);
      63           0 :     throw new AuthException(msg);
      64             :   }
      65             : 
      66             :   @Override
      67             :   public String getDomain() {
      68           0 :     throw new UnsupportedOperationException("UniversalAuthBackend doesn't support domain.");
      69             :   }
      70             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750