AuthChecker.java 4.2 KB
Newer Older
智布道's avatar
智布道 已提交
1 2
package me.zhyd.oauth.utils;

智布道's avatar
智布道 已提交
3
import me.zhyd.oauth.cache.AuthStateCache;
智布道's avatar
智布道 已提交
4
import me.zhyd.oauth.config.AuthConfig;
5
import me.zhyd.oauth.config.AuthDefaultSource;
6
import me.zhyd.oauth.config.AuthSource;
智布道's avatar
智布道 已提交
7
import me.zhyd.oauth.enums.AuthResponseStatus;
8
import me.zhyd.oauth.exception.AuthException;
智布道's avatar
智布道 已提交
9
import me.zhyd.oauth.model.AuthCallback;
智布道's avatar
智布道 已提交
10 11

/**
智布道's avatar
智布道 已提交
12 13
 * 授权配置类的校验器
 *
智布道's avatar
智布道 已提交
14
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
智布道's avatar
智布道 已提交
15
 * @since 1.6.1-beta
智布道's avatar
智布道 已提交
16
 */
17
public class AuthChecker {
智布道's avatar
智布道 已提交
18 19

    /**
智布道's avatar
智布道 已提交
20
     * 是否支持第三方登录
智布道's avatar
智布道 已提交
21
     *
22
     * @param config config
23
     * @param source source
智布道's avatar
智布道 已提交
24
     * @return true or false
智布道's avatar
智布道 已提交
25
     * @since 1.6.1-beta
智布道's avatar
智布道 已提交
26
     */
27
    public static boolean isSupportedAuth(AuthConfig config, AuthSource source) {
不合群的混子's avatar
不合群的混子 已提交
28
        boolean isSupported = StringUtils.isNotEmpty(config.getClientId()) && StringUtils.isNotEmpty(config.getClientSecret()) && StringUtils.isNotEmpty(config.getRedirectUri());
29
        if (isSupported && AuthDefaultSource.ALIPAY == source) {
30 31
            isSupported = StringUtils.isNotEmpty(config.getAlipayPublicKey());
        }
32
        if (isSupported && AuthDefaultSource.STACK_OVERFLOW == source) {
33 34
            isSupported = StringUtils.isNotEmpty(config.getStackOverflowKey());
        }
智布道's avatar
智布道 已提交
35
        if (isSupported && AuthDefaultSource.WECHAT_ENTERPRISE == source) {
36 37
            isSupported = StringUtils.isNotEmpty(config.getAgentId());
        }
38 39 40 41 42 43 44 45
        return isSupported;
    }

    /**
     * 检查配置合法性。针对部分平台, 对redirect uri有特定要求。一般来说redirect uri都是http://,而对于facebook平台, redirect uri 必须是https的链接
     *
     * @param config config
     * @param source source
智布道's avatar
智布道 已提交
46
     * @since 1.6.1-beta
47
     */
48
    public static void checkConfig(AuthConfig config, AuthSource source) {
49
        String redirectUri = config.getRedirectUri();
50
        if (!GlobalAuthUtils.isHttpProtocol(redirectUri) && !GlobalAuthUtils.isHttpsProtocol(redirectUri)) {
智布道's avatar
智布道 已提交
51
            throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
52
        }
53
        // facebook的回调地址必须为https的链接
54
        if (AuthDefaultSource.FACEBOOK == source && !GlobalAuthUtils.isHttpsProtocol(redirectUri)) {
智布道's avatar
智布道 已提交
55 56
            // Facebook's redirect uri must use the HTTPS protocol
            throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
57
        }
58
        // 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
59
        if (AuthDefaultSource.ALIPAY == source && GlobalAuthUtils.isLocalHost(redirectUri)) {
智布道's avatar
智布道 已提交
60 61
            // The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
            throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
62
        }
智布道's avatar
智布道 已提交
63
    }
64 65 66

    /**
     * 校验回调传回的code
智布道's avatar
智布道 已提交
67
     * <p>
68
     * {@code v1.10.0}版本中改为传入{@code source}和{@code callback},对于不同平台使用不同参数接受code的情况统一做处理
69
     *
智布道's avatar
智布道 已提交
70 71
     * @param source   当前授权平台
     * @param callback 从第三方授权回调回来时传入的参数集合
智布道's avatar
智布道 已提交
72
     * @since 1.8.0
73
     */
智布道's avatar
智布道 已提交
74 75
    public static void checkCode(AuthSource source, AuthCallback callback) {
        String code = callback.getCode();
76
        if (source == AuthDefaultSource.ALIPAY) {
智布道's avatar
智布道 已提交
77
            code = callback.getAuth_code();
78
        } else if (source == AuthDefaultSource.HUAWEI) {
智布道's avatar
智布道 已提交
79 80
            code = callback.getAuthorization_code();
        }
81
        if (StringUtils.isEmpty(code)) {
智布道's avatar
智布道 已提交
82 83 84 85 86 87 88 89 90 91 92 93
            throw new AuthException(AuthResponseStatus.ILLEGAL_CODE, source);
        }
    }

    /**
     * 校验回调传回的{@code state},为空或者不存在
     * <p>
     * {@code state}不存在的情况只有两种:
     * 1. {@code state}已使用,被正常清除
     * 2. {@code state}为前端伪造,本身就不存在
     *
     * @param state          {@code state}一定不为空
94
     * @param source         {@code source}当前授权平台
智布道's avatar
智布道 已提交
95 96 97 98 99
     * @param authStateCache {@code authStateCache} state缓存实现
     */
    public static void checkState(String state, AuthSource source, AuthStateCache authStateCache) {
        if (StringUtils.isEmpty(state) || !authStateCache.containsKey(state)) {
            throw new AuthException(AuthResponseStatus.ILLEGAL_STATUS, source);
100 101
        }
    }
智布道's avatar
智布道 已提交
102
}