From 7fe8a4d4bbfbf97278e7f90dbc0824f7ca33b254 Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Tue, 16 Jul 2019 16:42:37 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E4=BB=8EUrlBuilder=E4=B8=AD?= =?UTF-8?q?=E6=8B=86=E5=88=86alipay=E3=80=81baidu=E3=80=81coding=E3=80=81c?= =?UTF-8?q?sdn=E3=80=81dingtalk=E3=80=81douyin=E3=80=81facebook=E5=92=8Cgi?= =?UTF-8?q?tee=E7=9B=B8=E5=85=B3=E7=9A=84URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhyd/oauth/request/AuthAlipayRequest.java | 9 +- .../zhyd/oauth/request/AuthBaiduRequest.java | 26 +- .../zhyd/oauth/request/AuthCodingRequest.java | 20 +- .../zhyd/oauth/request/AuthCsdnRequest.java | 22 +- .../oauth/request/AuthDingTalkRequest.java | 23 +- .../zhyd/oauth/request/AuthDouyinRequest.java | 27 +- .../oauth/request/AuthFacebookRequest.java | 21 +- .../zhyd/oauth/request/AuthGiteeRequest.java | 21 +- .../me/zhyd/oauth/url/AlipayUrlBuilder.java | 45 +++ .../me/zhyd/oauth/url/BaiduUrlBuilder.java | 49 +++ .../me/zhyd/oauth/url/CodingUrlBuilder.java | 50 +++ .../me/zhyd/oauth/url/CsdnUrlBuilder.java | 49 +++ .../me/zhyd/oauth/url/DingtalkUrlBuilder.java | 46 +++ .../me/zhyd/oauth/url/DouyinUrlBuilder.java | 50 +++ .../me/zhyd/oauth/url/FacebookUrlBuilder.java | 48 +++ .../me/zhyd/oauth/url/GiteeUrlBuilder.java | 48 +++ .../oauth/url/entity/AuthRevokeEntity.java | 2 + .../oauth/url/entity/AuthUserInfoEntity.java | 12 +- .../java/me/zhyd/oauth/utils/UrlBuilder.java | 327 ------------------ 19 files changed, 521 insertions(+), 374 deletions(-) create mode 100644 src/main/java/me/zhyd/oauth/url/AlipayUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/BaiduUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/CodingUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/CsdnUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/DingtalkUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/DouyinUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/FacebookUrlBuilder.java create mode 100644 src/main/java/me/zhyd/oauth/url/GiteeUrlBuilder.java delete mode 100644 src/main/java/me/zhyd/oauth/utils/UrlBuilder.java diff --git a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java index fbcb952..77f5ce0 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java @@ -14,8 +14,9 @@ import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; +import me.zhyd.oauth.url.AlipayUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; import me.zhyd.oauth.utils.StringUtils; -import me.zhyd.oauth.utils.UrlBuilder; /** * 支付宝登录 @@ -29,7 +30,7 @@ public class AuthAlipayRequest extends BaseAuthRequest { private AlipayClient alipayClient; public AuthAlipayRequest(AuthConfig config) { - super(config, AuthSource.ALIPAY); + super(config, AuthSource.ALIPAY, new AlipayUrlBuilder()); this.alipayClient = new DefaultAlipayClient(AuthSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config .getAlipayPublicKey(), "RSA2"); } @@ -93,6 +94,8 @@ public class AuthAlipayRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java b/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java index 46f6a90..144f769 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java @@ -8,7 +8,11 @@ import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.enums.AuthBaiduErrorCode; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.*; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.BaiduUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthRevokeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** * 百度账号登录 @@ -20,13 +24,15 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthBaiduRequest extends BaseAuthRequest { public AuthBaiduRequest(AuthConfig config) { - super(config, AuthSource.BAIDU); + super(config, AuthSource.BAIDU, new BaiduUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getBaiduAccessTokenUrl(config.getClientId(), config.getClientSecret(), authCallback.getCode(), config - .getRedirectUri()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); HttpResponse response = HttpRequest.post(accessTokenUrl).execute(); JSONObject accessTokenObject = JSONObject.parseObject(response.body()); AuthBaiduErrorCode errorCode = AuthBaiduErrorCode.getErrorCode(accessTokenObject.getString("error")); @@ -44,7 +50,9 @@ public class AuthBaiduRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduUserInfoUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .build())).execute(); String userInfo = response.body(); JSONObject object = JSONObject.parseObject(userInfo); AuthBaiduErrorCode errorCode = AuthBaiduErrorCode.getErrorCode(object.getString("error")); @@ -68,13 +76,17 @@ public class AuthBaiduRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getBaiduAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } @Override public AuthResponse revoke(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduRevokeUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getRevokeUrl(AuthRevokeEntity.builder() + .accessToken(accessToken) + .build())).execute(); String userInfo = response.body(); JSONObject object = JSONObject.parseObject(userInfo); if (object.containsKey("error_code")) { diff --git a/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java b/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java index 690a5ee..e262d32 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java @@ -10,7 +10,10 @@ import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.CodingUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** * Cooding登录 @@ -22,12 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthCodingRequest extends BaseAuthRequest { public AuthCodingRequest(AuthConfig config) { - super(config, AuthSource.CODING); + super(config, AuthSource.CODING, new CodingUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getCodingAccessTokenUrl(config.getClientId(), config.getClientSecret(), authCallback.getCode()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); HttpResponse response = HttpRequest.get(accessTokenUrl).execute(); JSONObject accessTokenObject = JSONObject.parseObject(response.body()); if (accessTokenObject.getIntValue("code") != 0) { @@ -43,7 +49,9 @@ public class AuthCodingRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getCodingUserInfoUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .build())).execute(); JSONObject object = JSONObject.parseObject(response.body()); if (object.getIntValue("code") != 0) { throw new AuthException(object.getString("msg")); @@ -73,6 +81,8 @@ public class AuthCodingRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java b/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java index 43817c2..292149d 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java @@ -10,7 +10,10 @@ import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.CsdnUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** * CSDN登录 @@ -19,16 +22,19 @@ import me.zhyd.oauth.utils.UrlBuilder; * @version 1.0 * @since 1.8 */ +@Deprecated public class AuthCsdnRequest extends BaseAuthRequest { public AuthCsdnRequest(AuthConfig config) { - super(config, AuthSource.CSDN); + super(config, AuthSource.CSDN, new CsdnUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getCsdnAccessTokenUrl(config.getClientId(), config.getClientSecret(), authCallback.getCode(), config - .getRedirectUri()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); HttpResponse response = HttpRequest.post(accessTokenUrl).execute(); JSONObject accessTokenObject = JSONObject.parseObject(response.body()); if (accessTokenObject.containsKey("error_code")) { @@ -40,7 +46,9 @@ public class AuthCsdnRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getCsdnUserInfoUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .build())).execute(); JSONObject object = JSONObject.parseObject(response.body()); if (object.containsKey("error_code")) { throw new AuthException(object.getString("error")); @@ -63,6 +71,8 @@ public class AuthCsdnRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getCsdnAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java index ff802cc..7eb90bb 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java @@ -8,9 +8,14 @@ import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.enums.AuthDingTalkErrorCode; import me.zhyd.oauth.exception.AuthException; -import me.zhyd.oauth.model.*; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthToken; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.model.AuthUserGender; +import me.zhyd.oauth.url.DingtalkUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; import me.zhyd.oauth.utils.GlobalAuthUtil; -import me.zhyd.oauth.utils.UrlBuilder; /** * 钉钉登录 @@ -22,7 +27,7 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthDingTalkRequest extends BaseAuthRequest { public AuthDingTalkRequest(AuthConfig config) { - super(config, AuthSource.DINGTALK); + super(config, AuthSource.DINGTALK, new DingtalkUrlBuilder()); } @Override @@ -38,8 +43,12 @@ public class AuthDingTalkRequest extends BaseAuthRequest { String urlEncodeSignature = GlobalAuthUtil.generateDingTalkSignature(config.getClientSecret(), timestamp); JSONObject param = new JSONObject(); param.put("tmp_auth_code", code); - HttpResponse response = HttpRequest.post(UrlBuilder.getDingTalkUserInfoUrl(urlEncodeSignature, timestamp, config - .getClientId())).body(param.toJSONString()).execute(); + HttpResponse response = HttpRequest.post(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .signature(urlEncodeSignature) + .timestamp(timestamp) + .clientId(config.getClientId()) + .build() + )).body(param.toJSONString()).execute(); String userInfo = response.body(); JSONObject object = JSON.parseObject(userInfo); AuthDingTalkErrorCode errorCode = AuthDingTalkErrorCode.getErrorCode(object.getIntValue("errcode")); @@ -68,6 +77,8 @@ public class AuthDingTalkRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getDingTalkQrConnectUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java index cf29f19..7e87b92 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java @@ -7,7 +7,11 @@ import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.*; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.DouyinUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthRefreshTokenEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** @@ -20,12 +24,15 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthDouyinRequest extends BaseAuthRequest { public AuthDouyinRequest(AuthConfig config) { - super(config, AuthSource.DOUYIN); + super(config, AuthSource.DOUYIN, new DouyinUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getDouyinAccessTokenUrl(config.getClientId(), config.getClientSecret(), authCallback.getCode()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); return this.getToken(accessTokenUrl); } @@ -33,7 +40,10 @@ public class AuthDouyinRequest extends BaseAuthRequest { protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); String openId = authToken.getOpenId(); - String url = UrlBuilder.getDouyinUserInfoUrl(accessToken, openId); + String url = this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .openId(openId) + .build()); HttpResponse response = HttpRequest.get(url).execute(); JSONObject object = JSONObject.parseObject(response.body()); @@ -58,12 +68,17 @@ public class AuthDouyinRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getDouyinAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } @Override public AuthResponse refresh(AuthToken oldToken) { - String refreshTokenUrl = UrlBuilder.getDouyinRefreshUrl(config.getClientId(), oldToken.getRefreshToken()); + String refreshTokenUrl = this.urlBuilder.getRefreshUrl(AuthRefreshTokenEntity.builder() + .config(config) + .refreshToken(oldToken.getRefreshToken()) + .build()); return AuthResponse.builder() .code(ResponseStatus.SUCCESS.getCode()) .data(this.getToken(refreshTokenUrl)) diff --git a/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java b/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java index bc8fcac..4db331a 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java @@ -10,7 +10,10 @@ import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.FacebookUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** * Facebook登录 @@ -22,13 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthFacebookRequest extends BaseAuthRequest { public AuthFacebookRequest(AuthConfig config) { - super(config, AuthSource.FACEBOOK); + super(config, AuthSource.FACEBOOK, new FacebookUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getFacebookAccessTokenUrl(config.getClientId(), config.getClientSecret(), - authCallback.getCode(), config.getRedirectUri()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); HttpResponse response = HttpRequest.post(accessTokenUrl).execute(); JSONObject accessTokenObject = JSONObject.parseObject(response.body()); @@ -46,7 +51,9 @@ public class AuthFacebookRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getFacebookUserInfoUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .build())).execute(); String userInfo = response.body(); JSONObject object = JSONObject.parseObject(userInfo); if (object.containsKey("error")) { @@ -80,6 +87,8 @@ public class AuthFacebookRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getFacebookAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java b/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java index 192f684..68eb429 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java @@ -10,7 +10,10 @@ import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUserGender; -import me.zhyd.oauth.utils.UrlBuilder; +import me.zhyd.oauth.url.GiteeUrlBuilder; +import me.zhyd.oauth.url.entity.AuthAccessTokenEntity; +import me.zhyd.oauth.url.entity.AuthAuthorizeEntity; +import me.zhyd.oauth.url.entity.AuthUserInfoEntity; /** * Gitee登录 @@ -22,13 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder; public class AuthGiteeRequest extends BaseAuthRequest { public AuthGiteeRequest(AuthConfig config) { - super(config, AuthSource.GITEE); + super(config, AuthSource.GITEE, new GiteeUrlBuilder()); } @Override protected AuthToken getAccessToken(AuthCallback authCallback) { - String accessTokenUrl = UrlBuilder.getGiteeAccessTokenUrl(config.getClientId(), config.getClientSecret(), - authCallback.getCode(), config.getRedirectUri()); + String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder() + .config(config) + .code(authCallback.getCode()) + .build()); HttpResponse response = HttpRequest.post(accessTokenUrl).execute(); JSONObject accessTokenObject = JSONObject.parseObject(response.body()); if (accessTokenObject.containsKey("error")) { @@ -40,7 +45,9 @@ public class AuthGiteeRequest extends BaseAuthRequest { @Override protected AuthUser getUserInfo(AuthToken authToken) { String accessToken = authToken.getAccessToken(); - HttpResponse response = HttpRequest.get(UrlBuilder.getGiteeUserInfoUrl(accessToken)).execute(); + HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder() + .accessToken(accessToken) + .build())).execute(); String userInfo = response.body(); JSONObject object = JSONObject.parseObject(userInfo); return AuthUser.builder() @@ -66,6 +73,8 @@ public class AuthGiteeRequest extends BaseAuthRequest { */ @Override public String authorize() { - return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState()); + return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder() + .config(config) + .build()); } } diff --git a/src/main/java/me/zhyd/oauth/url/AlipayUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/AlipayUrlBuilder.java new file mode 100644 index 0000000..ff54f54 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/AlipayUrlBuilder.java @@ -0,0 +1,45 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * Baidu相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class AlipayUrlBuilder extends AbstractUrlBuilder { + + private static final String ALIPAY_AUTHORIZE_PATTERN = "{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state={3}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + return null; + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return null; + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(ALIPAY_AUTHORIZE_PATTERN, AuthSource.ALIPAY.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/BaiduUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/BaiduUrlBuilder.java new file mode 100644 index 0000000..fe0480f --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/BaiduUrlBuilder.java @@ -0,0 +1,49 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * Baidu相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class BaiduUrlBuilder extends AbstractUrlBuilder { + + private static final String BAIDU_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; + private static final String BAIDU_USER_INFO_PATTERN = "{0}?access_token={1}"; + private static final String BAIDU_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&display=popup&state={3}"; + private static final String BAIDU_REVOKE_PATTERN = "{0}?access_token={1}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(BAIDU_ACCESS_TOKEN_PATTERN, AuthSource.BAIDU.accessToken(), config.getClientId(), config.getClientSecret(), accessTokenEntity.getCode(), config.getRedirectUri()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(BAIDU_USER_INFO_PATTERN, AuthSource.BAIDU.userInfo(), userInfoEntity.getAccessToken()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(BAIDU_AUTHORIZE_PATTERN, AuthSource.BAIDU.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return MessageFormat.format(BAIDU_REVOKE_PATTERN, AuthSource.BAIDU.revoke(), revokeEntity.getAccessToken()); + } +} diff --git a/src/main/java/me/zhyd/oauth/url/CodingUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/CodingUrlBuilder.java new file mode 100644 index 0000000..9f78e33 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/CodingUrlBuilder.java @@ -0,0 +1,50 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * Coding相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class CodingUrlBuilder extends AbstractUrlBuilder { + + private static final String CODING_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}"; + private static final String CODING_USER_INFO_PATTERN = "{0}?access_token={1}"; + private static final String CODING_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&scope=user&state={3}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(CODING_ACCESS_TOKEN_PATTERN, AuthSource.CODING.accessToken(), config.getClientId(), + config.getClientSecret(), accessTokenEntity.getCode()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(CODING_USER_INFO_PATTERN, AuthSource.CODING.userInfo(), userInfoEntity.getAccessToken()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(CODING_AUTHORIZE_PATTERN, AuthSource.CODING.authorize(), config.getClientId(), + config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/CsdnUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/CsdnUrlBuilder.java new file mode 100644 index 0000000..8573bcc --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/CsdnUrlBuilder.java @@ -0,0 +1,49 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * csdn相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +@Deprecated +public class CsdnUrlBuilder extends AbstractUrlBuilder { + + private static final String CSDN_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; + private static final String CSDN_USER_INFO_PATTERN = "{0}?access_token={1}"; + private static final String CSDN_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(CSDN_ACCESS_TOKEN_PATTERN, AuthSource.CSDN.accessToken(), config.getClientId(), config.getClientSecret(), accessTokenEntity.getCode(), config.getRedirectUri()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(CSDN_USER_INFO_PATTERN, AuthSource.CSDN.userInfo(), userInfoEntity.getAccessToken()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(CSDN_AUTHORIZE_PATTERN, AuthSource.CSDN.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/DingtalkUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/DingtalkUrlBuilder.java new file mode 100644 index 0000000..580e0d6 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/DingtalkUrlBuilder.java @@ -0,0 +1,46 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * 钉钉相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class DingtalkUrlBuilder extends AbstractUrlBuilder { + + private static final String DING_TALK_QRCONNECT_PATTERN = "{0}?appid={1}&response_type=code&scope=snsapi_login&redirect_uri={2}&state={3}"; + private static final String DING_TALK_USER_INFO_PATTERN = "{0}?signature={1}×tamp={2}&accessKey={3}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + return null; + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(DING_TALK_USER_INFO_PATTERN, AuthSource.DINGTALK.userInfo(), userInfoEntity.getSignature(), userInfoEntity.getTimestamp(), userInfoEntity.getClientId()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(DING_TALK_QRCONNECT_PATTERN, AuthSource.DINGTALK.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/DouyinUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/DouyinUrlBuilder.java new file mode 100644 index 0000000..7d7bd4a --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/DouyinUrlBuilder.java @@ -0,0 +1,50 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * 抖音相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class DouyinUrlBuilder extends AbstractUrlBuilder { + + private static final String DOUYIN_AUTHORIZE_PATTERN = "{0}?client_key={1}&redirect_uri={2}&state={3}&response_type=code&scope=user_info"; + private static final String DOUYIN_ACCESS_TOKEN_PATTERN = "{0}?client_key={1}&client_secret={2}&code={3}&grant_type=authorization_code"; + private static final String DOUYIN_USER_INFO_PATTERN = "{0}?access_token={1}&open_id={2}"; + private static final String DOUYIN_REFRESH_TOKEN_PATTERN = "{0}?client_key={1}&refresh_token={2}&grant_type=refresh_token"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(DOUYIN_ACCESS_TOKEN_PATTERN, AuthSource.DOUYIN.accessToken(), config.getClientId(), config.getClientSecret(), accessTokenEntity.getCode()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(DOUYIN_USER_INFO_PATTERN, AuthSource.DOUYIN.userInfo(), userInfoEntity.getAccessToken(), userInfoEntity.getOpenId()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(DOUYIN_AUTHORIZE_PATTERN, AuthSource.DOUYIN.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + AuthConfig config = refreshTokenEntity.getConfig(); + return MessageFormat.format(DOUYIN_REFRESH_TOKEN_PATTERN, AuthSource.DOUYIN.refresh(), config.getClientId(), refreshTokenEntity.getRefreshToken()); + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/FacebookUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/FacebookUrlBuilder.java new file mode 100644 index 0000000..2205f87 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/FacebookUrlBuilder.java @@ -0,0 +1,48 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * Facebook相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class FacebookUrlBuilder extends AbstractUrlBuilder { + + private static final String FACEBOOK_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope="; + private static final String FACEBOOK_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"; + private static final String FACEBOOK_USER_INFO_PATTERN = "{0}?access_token={1}&fields=id,name,birthday,gender,hometown,email,devices,picture.width(400)"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(FACEBOOK_ACCESS_TOKEN_PATTERN, AuthSource.FACEBOOK.accessToken(), config.getClientId(), config.getClientSecret(), accessTokenEntity.getCode(), config.getRedirectUri()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(FACEBOOK_USER_INFO_PATTERN, AuthSource.FACEBOOK.userInfo(), userInfoEntity.getAccessToken()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(FACEBOOK_AUTHORIZE_PATTERN, AuthSource.FACEBOOK.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/GiteeUrlBuilder.java b/src/main/java/me/zhyd/oauth/url/GiteeUrlBuilder.java new file mode 100644 index 0000000..d7d13e8 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/url/GiteeUrlBuilder.java @@ -0,0 +1,48 @@ +package me.zhyd.oauth.url; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.url.entity.*; + +import java.text.MessageFormat; + +/** + * Gitee相关的URL构建类 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public class GiteeUrlBuilder extends AbstractUrlBuilder { + + private static final String GITEE_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; + private static final String GITEE_USER_INFO_PATTERN = "{0}?access_token={1}"; + private static final String GITEE_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"; + + @Override + public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) { + AuthConfig config = accessTokenEntity.getConfig(); + return MessageFormat.format(GITEE_ACCESS_TOKEN_PATTERN, AuthSource.GITEE.accessToken(), config.getClientId(), config.getClientSecret(), accessTokenEntity.getCode(), config.getRedirectUri()); + } + + @Override + public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) { + return MessageFormat.format(GITEE_USER_INFO_PATTERN, AuthSource.GITEE.userInfo(), userInfoEntity.getAccessToken()); + } + + @Override + public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) { + AuthConfig config = authorizeEntity.getConfig(); + return MessageFormat.format(GITEE_AUTHORIZE_PATTERN, AuthSource.GITEE.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState())); + } + + @Override + public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) { + return null; + } + + @Override + public String getRevokeUrl(AuthRevokeEntity revokeEntity) { + return null; + } +} diff --git a/src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java b/src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java index d9ab0a0..a43abe3 100644 --- a/src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java +++ b/src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java @@ -10,4 +10,6 @@ import lombok.*; @Getter @Builder public class AuthRevokeEntity { + + private String accessToken; } diff --git a/src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java b/src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java index 3e2a856..54fb17e 100644 --- a/src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java +++ b/src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java @@ -19,12 +19,20 @@ public class AuthUserInfoEntity { * 用户openId */ private String openId; + /** + * client key + */ + private String clientId; /** * 额外的属性 */ private String extra; /** - * client key + * 额外的属性 */ - private String clientId; + private String signature; + /** + * 额外的属性 + */ + private String timestamp; } diff --git a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java deleted file mode 100644 index ba1d600..0000000 --- a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java +++ /dev/null @@ -1,327 +0,0 @@ -package me.zhyd.oauth.utils; - -import me.zhyd.oauth.config.AuthSource; - -import java.text.MessageFormat; - -/** - * Url构建工具类 - * - * @author yadong.zhang (yadong.zhang0415(a)gmail.com) - * @version 1.0 - * @since 1.0 - */ -public class UrlBuilder { - - - - private static final String GITEE_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; - private static final String GITEE_USER_INFO_PATTERN = "{0}?access_token={1}"; - private static final String GITEE_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"; - - private static final String DING_TALK_QRCONNECT_PATTERN = "{0}?appid={1}&response_type=code&scope=snsapi_login&redirect_uri={2}&state={3}"; - private static final String DING_TALK_USER_INFO_PATTERN = "{0}?signature={1}×tamp={2}&accessKey={3}"; - - private static final String BAIDU_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; - private static final String BAIDU_USER_INFO_PATTERN = "{0}?access_token={1}"; - private static final String BAIDU_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&display=popup&state={3}"; - private static final String BAIDU_REVOKE_PATTERN = "{0}?access_token={1}"; - - private static final String CSDN_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"; - private static final String CSDN_USER_INFO_PATTERN = "{0}?access_token={1}"; - private static final String CSDN_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"; - - private static final String CODING_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}"; - private static final String CODING_USER_INFO_PATTERN = "{0}?access_token={1}"; - private static final String CODING_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&scope=user&state={3}"; - - private static final String ALIPAY_AUTHORIZE_PATTERN = "{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state={3}"; - - - private static final String FACEBOOK_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope="; - private static final String FACEBOOK_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"; - private static final String FACEBOOK_USER_INFO_PATTERN = "{0}?access_token={1}&fields=id,name,birthday,gender,hometown,email,devices,picture.width(400)"; - - private static final String DOUYIN_AUTHORIZE_PATTERN = "{0}?client_key={1}&redirect_uri={2}&state={3}&response_type=code&scope=user_info"; - private static final String DOUYIN_ACCESS_TOKEN_PATTERN = "{0}?client_key={1}&client_secret={2}&code={3}&grant_type=authorization_code"; - private static final String DOUYIN_USER_INFO_PATTERN = "{0}?access_token={1}&open_id={2}"; - private static final String DOUYIN_REFRESH_TOKEN_PATTERN = "{0}?client_key={1}&refresh_token={2}&grant_type=refresh_token"; - - - - /** - * 获取state,如果为空, 则默认去当前日期的时间戳 - * - * @param state state - */ - private static Object getState(String state) { - return StringUtils.isEmpty(state) ? String.valueOf(System.currentTimeMillis()) : state; - } - - /** - * 获取gitee token的接口地址 - * - * @param clientId gitee 应用的Client ID - * @param clientSecret gitee 应用的Client Secret - * @param code gitee 授权前的code,用来换token - * @param redirectUri 待跳转的页面 - * @return full url - */ - public static String getGiteeAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) { - return MessageFormat.format(GITEE_ACCESS_TOKEN_PATTERN, AuthSource.GITEE.accessToken(), clientId, clientSecret, code, redirectUri); - } - - /** - * 获取gitee用户详情的接口地址 - * - * @param token gitee 应用的token - * @return full url - */ - public static String getGiteeUserInfoUrl(String token) { - return MessageFormat.format(GITEE_USER_INFO_PATTERN, AuthSource.GITEE.userInfo(), token); - } - - /** - * 获取gitee授权地址 - * - * @param clientId gitee 应用的Client ID - * @param redirectUrl gitee 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return json - */ - public static String getGiteeAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(GITEE_AUTHORIZE_PATTERN, AuthSource.GITEE.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取钉钉登录二维码的地址 - * - * @param clientId 钉钉 应用的App Id - * @param redirectUrl 钉钉 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getDingTalkQrConnectUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(DING_TALK_QRCONNECT_PATTERN, AuthSource.DINGTALK.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取钉钉用户信息的地址 - * - * @param signature 通过appSecret计算出来的签名值,签名计算方法:https://open-doc.dingtalk.com/microapp/faquestions/hxs5v9 - * @param timestamp 当前时间戳,单位是毫秒 - * @param accessKey 钉钉 应用的App Id - * @return full url - */ - public static String getDingTalkUserInfoUrl(String signature, String timestamp, String accessKey) { - return MessageFormat.format(DING_TALK_USER_INFO_PATTERN, AuthSource.DINGTALK.userInfo(), signature, timestamp, accessKey); - } - - /** - * 获取baidu token的接口地址 - * - * @param clientId baidu 应用的API Key - * @param clientSecret baidu 应用的Secret Key - * @param code baidu 授权前的code,用来换token - * @param redirectUri 待跳转的页面 - * @return full url - */ - public static String getBaiduAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) { - return MessageFormat.format(BAIDU_ACCESS_TOKEN_PATTERN, AuthSource.BAIDU.accessToken(), clientId, clientSecret, code, redirectUri); - } - - /** - * 获取baidu用户详情的接口地址 - * - * @param token baidu 应用的token - * @return full url - */ - public static String getBaiduUserInfoUrl(String token) { - return MessageFormat.format(BAIDU_USER_INFO_PATTERN, AuthSource.BAIDU.userInfo(), token); - } - - /** - * 获取baidu授权地址 - * - * @param clientId baidu 应用的API Key - * @param redirectUrl baidu 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return json - */ - public static String getBaiduAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(BAIDU_AUTHORIZE_PATTERN, AuthSource.BAIDU.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取收回baidu授权的地址 - * - * @param accessToken baidu 授权登录后的token - * @return json - */ - public static String getBaiduRevokeUrl(String accessToken) { - return MessageFormat.format(BAIDU_REVOKE_PATTERN, AuthSource.BAIDU.revoke(), accessToken); - } - - /** - * 获取csdn token的接口地址 - * - * @param clientId csdn 应用的App Key - * @param clientSecret csdn 应用的App Secret - * @param code csdn 授权前的code,用来换token - * @param redirectUri 待跳转的页面 - * @return full url - */ - public static String getCsdnAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) { - return MessageFormat.format(CSDN_ACCESS_TOKEN_PATTERN, AuthSource.CSDN.accessToken(), clientId, clientSecret, code, redirectUri); - } - - /** - * 获取csdn用户详情的接口地址 - * - * @param token csdn 应用的token - * @return full url - */ - public static String getCsdnUserInfoUrl(String token) { - return MessageFormat.format(CSDN_USER_INFO_PATTERN, AuthSource.CSDN.userInfo(), token); - } - - /** - * 获取csdn授权地址 - * - * @param clientId csdn 应用的Client ID - * @param redirectUrl csdn 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getCsdnAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(CSDN_AUTHORIZE_PATTERN, AuthSource.CSDN.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取coding token的接口地址 - * - * @param clientId coding 应用的App Key - * @param clientSecret coding 应用的App Secret - * @param code coding 授权前的code,用来换token - * @return full url - */ - public static String getCodingAccessTokenUrl(String clientId, String clientSecret, String code) { - return MessageFormat.format(CODING_ACCESS_TOKEN_PATTERN, AuthSource.CODING.accessToken(), clientId, clientSecret, code); - } - - /** - * 获取coding用户详情的接口地址 - * - * @param token coding 应用的token - * @return full url - */ - public static String getCodingUserInfoUrl(String token) { - return MessageFormat.format(CODING_USER_INFO_PATTERN, AuthSource.CODING.userInfo(), token); - } - - /** - * 获取coding授权地址 - * - * @param clientId coding 应用的Client ID - * @param redirectUrl coding 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getCodingAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(CODING_AUTHORIZE_PATTERN, AuthSource.CODING.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取alipay授权地址 - * - * @param clientId alipay 应用的Client ID - * @param redirectUrl alipay 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getAlipayAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(ALIPAY_AUTHORIZE_PATTERN, AuthSource.ALIPAY.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取Facebook授权地址 - * - * @param clientId Facebook 应用的Client ID - * @param redirectUrl Facebook 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getFacebookAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(FACEBOOK_AUTHORIZE_PATTERN, AuthSource.FACEBOOK.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取Facebook token的接口地址 - * - * @param clientId Facebook 应用的Client ID - * @param clientSecret Facebook 应用的Client Secret - * @param code Facebook 授权前的code,用来换token - * @param redirectUri 待跳转的页面 - * @return full url - */ - public static String getFacebookAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) { - return MessageFormat.format(FACEBOOK_ACCESS_TOKEN_PATTERN, AuthSource.FACEBOOK.accessToken(), clientId, clientSecret, code, redirectUri); - } - - /** - * 获取Facebook用户详情的接口地址 - * - * @param token Facebook 应用的token - * @return full url - */ - public static String getFacebookUserInfoUrl(String token) { - return MessageFormat.format(FACEBOOK_USER_INFO_PATTERN, AuthSource.FACEBOOK.userInfo(), token); - } - - /** - * 获取Douyin授权地址 - * - * @param clientId Douyin 应用的Client ID - * @param redirectUrl Douyin 应用授权成功后的回调地址 - * @param state 随机字符串,用于保持会话状态,防止CSRF攻击 - * @return full url - */ - public static String getDouyinAuthorizeUrl(String clientId, String redirectUrl, String state) { - return MessageFormat.format(DOUYIN_AUTHORIZE_PATTERN, AuthSource.DOUYIN.authorize(), clientId, redirectUrl, getState(state)); - } - - /** - * 获取Douyin token的接口地址 - * - * @param clientId Douyin 应用的Client ID - * @param clientSecret Douyin 应用的Client Secret - * @param code Douyin 授权前的code,用来换token - * @return full url - */ - public static String getDouyinAccessTokenUrl(String clientId, String clientSecret, String code) { - return MessageFormat.format(DOUYIN_ACCESS_TOKEN_PATTERN, AuthSource.DOUYIN.accessToken(), clientId, clientSecret, code); - } - - /** - * 获取Douyin用户详情的接口地址 - * - * @param token Douyin 应用的token - * @param openId 用户在当前应用的唯一标识 通过token接口获取 - * @return full url - */ - public static String getDouyinUserInfoUrl(String token, String openId) { - return MessageFormat.format(DOUYIN_USER_INFO_PATTERN, AuthSource.DOUYIN.userInfo(), token, openId); - } - - /** - * 获取Douyin 刷新令牌 地址 - * - * @param clientId Douyin 应用的client_key - * @param refreshToken Douyin 应用返回的refresh_token - * @return full url - */ - public static String getDouyinRefreshUrl(String clientId, String refreshToken) { - return MessageFormat.format(DOUYIN_REFRESH_TOKEN_PATTERN, AuthSource.DOUYIN.refresh(), clientId, refreshToken); - } - -} -- GitLab