1
0
mirror of synced 2026-02-22 12:37:49 +08:00

Merge branch 'dev' into refactor-1.9.3

This commit is contained in:
yadong.zhang
2019-07-30 21:15:12 +08:00
committed by GitHub
24 changed files with 89 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
package me.zhyd.oauth.config;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthResponseStatus;
/**
* 各api需要的url 用枚举类分平台类型管理

View File

@@ -1,15 +1,21 @@
package me.zhyd.oauth.model;
package me.zhyd.oauth.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* JustAuth通用的状态码对照表
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @since 1.8
*/
@Getter
@AllArgsConstructor
public enum AuthResponseStatus {
/**
* 2000正常
* other调用异常具体异常内容见{@code msg}
*/
SUCCESS(2000, "Success"),
FAILURE(5000, "Failure"),
NOT_IMPLEMENTED(5001, "Not Implemented"),

View File

@@ -12,6 +12,10 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum AuthToutiaoErrorCode {
/**
* 0正常
* other调用异常具体异常内容见{@code desc}
*/
EC0(0, "接口调用成功"),
EC1(1, "API配置错误未传入Client Key"),
EC2(2, "API配置错误Client Key错误请检查是否和开放平台的ClientKey一致"),

View File

@@ -14,7 +14,14 @@ import java.util.Arrays;
@Getter
@AllArgsConstructor
public enum AuthUserGender {
MALE(1, ""), FEMALE(0, ""), UNKNOWN(-1, "未知");
/**
* MALE/FAMALE为正常值通过{@link AuthUserGender#getRealGender(String)}方法获取真实的性别
* UNKNOWN为容错值部分平台不会返回用户性别为了方便统一使用UNKNOWN标记所有未知或不可测的用户性别信息
*/
MALE(1, ""),
FEMALE(0, ""),
UNKNOWN(-1, "未知");
private int code;
private String desc;

View File

@@ -1,8 +1,10 @@
package me.zhyd.oauth.exception;
import me.zhyd.oauth.model.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthResponseStatus;
/**
* JustAuth通用异常类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @since 1.8
*/

View File

@@ -2,7 +2,6 @@ package me.zhyd.oauth.model;
import lombok.Getter;
import lombok.Setter;
import me.zhyd.oauth.cache.AuthStateCache;
/**
* 授权回调时的参数类
@@ -28,14 +27,4 @@ public class AuthCallback {
* 访问AuthorizeUrl后回调时带的参数state用于和请求AuthorizeUrl前的state比较防止CSRF攻击
*/
private String state;
/**
* 内置的检验state合法性的方法
*
* @return true state正常falsestate不正常可能授权时间过长导致state失效
* @since 1.9.3
*/
public boolean checkState() {
return AuthStateCache.containsKey(this.state);
}
}

View File

@@ -2,7 +2,7 @@ package me.zhyd.oauth.model;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import me.zhyd.oauth.enums.AuthResponseStatus;
/**
* JustAuth统一授权响应类

View File

@@ -5,6 +5,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -6,8 +6,12 @@ import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.AuthChecker;
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;
@@ -35,13 +39,38 @@ public abstract class AuthDefaultRequest implements AuthRequest {
AuthChecker.checkConfig(config, source);
}
/**
* 获取access token
*
* @param authCallback 授权成功后的回调参数
* @return token
* @see AuthDefaultRequest#authorize()
* @see AuthDefaultRequest#authorize(String)
*/
protected abstract AuthToken getAccessToken(AuthCallback authCallback);
/**
* 使用token换取用户信息
*
* @param authToken token信息
* @return 用户信息
* @see AuthDefaultRequest#getAccessToken(AuthCallback)
*/
protected abstract AuthUser getUserInfo(AuthToken authToken);
/**
* 统一的登录入口。当通过{@link AuthDefaultRequest#authorize(String)}授权成功后,会跳转到调用方的相关回调方法中
* 方法的入参可以使用{@code AuthCallback}{@code AuthCallback}类中封装好了OAuth2授权回调所需要的参数
*
* @param authCallback 用于接收回调参数的实体
* @return AuthResponse
*/
@Override
public AuthResponse login(AuthCallback authCallback) {
try {
if (!AuthStateCache.containsKey(authCallback.getState())) {
throw new AuthException(AuthResponseStatus.ILLEGAL_REQUEST);
}
AuthChecker.checkCode(source == AuthSource.ALIPAY ? authCallback.getAuth_code() : authCallback.getCode());
AuthChecker.checkState(authCallback);
@@ -54,6 +83,12 @@ public abstract class AuthDefaultRequest implements AuthRequest {
}
}
/**
* 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
*
* @param e 具体的异常
* @return AuthResponse
*/
private AuthResponse responseError(Exception e) {
int errorCode = AuthResponseStatus.FAILURE.getCode();
if (e instanceof AuthException) {
@@ -207,7 +242,6 @@ public abstract class AuthDefaultRequest implements AuthRequest {
*
* @param authToken token封装
* @return HttpResponse
* @since
*/
@Deprecated
protected HttpResponse doPostRevoke(AuthToken authToken) {

View File

@@ -5,6 +5,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;
@@ -112,7 +113,6 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
* @return 用户的邮箱地址
*/
private String getUserEmail(String accessToken) {
String email = null;
HttpResponse emailResponse = HttpRequest.get("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))")
.header("Host", "api.linkedin.com")
.header("Connection", "Keep-Alive")

View File

@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -5,6 +5,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;
@@ -122,7 +123,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
/**
* 返回获取accessToken的url
*
* @param code
* @param code 授权code
* @return 返回获取accessToken的url
*/
@Override

View File

@@ -6,6 +6,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -13,7 +13,7 @@ import me.zhyd.oauth.utils.UrlBuilder;
import java.util.Objects;
import static me.zhyd.oauth.config.AuthSource.RENREN;
import static me.zhyd.oauth.model.AuthResponseStatus.SUCCESS;
import static me.zhyd.oauth.enums.AuthResponseStatus.SUCCESS;
/**
* 人人登录

View File

@@ -3,7 +3,7 @@ package me.zhyd.oauth.request;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.model.AuthToken;
/**

View File

@@ -5,6 +5,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -5,6 +5,7 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;

View File

@@ -9,7 +9,7 @@ package me.zhyd.oauth.utils;
public class AuthStateUtils {
/**
* 生成随机state采用{@see https://github.com/lets-mica/mica}的UUID工具
* 生成随机state采用https://github.com/lets-mica/mica的UUID工具
*
* @return 随机的state字符串
*/

View File

@@ -4,7 +4,7 @@ import java.nio.charset.StandardCharsets;
import java.util.concurrent.ThreadLocalRandom;
/**
* 高性能的创建UUID的工具类{@see https://github.com/lets-mica/mica}
* 高性能的创建UUID的工具类https://github.com/lets-mica/mica
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @since 1.9.3

View File

@@ -21,7 +21,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -36,7 +35,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -51,7 +49,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -66,7 +63,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -81,7 +77,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -96,7 +91,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -111,7 +105,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -126,7 +119,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
authRequest.login(new AuthCallback());
}
@@ -142,7 +134,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -157,7 +148,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -172,7 +162,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -187,7 +176,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -202,7 +190,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -217,7 +204,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -232,7 +218,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -247,7 +232,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -262,7 +246,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -277,7 +260,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}
@@ -292,7 +274,6 @@ public class AuthRequestTest {
// 返回授权页面,可自行跳转
authRequest.authorize("state");
// 授权登录后会返回codeauth_code仅限支付宝、state1.8.0版本后可以用AuthCallback类作为回调接口的入参
// 1.9.3版本后 如果需要验证state可以在login之前调用{@see AuthCallback#checkState}方法校验state合法性
// 注JustAuth默认保存state的时效为3分钟3分钟内未使用则会自动清除过期的state
AuthResponse login = authRequest.login(new AuthCallback());
}