/*-
 * ~~~~~~licensing~~~~~~
 * htpasswd
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.auth.basic.htpasswd;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.entaxy.esb.system.auth.basic.htpasswd.entity.Htpasswd;
import ru.entaxy.esb.system.auth.basic.jpa.api.entity.BasicAuthAccount;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class HtpasswdGenerator {

    private static final Log LOG = LogFactory.getLog(HtpasswdGenerator.class);

    private String checkSumFileName;

    public Htpasswd htpasswd;

    public void generateHtpasswd(List<BasicAuthAccount> accounts, String salt) throws IOException, NoSuchAlgorithmException {
        htpasswd.setMasterSalt(salt);
        htpasswd.prepare(accounts);
        createFile();
    }

    private void createFile() throws IOException {
        String content = htpasswd.toString();
        LOG.trace("HTTPASSWD " + content);
        String storeFolder = htpasswd.getDirectory();
        File folder = new File(storeFolder);
        folder.mkdirs();

        File htpasswdFile = new File(folder.getAbsolutePath() + File.separator + htpasswd.getFileName());

        Path path = Paths.get(htpasswdFile.getAbsolutePath());
        Files.write(path, content.getBytes());

        String checkSum = calculateCheckSum(path);
        File checkSumFile = new File(folder.getAbsolutePath() + File.separator + checkSumFileName);
        path = Paths.get(checkSumFile.getAbsolutePath());
        Files.write(path, checkSum.getBytes());
    }

    private String calculateCheckSum(Path path) throws IOException {
        String md5;
        try (InputStream is = Files.newInputStream(path)) {
            md5 = DigestUtils.md5Hex(is);
        }
        return md5;
    }

    public Htpasswd getHtpasswd() {
        return htpasswd;
    }

    public void setHtpasswd(Htpasswd htpasswd) {
        this.htpasswd = htpasswd;
    }

    public String getCheckSumFileName() {
        return checkSumFileName;
    }

    public void setCheckSumFileName(String checkSumFileName) {
        this.checkSumFileName = checkSumFileName;
    }

}
