package com.qmth.themis.common.token;

import com.qmth.themis.common.enums.Platform;
import com.qmth.themis.common.util.MD5Util;
import org.apache.commons.lang3.StringUtils;

import java.security.NoSuchAlgorithmException;

/**
 * 通用访问令牌工具
 *
 * @author luoshi
 */
public class AccessToken {

    private static final String JOINER = "&";

    /**
     * Token构造方法
     *
     * @param uid      - 用户唯一标识
     * @param platform - 访问来源终端
     * @param deviceId - 访问设备编号
     * @return
     */
    public static String build(String uid, Platform platform, String deviceId) throws NoSuchAlgorithmException {
        if (uid == null || platform == null || deviceId == null) {
            throw new IllegalArgumentException("access token param invalid");
        }
        return MD5Util.encoder(StringUtils.joinWith(JOINER, uid, platform.toString(), deviceId,
                String.valueOf(System.currentTimeMillis())));
    }
}
