AuthChecker.java 2.5 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 35 36 37 38 39
        return isSupported;
    }

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

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