me.zhyd.oauth.config.AuthConfig Java Examples

The following examples show how to use me.zhyd.oauth.config.AuthConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GlobalAuthUtilsTest.java    From JustAuth with MIT License 6 votes vote down vote up
@Test
public void testGenerateTwitterSignatureForRequestToken() {
    AuthConfig config = AuthConfig.builder()
        .clientId("HD0XLqzi5Wz0G08rh45Cg8mgh")
        .clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
        .redirectUri("https://codinglife.tech")
        .build();
    Map<String, String> params = new HashMap<>();
    params.put("oauth_consumer_key", config.getClientId());
    params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
    params.put("oauth_signature_method", "HMAC-SHA1");
    params.put("oauth_timestamp", "1569750981");
    params.put("oauth_callback", config.getRedirectUri());
    params.put("oauth_version", "1.0");

    String baseUrl = "https://api.twitter.com/oauth/request_token";
    params.put("oauth_signature", GlobalAuthUtils.generateTwitterSignature(params, "POST", baseUrl, config.getClientSecret(), null));

    params.forEach((k, v) -> params.put(k, "\"" + GlobalAuthUtils.urlEncode(v) + "\""));
    String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");

    assertEquals("OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"%2BL5Jq%2FTaKubge04cWw%2B4yfjFlaU%3D\", oauth_callback=\"https%3A%2F%2Fcodinglife.tech\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569750981\", oauth_version=\"1.0\"", actual);
}
 
Example #2
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 6 votes vote down vote up
@Test
public void login() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    String state = AuthStateUtils.createState();
    request.authorize(state);
    AuthCallback callback = AuthCallback.builder()
        .code("code")
        .state(state)
        .build();
    AuthResponse response = request.login(callback);
    Assert.assertNotNull(response);

    AuthUser user = (AuthUser) response.getData();
    Assert.assertNotNull(user);
    System.out.println(JSON.toJSONString(user));
}
 
Example #3
Source File: AuthChecker.java    From JustAuth with MIT License 6 votes vote down vote up
/**
 * 检查配置合法性。针对部分平台, 对redirect uri有特定要求。一般来说redirect uri都是http://,而对于facebook平台, redirect uri 必须是https的链接
 *
 * @param config config
 * @param source source
 * @since 1.6.1-beta
 */
public static void checkConfig(AuthConfig config, AuthSource source) {
    String redirectUri = config.getRedirectUri();
    if (!GlobalAuthUtils.isHttpProtocol(redirectUri) && !GlobalAuthUtils.isHttpsProtocol(redirectUri)) {
        throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
    }
    // facebook的回调地址必须为https的链接
    if (AuthDefaultSource.FACEBOOK == source && !GlobalAuthUtils.isHttpsProtocol(redirectUri)) {
        // Facebook's redirect uri must use the HTTPS protocol
        throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
    }
    // 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
    if (AuthDefaultSource.ALIPAY == source && GlobalAuthUtils.isLocalHost(redirectUri)) {
        // The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
        throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
    }
}
 
Example #4
Source File: AuthChecker.java    From JustAuth with MIT License 6 votes vote down vote up
/**
 * 是否支持第三方登录
 *
 * @param config config
 * @param source source
 * @return true or false
 * @since 1.6.1-beta
 */
public static boolean isSupportedAuth(AuthConfig config, AuthSource source) {
    boolean isSupported = StringUtils.isNotEmpty(config.getClientId()) && StringUtils.isNotEmpty(config.getClientSecret()) && StringUtils.isNotEmpty(config.getRedirectUri());
    if (isSupported && AuthDefaultSource.ALIPAY == source) {
        isSupported = StringUtils.isNotEmpty(config.getAlipayPublicKey());
    }
    if (isSupported && AuthDefaultSource.STACK_OVERFLOW == source) {
        isSupported = StringUtils.isNotEmpty(config.getStackOverflowKey());
    }
    if (isSupported && AuthDefaultSource.WECHAT_ENTERPRISE == source) {
        isSupported = StringUtils.isNotEmpty(config.getAgentId());
    }
    if (isSupported && AuthDefaultSource.CODING == source) {
        isSupported = StringUtils.isNotEmpty(config.getCodingGroupName());
    }
    return isSupported;
}
 
