AuthChecker.java 3.0 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
import me.zhyd.oauth.model.AuthCallback;
智布道's avatar
智布道 已提交
8 9

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

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

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

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