LCOV - code coverage report
Current view: top level - server/auth - InternalAuthBackend.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 5 17 29.4 %
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 com.google.gerrit.server.account.AccountCache;
      18             : import com.google.gerrit.server.account.AccountState;
      19             : import com.google.gerrit.server.account.externalids.PasswordVerifier;
      20             : import com.google.gerrit.server.config.AuthConfig;
      21             : import com.google.inject.Inject;
      22             : import com.google.inject.Singleton;
      23             : import java.util.Locale;
      24             : 
      25             : @Singleton
      26             : public class InternalAuthBackend implements AuthBackend {
      27             :   private final AccountCache accountCache;
      28             :   private final AuthConfig authConfig;
      29             :   private final PasswordVerifier passwordVerifier;
      30             : 
      31             :   @Inject
      32             :   InternalAuthBackend(
      33         138 :       AccountCache accountCache, AuthConfig authConfig, PasswordVerifier passwordVerifier) {
      34         138 :     this.accountCache = accountCache;
      35         138 :     this.authConfig = authConfig;
      36         138 :     this.passwordVerifier = passwordVerifier;
      37         138 :   }
      38             : 
      39             :   @Override
      40             :   public String getDomain() {
      41           0 :     return "gerrit";
      42             :   }
      43             : 
      44             :   // TODO(gerritcodereview-team): This function has no coverage.
      45             :   @Override
      46             :   public AuthUser authenticate(AuthRequest req)
      47             :       throws MissingCredentialsException, InvalidCredentialsException, UnknownUserException,
      48             :           UserNotAllowedException, AuthException {
      49           0 :     if (!req.getUsername().isPresent() || !req.getPassword().isPresent()) {
      50           0 :       throw new MissingCredentialsException();
      51             :     }
      52             : 
      53             :     String username;
      54           0 :     if (authConfig.isUserNameToLowerCase()) {
      55           0 :       username = req.getUsername().map(u -> u.toLowerCase(Locale.US)).get();
      56             :     } else {
      57           0 :       username = req.getUsername().get();
      58             :     }
      59             : 
      60           0 :     AccountState who = accountCache.getByUsername(username).orElseThrow(UnknownUserException::new);
      61             : 
      62           0 :     if (!who.account().isActive()) {
      63           0 :       throw new UserNotAllowedException(
      64             :           "Authentication failed for "
      65             :               + username
      66             :               + ": account inactive or not provisioned in Gerrit");
      67             :     }
      68             : 
      69           0 :     if (!passwordVerifier.checkPassword(who.externalIds(), username, req.getPassword().get())) {
      70           0 :       throw new InvalidCredentialsException();
      71             :     }
      72           0 :     return new AuthUser(AuthUser.UUID.create(username), username);
      73             :   }
      74             : }

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