package cn.com.qmth.stmms.common.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class AccessControlUtils {

    public static final int DEFAULT_RANDOM_STRING_LENGTH = 32;

    public static final int DEFAULT_EXPIRE_SECONDS = 3600 * 10;

    public static String randomString() {
        return randomString(DEFAULT_RANDOM_STRING_LENGTH);
    }

    public static String randomString(int length) {
        return RandomStringUtils.random(length, true, true);
    }

    public static String uuidString() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean expired(Date refreshTime, int expireSeconds) {
        Calendar c = Calendar.getInstance();
        c.setTime(refreshTime);
        c.add(Calendar.SECOND, expireSeconds);
        return new Date().after(c.getTime());
    }

    public static boolean expired(Date refreshTime) {
        return expired(refreshTime, DEFAULT_EXPIRE_SECONDS);
    }
}
