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;
|
|
|
|
|
import me.zhyd.oauth.config.AuthConfig;
|
2019-06-20 10:10:18 +08:00
|
|
|
import me.zhyd.oauth.config.AuthSource;
|
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-16 16:12:56 +08:00
|
|
|
import me.zhyd.oauth.url.MiUrlBuilder;
|
2019-07-16 15:50:44 +08:00
|
|
|
import me.zhyd.oauth.url.entity.AuthAccessTokenEntity;
|
|
|
|
|
import me.zhyd.oauth.url.entity.AuthRefreshTokenEntity;
|
|
|
|
|
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 小米登录
|
|
|
|
|
*
|
|
|
|
|
* @author yangkai.shen (https://xkcoding.com)
|
|
|
|
|
* @version 1.5
|
|
|
|
|
* @since 1.5
|
|
|
|
|
*/
|
|
|
|
|
public class AuthMiRequest extends BaseAuthRequest {
|
|
|
|
|
private static final String PREFIX = "&&&START&&&";
|
|
|
|
|
|
|
|
|
|
public AuthMiRequest(AuthConfig config) {
|
2019-07-16 16:12:56 +08:00
|
|
|
super(config, AuthSource.MI, new MiUrlBuilder());
|
2019-05-28 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-27 19:39:21 +08:00
|
|
|
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
2019-07-16 15:50:44 +08:00
|
|
|
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(AuthAccessTokenEntity.builder()
|
|
|
|
|
.config(config)
|
|
|
|
|
.code(authCallback.getCode())
|
|
|
|
|
.build());
|
2019-05-28 19:26:10 +08:00
|
|
|
return getToken(accessTokenUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06-28 22:58:34 +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"))
|
2019-05-28 19:26:10 +08:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
|
|
|
// 获取用户信息
|
2019-07-16 15:50:44 +08:00
|
|
|
HttpResponse userResponse = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
|
|
|
|
|
.clientId(config.getClientId())
|
|
|
|
|
.accessToken(authToken.getAccessToken())
|
|
|
|
|
.build()))
|
2019-05-28 19:26:10 +08:00
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
.uuid(authToken.getOpenId())
|
|
|
|
|
.username(user.getString("miliaoNick"))
|
|
|
|
|
.nickname(user.getString("miliaoNick"))
|
|
|
|
|
.avatar(user.getString("miliaoIcon"))
|
|
|
|
|
.email(user.getString("mail"))
|
2019-06-18 19:21:05 +08:00
|
|
|
.gender(AuthUserGender.UNKNOW)
|
2019-05-28 19:26:10 +08:00
|
|
|
.token(authToken)
|
|
|
|
|
.source(AuthSource.MI)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 获取用户邮箱手机号等信息
|
|
|
|
|
String emailPhoneUrl = MessageFormat.format("{0}?clientId={1}&token={2}", "https://open.account.xiaomi.com/user/phoneAndEmail", config
|
|
|
|
|
.getClientId(), authToken.getAccessToken());
|
|
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新access token (续期)
|
|
|
|
|
*
|
|
|
|
|
* @param authToken 登录成功后返回的Token信息
|
|
|
|
|
* @return AuthResponse
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public AuthResponse refresh(AuthToken authToken) {
|
2019-07-16 15:50:44 +08:00
|
|
|
String miRefreshUrl = this.urlBuilder.getRefreshUrl(AuthRefreshTokenEntity.builder()
|
|
|
|
|
.config(config)
|
|
|
|
|
.refreshToken(authToken.getRefreshToken())
|
|
|
|
|
.build());
|
2019-05-28 19:26:10 +08:00
|
|
|
|
|
|
|
|
return AuthResponse.builder().code(ResponseStatus.SUCCESS.getCode()).data(getToken(miRefreshUrl)).build();
|
|
|
|
|
}
|
|
|
|
|
}
|