提交 ec31b7cb 编写于 作者: 智布道's avatar 智布道 👁

从UrlBuilder中拆分小米和OSChina相关的URL

上级 6b196456
......@@ -8,7 +8,10 @@ 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.entity.AuthAccessTokenEntity;
import me.zhyd.oauth.url.entity.AuthAuthorizeEntity;
import me.zhyd.oauth.url.entity.AuthRefreshTokenEntity;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.text.MessageFormat;
......@@ -28,7 +31,10 @@ public class AuthMiRequest extends BaseAuthRequest {
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
String accessTokenUrl = UrlBuilder.getMiAccessTokenUrl(config.getClientId(), config.getClientSecret(), config.getRedirectUri(), authCallback.getCode());
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder()
.config(config)
.code(authCallback.getCode())
.build());
return getToken(accessTokenUrl);
}
......@@ -56,7 +62,10 @@ public class AuthMiRequest extends BaseAuthRequest {
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
// 获取用户信息
HttpResponse userResponse = HttpRequest.get(UrlBuilder.getMiUserInfoUrl(config.getClientId(), authToken.getAccessToken()))
HttpResponse userResponse = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
.clientId(config.getClientId())
.accessToken(authToken.getAccessToken())
.build()))
.execute();
JSONObject userProfile = JSONObject.parseObject(userResponse.body());
......@@ -98,7 +107,9 @@ public class AuthMiRequest extends BaseAuthRequest {
*/
@Override
public String authorize() {
return UrlBuilder.getMiAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState());
return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder()
.config(config)
.build());
}
/**
......@@ -109,8 +120,10 @@ public class AuthMiRequest extends BaseAuthRequest {
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
String miRefreshUrl = UrlBuilder.getMiRefreshUrl(config.getClientId(), config.getClientSecret(), config.getRedirectUri(), authToken
.getRefreshToken());
String miRefreshUrl = this.urlBuilder.getRefreshUrl(AuthRefreshTokenEntity.builder()
.config(config)
.refreshToken(authToken.getRefreshToken())
.build());
return AuthResponse.builder().code(ResponseStatus.SUCCESS.getCode()).data(getToken(miRefreshUrl)).build();
}
......
......@@ -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.OschinaUrlBuilder;
import me.zhyd.oauth.url.entity.AuthAccessTokenEntity;
import me.zhyd.oauth.url.entity.AuthAuthorizeEntity;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
/**
* oschina登录
......@@ -22,13 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder;
public class AuthOschinaRequest extends BaseAuthRequest {
public AuthOschinaRequest(AuthConfig config) {
super(config, AuthSource.OSCHINA);
super(config, AuthSource.OSCHINA, new OschinaUrlBuilder());
}
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
String accessTokenUrl = UrlBuilder.getOschinaAccessTokenUrl(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")) {
......@@ -45,7 +50,9 @@ public class AuthOschinaRequest extends BaseAuthRequest {
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
HttpResponse response = HttpRequest.get(UrlBuilder.getOschinaUserInfoUrl(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")) {
throw new AuthException(object.getString("error_description"));
......@@ -71,6 +78,8 @@ public class AuthOschinaRequest extends BaseAuthRequest {
*/
@Override
public String authorize() {
return UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri(), config.getState());
return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder()
.config(config)
.build());
}
}
......@@ -81,7 +81,7 @@ public class AuthWeChatRequest extends BaseAuthRequest {
@Override
public AuthResponse refresh(AuthToken oldToken) {
String refreshTokenUrl = this.urlBuilder.getRefreshUrl(AuthRefreshTokenEntity.builder()
.clientId(config.getClientId())
.config(config)
.refreshToken(oldToken.getRefreshToken())
.build());
return AuthResponse.builder()
......
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 MiUrlBuilder extends AbstractUrlBuilder {
private static final String MI_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&response_type=code&scope=1%203%204%206&state={3}&skip_confirm=false";
private static final String MI_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}&grant_type=authorization_code";
private static final String MI_USER_INFO_PATTERN = "{0}?clientId={1}&token={2}";
private static final String MI_REFRESH_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token";
@Override
public String getAccessTokenUrl(AuthAccessTokenEntity accessTokenEntity) {
AuthConfig config = accessTokenEntity.getConfig();
return MessageFormat.format(MI_ACCESS_TOKEN_PATTERN, AuthSource.MI.accessToken(), config.getClientId(), config.getClientSecret(), config.getRedirectUri(), accessTokenEntity.getCode());
}
@Override
public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
return MessageFormat.format(MI_USER_INFO_PATTERN, AuthSource.MI.userInfo(), userInfoEntity.getClientId(), userInfoEntity.getAccessToken());
}
@Override
public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) {
AuthConfig config = authorizeEntity.getConfig();
return MessageFormat.format(MI_AUTHORIZE_PATTERN, AuthSource.MI.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState()));
}
@Override
public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) {
AuthConfig config = refreshTokenEntity.getConfig();
return MessageFormat.format(MI_REFRESH_TOKEN_PATTERN, AuthSource.MI.refresh(), config.getClientId(), config.getClientSecret(), config.getRedirectUri(), refreshTokenEntity.getRefreshToken());
}
@Override
public String getRevokeUrl(AuthRevokeEntity revokeEntity) {
return null;
}
}
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;
/**
* OSChina相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class OschinaUrlBuilder extends AbstractUrlBuilder {
private static final String OSCHINA_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}&dataType=json";
private static final String OSCHINA_USER_INFO_PATTERN = "{0}?access_token={1}&dataType=json";
private static final String OSCHINA_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(OSCHINA_ACCESS_TOKEN_PATTERN, AuthSource.OSCHINA.accessToken(), config.getClientId(),
config.getClientSecret(), accessTokenEntity.getCode(), config.getRedirectUri());
}
@Override
public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
return MessageFormat.format(OSCHINA_USER_INFO_PATTERN, AuthSource.OSCHINA.userInfo(), userInfoEntity.getAccessToken());
}
@Override
public String getAuthorizeUrl(AuthAuthorizeEntity authorizeEntity) {
AuthConfig config = authorizeEntity.getConfig();
return MessageFormat.format(OSCHINA_AUTHORIZE_PATTERN, AuthSource.OSCHINA.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;
}
}
......@@ -39,7 +39,7 @@ public class WechatUrlBuilder extends AbstractUrlBuilder {
@Override
public String getRefreshUrl(AuthRefreshTokenEntity refreshTokenEntity) {
return MessageFormat.format(WECHAT_REFRESH_TOKEN_PATTERN, AuthSource.WECHAT.refresh(), refreshTokenEntity.getClientId(), refreshTokenEntity.getRefreshToken());
return MessageFormat.format(WECHAT_REFRESH_TOKEN_PATTERN, AuthSource.WECHAT.refresh(), refreshTokenEntity.getConfig().getClientId(), refreshTokenEntity.getRefreshToken());
}
@Override
......
......@@ -2,6 +2,7 @@ package me.zhyd.oauth.url.entity;
import lombok.Builder;
import lombok.Getter;
import me.zhyd.oauth.config.AuthConfig;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
......@@ -12,6 +13,6 @@ import lombok.Getter;
@Builder
public class AuthRefreshTokenEntity {
private String clientId;
private AuthConfig config;
private String refreshToken;
}
......@@ -42,10 +42,6 @@ public class UrlBuilder {
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 OSCHINA_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}&dataType=json";
private static final String OSCHINA_USER_INFO_PATTERN = "{0}?access_token={1}&dataType=json";
private static final String OSCHINA_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
private static final String ALIPAY_AUTHORIZE_PATTERN = "{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state={3}";
......@@ -68,10 +64,6 @@ public class UrlBuilder {
private static final String MICROSOFT_USER_INFO_PATTERN = "{0}";
private static final String MICROSOFT_REFRESH_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&scope=user.read%20mail.read&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token";
private static final String MI_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&response_type=code&scope=1%203%204%206&state={3}&skip_confirm=false";
private static final String MI_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}&grant_type=authorization_code";
private static final String MI_USER_INFO_PATTERN = "{0}?clientId={1}&token={2}";
private static final String MI_REFRESH_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token";
/**
......@@ -291,41 +283,6 @@ public class UrlBuilder {
return MessageFormat.format(CODING_AUTHORIZE_PATTERN, AuthSource.CODING.authorize(), clientId, redirectUrl, getState(state));
}
/**
* 获取oschina token的接口地址
*
* @param clientId oschina 应用的App Key
* @param clientSecret oschina 应用的App Secret
* @param code oschina 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public static String getOschinaAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) {
return MessageFormat.format(OSCHINA_ACCESS_TOKEN_PATTERN, AuthSource.OSCHINA.accessToken(), clientId, clientSecret, code, redirectUri);
}
/**
* 获取oschina用户详情的接口地址
*
* @param token oschina 应用的token
* @return full url
*/
public static String getOschinaUserInfoUrl(String token) {
return MessageFormat.format(OSCHINA_USER_INFO_PATTERN, AuthSource.OSCHINA.userInfo(), token);
}
/**
* 获取oschina授权地址
*
* @param clientId oschina 应用的Client ID
* @param redirectUrl oschina 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public static String getOschinaAuthorizeUrl(String clientId, String redirectUrl, String state) {
return MessageFormat.format(OSCHINA_AUTHORIZE_PATTERN, AuthSource.OSCHINA.authorize(), clientId, redirectUrl, getState(state));
}
/**
* 获取alipay授权地址
*
......@@ -547,52 +504,4 @@ public class UrlBuilder {
return MessageFormat.format(MICROSOFT_REFRESH_TOKEN_PATTERN, AuthSource.MICROSOFT.refresh(), clientId, clientSecret, redirectUrl, refreshToken);
}
/**
* 获取小米授权地址
*
* @param clientId 小米 应用的Client ID
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public static String getMiAuthorizeUrl(String clientId, String redirectUrl, String state) {
return MessageFormat.format(MI_AUTHORIZE_PATTERN, AuthSource.MI.authorize(), clientId, redirectUrl, getState(state));
}
/**
* 获取小米 token的接口地址
*
* @param clientId 小米 应用的Client ID
* @param clientSecret 小米 应用的Client Secret
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param code 小米 授权前的code,用来换token
* @return full url
*/
public static String getMiAccessTokenUrl(String clientId, String clientSecret, String redirectUrl, String code) {
return MessageFormat.format(MI_ACCESS_TOKEN_PATTERN, AuthSource.MI.accessToken(), clientId, clientSecret, redirectUrl, code);
}
/**
* 获取小米用户详情的接口地址
*
* @param clientId 小米 应用的client_key
* @param token token
* @return full url
*/
public static String getMiUserInfoUrl(String clientId, String token) {
return MessageFormat.format(MI_USER_INFO_PATTERN, AuthSource.MI.userInfo(), clientId, token);
}
/**
* 获取小米 刷新令牌 地址
*
* @param clientId 小米 应用的client_key
* @param clientSecret 小米 应用的Client Secret
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param refreshToken 小米 应用返回的refresh_token
* @return full url
*/
public static String getMiRefreshUrl(String clientId, String clientSecret, String redirectUrl, String refreshToken) {
return MessageFormat.format(MI_REFRESH_TOKEN_PATTERN, AuthSource.MI.refresh(), clientId, clientSecret, redirectUrl, refreshToken);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册