AuthExtendSource.java 2.3 KB
Newer Older
1 2
package me.zhyd.oauth.config;

3 4
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.exception.AuthException;
5
import me.zhyd.oauth.request.AuthDefaultRequest;
6 7 8
import me.zhyd.oauth.request.AuthExtendRequest;
import me.zhyd.oauth.request.AuthRequest;

9 10 11 12 13 14 15 16 17
/**
 * 测试自定义实现{@link AuthSource}接口后的枚举类
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.12.0
 */
public enum AuthExtendSource implements AuthSource {

18
    OTHER (AuthExtendRequest.class){
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        /**
         * 授权的api
         *
         * @return url
         */
        @Override
        public String authorize() {
            return "http://authorize";
        }

        /**
         * 获取accessToken的api
         *
         * @return url
         */
        @Override
        public String accessToken() {
            return "http://accessToken";
        }

        /**
         * 获取用户信息的api
         *
         * @return url
         */
        @Override
        public String userInfo() {
            return null;
        }

        /**
         * 取消授权的api
         *
         * @return url
         */
        @Override
        public String revoke() {
            return null;
        }

        /**
         * 刷新授权的api
         *
         * @return url
         */
        @Override
        public String refresh() {
            return null;
        }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    };

    private Class<? extends AuthDefaultRequest> targetClass;

    AuthExtendSource(Class<? extends AuthDefaultRequest> targetClass) {
        this.targetClass = targetClass;
    }

    public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
        return this.getAuthRequestInstance(authConfig,null);
    }

    public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
        try {
            if(authStateCache==null){
                return this.targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(authConfig);
            }else{
                return this.targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(authConfig, authStateCache);
            }
        } catch (Exception e) {
            throw new AuthException("未获取到有效的Auth配置");
        }
90
    }
91

92
}