// Copyright (C) 2013 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.google.gerrit.server.account;

import static com.google.gerrit.reviewdb.client.AccountExternalId.SCHEME_USERNAME;

import com.google.common.base.Strings;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.AccountExternalId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.PutHttpPassword.Input;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;

import org.apache.commons.codec.binary.Base64;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Collections;

public class PutHttpPassword implements RestModifyView<AccountResource, Input> {
  public static class Input {
    public String httpPassword;
    public boolean generate;
  }

  private static final int LEN = 12;
  private static final SecureRandom rng;

  static {
    try {
      rng = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException("Cannot create RNG for password generator", e);
    }
  }

  private final Provider<CurrentUser> self;
  private final Provider<ReviewDb> dbProvider;
  private final AccountCache accountCache;

  @Inject
  PutHttpPassword(Provider<CurrentUser> self, Provider<ReviewDb> dbProvider,
      AccountCache accountCache) {
    this.self = self;
    this.dbProvider = dbProvider;
    this.accountCache = accountCache;
  }

  @Override
  public Response<String> apply(AccountResource rsrc, Input input) throws AuthException,
      ResourceNotFoundException, ResourceConflictException, OrmException {
    if (input == null) {
      input = new Input();
    }
    if (self.get() != rsrc.getUser()
        && !self.get().getCapabilities().canAdministrateServer()) {
      if (input.generate && !self.get().getCapabilities().canGenerateHttpPassword()) {
        throw new AuthException("not allowed to generate HTTP password");
      } else {
        throw new AuthException("not allowed to set HTTP password");
      }
    }
    if (rsrc.getUser().getUserName() == null) {
      throw new ResourceConflictException("username must be set");
    }
    AccountExternalId.Key key =
        new AccountExternalId.Key(SCHEME_USERNAME, rsrc.getUser().getUserName());
    AccountExternalId id = dbProvider.get().accountExternalIds().get(key);
    if (id == null) {
      throw new ResourceNotFoundException();
    }

    String newPassword;
    if (input.generate) {
      newPassword = generate();
    } else {
      if (!Strings.isNullOrEmpty(input.httpPassword)
          && !self.get().getCapabilities().canAdministrateServer()) {
        throw new AuthException("not allowed to set HTTP password directly, "
            + "need to be Gerrit administrator");
      }
      newPassword = Strings.emptyToNull(input.httpPassword);
    }

    id.setPassword(newPassword);
    dbProvider.get().accountExternalIds().update(Collections.singleton(id));
    accountCache.evict(rsrc.getUser().getAccountId());

    return Strings.isNullOrEmpty(newPassword)
        ? Response.<String>none()
        : Response.ok(newPassword);
  }

  private String generate() {
    byte[] rand = new byte[LEN];
    rng.nextBytes(rand);

    byte[] enc = Base64.encodeBase64(rand, false);
    StringBuilder r = new StringBuilder(LEN);
    for (int i = 0; i < LEN; i++) {
      if (enc[i] == '=') {
        break;
      }
      r.append((char) enc[i]);
    }
    return r.toString();
  }
}

@@ -2639,52 +2639,130 @@
    i
-f (self.get() != rsrc.getUser()
+nput.httpPassword = Strings.emptyToNull(input.httpPassword);%0A%0A    String newPassword;%0A    if (input.generate) %7B
 %0A      
-  && !
+if (
 self
@@ -2771,78 +2771,36 @@
 et()
-.getCapabilities().canAdministrateServ
+ != rsrc.getUs
 er()
-) %7B
 %0A      
-if (input.generate
+   
  && 
@@ -2943,431 +2943,198 @@
    %7D
- else %7B%0A        throw new AuthException(%22not allowed to set HTTP password%22);%0A      %7D%0A    %7D%0A    if (rsrc.getUser().getUserName() == null) %7B%0A      throw new ResourceConflictException(%22username must be set%22);
+%0A      newPassword = generate();%0A%0A    %7D else if (input.httpPassword == null) %7B%0A      if (self.get() != rsrc.getUser()
 %0A    
-%7D%0A
     
-AccountExternalId.Key key =%0A        new AccountExternalId.Key(SCHEME_USERNAME, rsrc.getUser().getUserName());%0A    AccountExternalId id = dbProvider.get().accountExternalIds().get(key);%0A    if (id == null
+  && !self.get().getCapabilities().canAdministrateServer()
 ) %7B%0A
+  
     
@@ -3149,93 +3149,68 @@
 new 
-ResourceNotFoundException();%0A    %7D%0A%0A    String newP
+AuthException(%22not allowed to clear HTTP p
 assword
+%22)
 ;%0A    
-if (input.generate) %7B
+  %7D
 %0A   
@@ -3226,26 +3226,20 @@
 sword = 
-generate()
+null
 ;%0A    %7D 
@@ -3259,64 +3259,8 @@
 if (
-!Strings.isNullOrEmpty(input.httpPassword)%0A          && 
 !sel
@@ -3472,46 +3472,399 @@
 d = 
-Strings.emptyToNull(input.httpPassword
+input.httpPassword;%0A    %7D%0A%0A    if (rsrc.getUser().getUserName() == null) %7B%0A      throw new ResourceConflictException(%22username must be set%22);%0A    %7D%0A%0A    AccountExternalId id = dbProvider.get().accountExternalIds()%0A        .get(new AccountExternalId.Key(%0A            SCHEME_USERNAME,%0A            rsrc.getUser().getUserName()));%0A    if (id == null) %7B%0A      throw new ResourceNotFoundException(
 );%0A 
@@ -3864,25 +3864,24 @@
 on();%0A    %7D%0A
-%0A
     id.setPa
@@ -4165,16 +4165,23 @@
 private 
+static 
 String g

Fix PutHttpPassword to allow all intended use cases