GlobalAuthUtil.java 2.9 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8
package me.zhyd.oauth.utils;

import cn.hutool.core.codec.Base64;
import me.zhyd.oauth.exception.AuthException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
智布道's avatar
智布道 已提交
9
import java.net.URLDecoder;
智布道's avatar
智布道 已提交
10 11 12 13
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
智布道's avatar
智布道 已提交
14 15
import java.util.HashMap;
import java.util.Map;
智布道's avatar
智布道 已提交
16

智布道's avatar
智布道 已提交
17
public class GlobalAuthUtil {
智布道's avatar
智布道 已提交
18 19 20
    private static final String DEFAULT_ENCODING = "UTF-8";
    private static final String ALGORITHM = "HmacSHA256";

智布道's avatar
智布道 已提交
21
    public static String generateDingTalkSignature(String canonicalString, String secret) {
智布道's avatar
智布道 已提交
22 23
        try {
            byte[] signData = sign(canonicalString.getBytes(DEFAULT_ENCODING), secret.getBytes(DEFAULT_ENCODING));
智布道's avatar
智布道 已提交
24
            return urlEncode(new String(Base64.encode(signData, false)));
智布道's avatar
智布道 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        } catch (UnsupportedEncodingException ex) {
            throw new AuthException("Unsupported algorithm: " + DEFAULT_ENCODING, ex);
        }
    }

    private static byte[] sign(byte[] key, byte[] data) {
        try {
            Mac mac = Mac.getInstance(ALGORITHM);
            mac.init(new SecretKeySpec(key, ALGORITHM));
            return mac.doFinal(data);
        } catch (NoSuchAlgorithmException ex) {
            throw new AuthException("Unsupported algorithm: " + ALGORITHM, ex);
        } catch (InvalidKeyException ex) {
            throw new AuthException("Invalid key: " + Arrays.toString(key), ex);
        }
    }

智布道's avatar
智布道 已提交
42
    private static String urlEncode(String value) {
智布道's avatar
智布道 已提交
43 44 45 46 47
        if (value == null) {
            return "";
        }

        try {
智布道's avatar
智布道 已提交
48
            String encoded = URLEncoder.encode(value, GlobalAuthUtil.DEFAULT_ENCODING);
智布道's avatar
智布道 已提交
49 50 51
            return encoded.replace("+", "%20").replace("*", "%2A")
                    .replace("~", "%7E").replace("/", "%2F");
        } catch (UnsupportedEncodingException e) {
智布道's avatar
智布道 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            throw new AuthException("Failed To Encode Uri", e);
        }
    }

    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        }
        try {
            return URLDecoder.decode(value, GlobalAuthUtil.DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new AuthException("Failed To Decode Uri", e);
        }
    }

    public static Map<String, String> parseStringToMap(String accessTokenStr) {
        Map<String, String> res = new HashMap<>();
        if (accessTokenStr.contains("&")) {
            String[] fields = accessTokenStr.split("&");
            for (String field : fields) {
                if (field.contains("=")) {
                    String[] keyValue = field.split("=");
                    res.put(GlobalAuthUtil.urlDecode(keyValue[0]), keyValue.length == 2 ? GlobalAuthUtil.urlDecode(keyValue[1]) : null);
                }
            }
智布道's avatar
智布道 已提交
77
        }
智布道's avatar
智布道 已提交
78
        return res;
智布道's avatar
智布道 已提交
79 80
    }
}