Example #5
Source File: GlobalAuthUtilsTest.java    From JustAuth with MIT License 5 votes vote down vote up
@Test
public void testGenerateTwitterSignatureForAccessToken() {
    AuthConfig config = AuthConfig.builder()
        .clientId("HD0XLqzi5Wz0G08rh45Cg8mgh")
        .clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
        .build();
    AuthCallback authCallback = AuthCallback.builder()
        .oauth_token("W_KLmAAAAAAAxq5LAAABbXxJeD0")
        .oauth_verifier("lYou4gxfA6S5KioUa8VF8HCShzA2nSxp")
        .build();
    Map<String, String> params = new HashMap<>();
    params.put("oauth_consumer_key", config.getClientId());
    params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
    params.put("oauth_signature_method", "HMAC-SHA1");
    params.put("oauth_timestamp", "1569751082");
    params.put("oauth_token", authCallback.getOauth_token());
    params.put("oauth_verifier", authCallback.getOauth_verifier());
    params.put("oauth_version", "1.0");

    params.put("oauth_signature", GlobalAuthUtils.generateTwitterSignature(params, "POST", TWITTER.accessToken(), config.getClientSecret(), authCallback
        .getOauth_token()));

    params.forEach((k, v) -> params.put(k, "\"" + GlobalAuthUtils.urlEncode(v) + "\""));
    String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");

    assertEquals("OAuth oauth_verifier=\"lYou4gxfA6S5KioUa8VF8HCShzA2nSxp\", oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"9i0lmWgvphtkl2KcCO9VyZ3K2%2F0%3D\", oauth_token=\"W_KLmAAAAAAAxq5LAAABbXxJeD0\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\"", actual);
}
 
Example #6
Source File: MicrosoftRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getMicrosoft();
    return new AuthMicrosoftRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #7
Source File: TeambitionRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getTeambition();
    return new AuthTeambitionRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #8
Source File: TaobaoRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getTaobao();
    return new AuthTaobaoRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #9
Source File: AuthRequestFactory.java    From justauth-spring-boot-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取自定义的 request
 *
 * @param clazz  枚举类 {@link AuthSource}
 * @param source {@link AuthSource}
 * @return {@link AuthRequest}
 */
@SuppressWarnings("unchecked")
private AuthRequest getExtendRequest(Class clazz, String source) {
    try {
        EnumUtil.fromString(clazz, source.toUpperCase());
    } catch (IllegalArgumentException e) {
        // 无自定义匹配
        return null;
    }

    Map<String, ExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();

    // key 转大写
    Map<String, ExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6);
    extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v));

    ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase());
    if (extendRequestConfig != null) {
        Class<? extends AuthRequest> requestClass = extendRequestConfig.getRequestClass();

        if (requestClass != null) {
            // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法
            return ReflectUtil.newInstance(requestClass, (AuthConfig) extendRequestConfig, authStateCache);
        }
    }

    return null;
}
 
Example #10
Source File: DouyinRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getDouyin();
    return new AuthDouyinRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #11
Source File: StackoverflowRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getStackoverflow();
    return new AuthStackOverflowRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .stackOverflowKey(authConfig.getStackOverflowKey())
            .build());
}
 
Example #12
Source File: AuthDefaultRequest.java    From JustAuth with MIT License 5 votes vote down vote up
public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) {
    this.config = config;
    this.source = source;
    this.authStateCache = authStateCache;
    if (!AuthChecker.isSupportedAuth(config, source)) {
        throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE, source);
    }
    // 校验配置合法性
    AuthChecker.checkConfig(config, source);
}
 
Example #13
Source File: PinterestRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getPinterest();
    return new AuthPinterestRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #14
Source File: WechatEnterpriseRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getWechatEnterprise();
    return new AuthWeChatEnterpriseRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .agentId(authConfig.getAgentId())
            .build());
}
 
Example #15
Source File: ToutiaoRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getToutiao();
    return new AuthToutiaoRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #16
Source File: CustomTags.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取所有可用的Oauth平台
 *
 * @param params
 * @return
 */
public Object listAvailableOAuthPlatforms(Map params) {
    List<String> list = new ArrayList<>();
    try {
        for (Field f : authProperties.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            String fieldName = f.getName();
            AuthSource source = null;
            if ("tencentCloud".equals(fieldName)) {
                source = AuthSource.TENCENT_CLOUD;
            } else if ("stackoverflow".equals(fieldName)) {
                source = AuthSource.STACK_OVERFLOW;
            } else if ("wechatEnterprise".equals(fieldName)) {
                source = AuthSource.WECHAT_ENTERPRISE;
            } else {
                source = AuthSource.valueOf(fieldName.toUpperCase());
            }
            AuthConfig authConfig = (AuthConfig) f.get(authProperties);
            if (null != authConfig) {
                if (AuthChecker.isSupportedAuth(authConfig, source)) {
                    list.add(fieldName);
                }
            }

        }
    } catch (Exception e) {
        log.error("获取所有可用的Oauth平台发生异常", e);
    }

    return list;
}
 
