/*
 * Copyright 2019 ThoughtWorks, Inc.
 *
 * 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.thoughtworks.go.config.materials;

import com.thoughtworks.go.security.CryptoException;
import com.thoughtworks.go.security.GoCipher;
import org.springframework.stereotype.Component;

import static com.thoughtworks.go.config.materials.ScmMaterialConfig.ENCRYPTED_PASSWORD;
import static com.thoughtworks.go.config.materials.ScmMaterialConfig.PASSWORD;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Component
public class PasswordDeserializer {
    public GoCipher goCipher;

    public PasswordDeserializer() {
        this.goCipher = new GoCipher();
    }

    public String deserialize(String password, String encryptedPassword, AbstractMaterialConfig materialConfig) {
        if (isNotBlank(password) && isNotBlank(encryptedPassword)) {
            materialConfig.addError(PASSWORD, "You may only specify `password` or `encrypted_password`, not both!");
            materialConfig.addError(ScmMaterialConfig.ENCRYPTED_PASSWORD, "You may only specify `password` or `encrypted_password`, not both!");
        }

        if (isNotBlank(password)) {
            try {
                return goCipher.encrypt(password);
            } catch (CryptoException e) {
                materialConfig.addError(PASSWORD, "Could not encrypt the password. This usually happens when the cipher text is invalid");
            }
        } else if (isNotBlank(encryptedPassword)) {
            try {
                goCipher.decrypt(encryptedPassword);
            } catch (Exception e) {
                materialConfig.addError(ENCRYPTED_PASSWORD, "Encrypted value for password is invalid. This usually happens when the cipher text is invalid.");
            }

            return goCipher.maybeReEncryptForPostConstructWithoutExceptions(encryptedPassword);
        }
        return null;
    }
}

@@ -642,16 +642,63 @@
 rials;%0A%0A
+import com.thoughtworks.go.config.Validatable;%0A
 import c
@@ -1308,40 +1308,21 @@
 rd, 
-AbstractMaterialConfig materialC
+Validatable c
 onfi
@@ -1403,33 +1403,25 @@
             
-materialC
+c
 onfig.addErr
@@ -1512,33 +1512,25 @@
             
-materialC
+c
 onfig.addErr
@@ -1811,33 +1811,25 @@
             
-materialC
+c
 onfig.addErr
@@ -2123,17 +2123,9 @@
     
-materialC
+c
 onfi

Change interface to allow using this component for all config models.