package io.cattle.platform.register.util;

import io.cattle.platform.archaius.util.ArchaiusUtil;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

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

import com.netflix.config.DynamicLongProperty;

public class RegistrationToken {

    public static final String HMAC_SHA1_ALGORITHM  = "HmacSHA1";
    private static final DynamicLongProperty TOKEN_PERIOD = ArchaiusUtil.getLong("registration.token.period.millis");

    public static long getAllowedTime() {
        return TOKEN_PERIOD.get();
    }

    public static final String createToken(String accessKey, String secretKey) {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return createToken(accessKey, secretKey, cal.getTime());
    }

    public static final String createToken(String accessKey, String secretKey, Date date) {
        String prefix = String.format("%s:%d", accessKey, date.getTime());

        try {
            SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), HMAC_SHA1_ALGORITHM);
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(signingKey);

            String signature = Hex.encodeHexString(mac.doFinal(prefix.getBytes("UTF-8")));

            return String.format("%s:%s", prefix, signature);
        } catch (InvalidKeyException e) {
            throw new IllegalStateException("Failed to generate signature key for [" + prefix + "]", e);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("Failed to generate signature key for [" + prefix + "]", e);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Failed to generate signature key for [" + prefix + "]", e);
        }
    }
}

@@ -408,19 +408,22 @@
 .binary.
-Hex
+Base64
 ;%0A%0Aimpor
@@ -1532,21 +1532,27 @@
 e = 
-Hex
+Base64
 .encode
-Hex
+Base64
 Stri
@@ -1592,16 +1592,40 @@
 TF-8%22)))
+.replaceAll(%22%5B/=+%5D%22, %22%22)
 ;%0A%0A     

base64 token, not hex. token not as long