2019-02-20 13:14:14 +08:00
|
|
|
package me.zhyd.oauth.request;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2019-05-18 15:31:17 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2019-02-20 13:14:14 +08:00
|
|
|
import me.zhyd.oauth.config.AuthConfig;
|
|
|
|
|
import me.zhyd.oauth.exception.AuthException;
|
|
|
|
|
import me.zhyd.oauth.model.*;
|
|
|
|
|
import me.zhyd.oauth.utils.UrlBuilder;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-05-17 10:19:52 +08:00
|
|
|
* 百度账号登录
|
|
|
|
|
*
|
2019-02-20 13:14:14 +08:00
|
|
|
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
|
|
|
|
* @version 1.0
|
|
|
|
|
* @since 1.8
|
|
|
|
|
*/
|
|
|
|
|
public class AuthBaiduRequest extends BaseAuthRequest {
|
|
|
|
|
|
|
|
|
|
public AuthBaiduRequest(AuthConfig config) {
|
|
|
|
|
super(config, AuthSource.BAIDU);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-17 18:37:57 +08:00
|
|
|
protected AuthToken getAccessToken(String code) {
|
2019-02-20 13:14:14 +08:00
|
|
|
String accessTokenUrl = UrlBuilder.getBaiduAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
|
|
|
|
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
|
|
|
|
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
|
|
|
|
AuthBaiduErrorCode errorCode = AuthBaiduErrorCode.getErrorCode(accessTokenObject.getString("error"));
|
|
|
|
|
if (!AuthBaiduErrorCode.OK.equals(errorCode)) {
|
|
|
|
|
throw new AuthException(errorCode.getDesc());
|
|
|
|
|
}
|
2019-05-17 18:37:57 +08:00
|
|
|
return AuthToken.builder()
|
|
|
|
|
.accessToken(accessTokenObject.getString("access_token"))
|
|
|
|
|
.build();
|
2019-02-20 13:14:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-17 18:37:57 +08:00
|
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
|
|
|
String accessToken = authToken.getAccessToken();
|
2019-02-20 13:14:14 +08:00
|
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduUserInfoUrl(accessToken)).execute();
|
|
|
|
|
String userInfo = response.body();
|
|
|
|
|
JSONObject object = JSONObject.parseObject(userInfo);
|
|
|
|
|
AuthBaiduErrorCode errorCode = AuthBaiduErrorCode.getErrorCode(object.getString("error"));
|
|
|
|
|
if (!AuthBaiduErrorCode.OK.equals(errorCode)) {
|
|
|
|
|
throw new AuthException(errorCode.getDesc());
|
|
|
|
|
}
|
|
|
|
|
return AuthUser.builder()
|
2019-05-23 19:29:33 +08:00
|
|
|
.uuid(object.getString("userid"))
|
2019-02-20 13:14:14 +08:00
|
|
|
.username(object.getString("username"))
|
|
|
|
|
.nickname(object.getString("username"))
|
|
|
|
|
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
2019-05-18 15:31:17 +08:00
|
|
|
.token(authToken)
|
2019-02-20 13:14:14 +08:00
|
|
|
.source(AuthSource.BAIDU)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-17 18:37:57 +08:00
|
|
|
public AuthResponse revoke(AuthToken authToken) {
|
|
|
|
|
String accessToken = authToken.getAccessToken();
|
2019-02-20 13:14:14 +08:00
|
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduRevokeUrl(accessToken)).execute();
|
|
|
|
|
String userInfo = response.body();
|
|
|
|
|
JSONObject object = JSONObject.parseObject(userInfo);
|
2019-05-18 15:31:17 +08:00
|
|
|
if(object.containsKey("error_code")) {
|
|
|
|
|
return AuthResponse.builder()
|
|
|
|
|
.code(ResponseStatus.FAILURE.getCode())
|
|
|
|
|
.msg(object.getString("error_msg"))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2019-02-20 13:14:14 +08:00
|
|
|
ResponseStatus status = object.getIntValue("result") == 1 ? ResponseStatus.SUCCESS : ResponseStatus.FAILURE;
|
|
|
|
|
return AuthResponse.builder().code(status.getCode()).msg(status.getMsg()).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|