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

import me.zhyd.oauth.config.AuthConfig;
4
import me.zhyd.oauth.config.AuthSource;
智布道's avatar
智布道 已提交
5
import me.zhyd.oauth.enums.AuthResponseStatus;
6
import me.zhyd.oauth.exception.AuthException;
智布道's avatar
智布道 已提交
7 8

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

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

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

    /**
     * 校验回调传回的code
     *
     * @param code 回调时传回的code
智布道's avatar
智布道 已提交
64
     * @since 1.8.0
65 66 67
     */
    public static void checkCode(String code) {
        if (StringUtils.isEmpty(code)) {
智布道's avatar
智布道 已提交
68
            throw new AuthException(AuthResponseStatus.ILLEGAL_CODE);
69 70
        }
    }
智布道's avatar
智布道 已提交
71
}