Example #17
Source File: SocialPlugin.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 获取 JustAuth Request
 *
 * @param source 来源平台
 * @return {@link AuthRequest}
 */
public AuthRequest getRequest(String source) {
    AuthConfig config = this.getConfig(source);
    AuthRequest authRequest = null;
    switch (source) {
        case "github":
            authRequest = new AuthGithubRequest(config);
            break;
        case "gitee":
            authRequest = new AuthGiteeRequest(config);
            break;
        case "weibo":
            authRequest = new AuthWeiboRequest(config);
            break;
        case "oschina":
            authRequest = new AuthOschinaRequest(config);
            break;
        case "wechat":
            authRequest = new AuthWeChatOpenRequest(config);
            break;
        default:
            break;
    }
    if (null == authRequest) {
        throw new AuthException(source + "登录还没有相关配置,联系站长吧!");
    }
    return authRequest;
}
 
Example #18
Source File: MiRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getMi();
    return new AuthMiRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #19
Source File: FacebookRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getFacebook();
    return new AuthFacebookRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #20
Source File: WechatRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getWechat();
    return new AuthWeChatRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #21
Source File: HuaweiRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getHuawei();
    return new AuthHuaweiRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #22
Source File: AlipayRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getAlipay();
    return new AuthAlipayRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .alipayPublicKey(authConfig.getAlipayPublicKey())
            .build());
}
 
Example #23
Source File: CodingRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getCoding();
    return new AuthCodingRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #24
Source File: WeiboRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getWeibo();
    return new AuthWeiboRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #25
Source File: SocialSignOnProviderService.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
public AuthRequest  getAuthRequest(String provider) {
	AuthRequest authRequest = null;
	AuthConfig authConfig = AuthConfig.builder()
			.clientId(this.get(provider).getClientId())
			.clientSecret(this.get(provider).getClientSecret())
			.redirectUri(WebContext.getHttpContextPath()+ "/logon/oauth20/callback/"+provider)
			.build();
	
	if(provider.equalsIgnoreCase("WeChatOpen")) {
		authRequest = new AuthWeChatOpenRequest(authConfig);
	}else if(provider.equalsIgnoreCase("sinaweibo")) {
		authRequest = new AuthWeiboRequest(authConfig);
	}else if(provider.equalsIgnoreCase("qq")) {
		authRequest = new AuthQqRequest(authConfig);
	}else if(provider.equalsIgnoreCase("Alipay")) {
		authRequest = new AuthAlipayRequest(authConfig);
	}else if(provider.equalsIgnoreCase("Twitter")) {
		authRequest = new AuthTwitterRequest(authConfig);
	}else if(provider.equalsIgnoreCase("google")) {
		authRequest = new AuthGoogleRequest(authConfig);
	}else if(provider.equalsIgnoreCase("microsoft")) {
		authRequest = new AuthMicrosoftRequest(authConfig);
	}else if(provider.equalsIgnoreCase("Linkedin")) {
		authRequest = new AuthLinkedinRequest(authConfig);
	}else if(provider.equalsIgnoreCase("DingTalk")) {
		authRequest = new AuthDingTalkRequest(authConfig);
	}
	
	
	
	return authRequest;
}
 
Example #26
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 5 votes vote down vote up
@Test
public void revoke() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    AuthResponse response = request.revoke(AuthToken.builder().build());
    Assert.assertNotNull(response);
    System.out.println(JSON.toJSONString(response));
}
 
Example #27
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 5 votes vote down vote up
@Test
public void refresh() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    AuthResponse response = request.refresh(AuthToken.builder().build());
    Assert.assertNotNull(response);
    System.out.println(JSON.toJSONString(response.getData()));

}
 
Example #28
Source File: GithubRequest.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthRequest getRequest() {
    AuthConfig authConfig = properties.getGithub();
    return new AuthGithubRequest(AuthConfig.builder()
            .clientId(authConfig.getClientId())
            .clientSecret(authConfig.getClientSecret())
            .redirectUri(authConfig.getRedirectUri())
            .build());
}
 
Example #29
Source File: AuthStackOverflowRequest.java    From JustAuth with MIT License 4 votes vote down vote up
public AuthStackOverflowRequest(AuthConfig config) {
    super(config, STACK_OVERFLOW);
}
 
Example #30
Source File: AuthDingTalkRequest.java    From JustAuth with MIT License 4 votes vote down vote up
public AuthDingTalkRequest(AuthConfig config) {
    super(config, AuthDefaultSource.DINGTALK);
}