package com.orange.clara.cloud.oauthregisterbroker.security;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Copyright (C) 2016 Orange
 * <p>
 * This software is distributed under the terms and conditions of the 'Apache-2.0'
 * license which can be found in the file 'LICENSE' in this package distribution
 * or at 'https://opensource.org/licenses/Apache-2.0'.
 * <p>
 * Author: Arthur Halet
 * Date: 15/05/2016
 */
@Component
public class BootSecurity {

    @Value("${encryption.key:MySuperSecretKey}")
    protected String encryptionKey;

    @Value("classpath:properties/encryption_key.txt")
    protected File encryptionKeyFile;

    public void fillEncryptionKeyFile() throws IOException, NoSuchAlgorithmException {
        //force to have always a 32 bytes key (to use AES encryption with 256 bits key length)
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(encryptionKey.getBytes(Charsets.UTF_8));
        byte[] key = md.digest();

        Files.write(key, encryptionKeyFile);
    }

    public void removeEncryptionRestriction() {
        try {
            Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
            field.setAccessible(true);
            field.set(null, java.lang.Boolean.FALSE);
        } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @PostConstruct
    public void runSequence() {
        this.removeEncryptionRestriction();
        try {
            this.fillEncryptionKeyFile();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -1373,465 +1373,8 @@
  %7D%0A%0A
-    public void removeEncryptionRestriction() %7B%0A        try %7B%0A            Field field = Class.forName(%22javax.crypto.JceSecurity%22).getDeclaredField(%22isRestricted%22);%0A            field.setAccessible(true);%0A            field.set(null, java.lang.Boolean.FALSE);%0A        %7D catch (ClassNotFoundException %7C NoSuchFieldException %7C SecurityException %7C IllegalArgumentException %7C IllegalAccessException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A
     
@@ -1424,52 +1424,8 @@
 ) %7B%0A
-        this.removeEncryptionRestriction();%0A
     

remove aop to delete encryption restrictions