2019-05-28 19:26:10 +08:00
|
|
|
package me.zhyd.oauth.request;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2019-07-16 19:49:18 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2019-05-28 19:26:10 +08:00
|
|
|
import me.zhyd.oauth.config.AuthConfig;
|
2019-06-20 10:10:18 +08:00
|
|
|
import me.zhyd.oauth.config.AuthSource;
|
2019-07-19 14:26:42 +08:00
|
|
|
import me.zhyd.oauth.enums.AuthUserGender;
|
2019-05-28 19:26:10 +08:00
|
|
|
import me.zhyd.oauth.exception.AuthException;
|
2019-06-27 19:39:21 +08:00
|
|
|
import me.zhyd.oauth.model.*;
|
2019-07-18 20:37:41 +08:00
|
|
|
import me.zhyd.oauth.utils.UrlBuilder;
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 小米登录
|
|
|
|
|
*
|
|
|
|
|
* @author yangkai.shen (https://xkcoding.com)
|
|
|
|
|
* @version 1.5
|
|
|
|
|
* @since 1.5
|
|
|
|
|
*/
|
2019-07-16 19:49:18 +08:00
|
|
|
@Slf4j
|
2019-07-16 17:59:36 +08:00
|
|
|
public class AuthMiRequest extends AuthDefaultRequest {
|
2019-05-28 19:26:10 +08:00
|
|
|
private static final String PREFIX = "&&&START&&&";
|
|
|
|
|
|
|
|
|
|
public AuthMiRequest(AuthConfig config) {
|
2019-07-18 20:37:41 +08:00
|
|
|
super(config, AuthSource.MI);
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-27 19:39:21 +08:00
|
|
|
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
2019-07-18 20:37:41 +08:00
|
|
|
return getToken(accessTokenUrl(authCallback.getCode()));
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AuthToken getToken(String accessTokenUrl) {
|
|
|
|
|
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
|
|
|
|
String jsonStr = StrUtil.replace(response.body(), PREFIX, StrUtil.EMPTY);
|
2019-06-28 22:58:34 +08:00
|
|
|
JSONObject accessTokenObject = JSONObject.parseObject(jsonStr);
|
2019-05-28 19:26:10 +08:00
|
|
|
|
2019-06-28 22:58:34 +08:00
|
|
|
if (accessTokenObject.containsKey("error")) {
|
|
|
|
|
throw new AuthException(accessTokenObject.getString("error_description"));
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return AuthToken.builder()
|
2019-07-18 20:37:41 +08:00
|
|
|
.accessToken(accessTokenObject.getString("access_token"))
|
|
|
|
|
.expireIn(accessTokenObject.getIntValue("expires_in"))
|
|
|
|
|
.scope(accessTokenObject.getString("scope"))
|
|
|
|
|
.tokenType(accessTokenObject.getString("token_type"))
|
|
|
|
|
.refreshToken(accessTokenObject.getString("refresh_token"))
|
|
|
|
|
.openId(accessTokenObject.getString("openId"))
|
|
|
|
|
.macAlgorithm(accessTokenObject.getString("mac_algorithm"))
|
|
|
|
|
.macKey(accessTokenObject.getString("mac_key"))
|
|
|
|
|
.build();
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
|
|
|
// 获取用户信息
|
2019-07-18 20:37:41 +08:00
|
|
|
HttpResponse userResponse = doGetUserInfo(authToken);
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
JSONObject userProfile = JSONObject.parseObject(userResponse.body());
|
2019-06-21 15:36:41 +08:00
|
|
|
if ("error".equalsIgnoreCase(userProfile.getString("result"))) {
|
2019-05-28 19:26:10 +08:00
|
|
|
throw new AuthException(userProfile.getString("description"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject user = userProfile.getJSONObject("data");
|
|
|
|
|
|
|
|
|
|
AuthUser authUser = AuthUser.builder()
|
2019-07-18 20:37:41 +08:00
|
|
|
.uuid(authToken.getOpenId())
|
|
|
|
|
.username(user.getString("miliaoNick"))
|
|
|
|
|
.nickname(user.getString("miliaoNick"))
|
|
|
|
|
.avatar(user.getString("miliaoIcon"))
|
|
|
|
|
.email(user.getString("mail"))
|
|
|
|
|
.gender(AuthUserGender.UNKNOWN)
|
|
|
|
|
.token(authToken)
|
2019-07-19 22:00:07 +08:00
|
|
|
.source(source)
|
2019-07-18 20:37:41 +08:00
|
|
|
.build();
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
// 获取用户邮箱手机号等信息
|
|
|
|
|
String emailPhoneUrl = MessageFormat.format("{0}?clientId={1}&token={2}", "https://open.account.xiaomi.com/user/phoneAndEmail", config
|
2019-07-18 20:37:41 +08:00
|
|
|
.getClientId(), authToken.getAccessToken());
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
HttpResponse emailResponse = HttpRequest.get(emailPhoneUrl).execute();
|
|
|
|
|
JSONObject userEmailPhone = JSONObject.parseObject(emailResponse.body());
|
2019-06-21 15:36:41 +08:00
|
|
|
if (!"error".equalsIgnoreCase(userEmailPhone.getString("result"))) {
|
2019-05-28 19:26:10 +08:00
|
|
|
JSONObject emailPhone = userEmailPhone.getJSONObject("data");
|
|
|
|
|
authUser.setEmail(emailPhone.getString("email"));
|
2019-07-16 19:49:18 +08:00
|
|
|
} else {
|
|
|
|
|
log.warn("小米开发平台暂时不对外开放用户手机及邮箱信息的获取");
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新access token (续期)
|
|
|
|
|
*
|
|
|
|
|
* @param authToken 登录成功后返回的Token信息
|
|
|
|
|
* @return AuthResponse
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public AuthResponse refresh(AuthToken authToken) {
|
2019-07-18 20:37:41 +08:00
|
|
|
return AuthResponse.builder()
|
|
|
|
|
.code(AuthResponseStatus.SUCCESS.getCode())
|
|
|
|
|
.data(getToken(refreshTokenUrl(authToken.getRefreshToken())))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2019-05-28 19:26:10 +08:00
|
|
|
|
2019-07-18 20:37:41 +08:00
|
|
|
/**
|
|
|
|
|
* 返回认证url,可自行跳转页面
|
|
|
|
|
*
|
|
|
|
|
* @return 返回授权地址
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String authorize() {
|
|
|
|
|
return UrlBuilder.fromBaseUrl(source.authorize())
|
|
|
|
|
.queryParam("response_type", "code")
|
|
|
|
|
.queryParam("client_id", config.getClientId())
|
|
|
|
|
.queryParam("redirect_uri", config.getRedirectUri())
|
|
|
|
|
.queryParam("state", getRealState(config.getState()))
|
|
|
|
|
.queryParam("scope", "user/profile%20user/openIdV2%20user/phoneAndEmail")
|
|
|
|
|
.queryParam("skip_confirm", "false")
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回获取userInfo的url
|
|
|
|
|
*
|
2019-07-20 17:21:21 +08:00
|
|
|
* @param authToken 用户授权后的token
|
2019-07-18 20:37:41 +08:00
|
|
|
* @return 返回获取userInfo的url
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected String userInfoUrl(AuthToken authToken) {
|
|
|
|
|
return UrlBuilder.fromBaseUrl(source.userInfo())
|
|
|
|
|
.queryParam("clientId", config.getClientId())
|
|
|
|
|
.queryParam("token", authToken.getAccessToken())
|
|
|
|
|
.build();
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
}
|