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

集成teambition

上级 59b5ba5a
......@@ -442,7 +442,7 @@ public enum AuthSource {
@Override
public String userInfo() {
return "https://account.teambition.com/users/me";
return "https://api.teambition.com/users/me";
}
};
......
package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
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.url.AuthTeambitionUrlBuilder;
/**
* 微信登录
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class AuthTeambitionRequest extends AuthDefaultRequest {
public AuthTeambitionRequest(AuthConfig config) {
super(config, AuthSource.TEAMBITION, new AuthTeambitionUrlBuilder());
}
/**
* @param authCallback 回调返回的参数
* @return 所有信息
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
HttpResponse response = HttpRequest.post(accessTokenUrl)
.form("client_id", config.getClientId())
.form("client_secret", config.getClientSecret())
.form("code", authCallback.getCode())
.form("grant_type", "code")
.execute();
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
this.checkResponse(accessTokenObject);
return AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
.refreshToken(accessTokenObject.getString("refresh_token"))
.build();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(null))
.header("Authorization", "OAuth2 " + accessToken)
.execute();
JSONObject object = JSONObject.parseObject(response.body());
this.checkResponse(object);
authToken.setUid(object.getString("_id"));
return AuthUser.builder()
.uuid(object.getString("_id"))
.username(object.getString("name"))
.nickname(object.getString("name"))
.avatar(object.getString("avatarUrl"))
.blog(object.getString("website"))
.location(object.getString("location"))
.email(object.getString("email"))
.gender(AuthUserGender.UNKNOW)
.token(authToken)
.source(AuthSource.TEAMBITION)
.build();
}
@Override
public AuthResponse refresh(AuthToken oldToken) {
String uid = oldToken.getUid();
String refreshToken = oldToken.getRefreshToken();
String refreshTokenUrl = this.urlBuilder.getRefreshUrl(refreshToken);
HttpResponse response = HttpRequest.post(refreshTokenUrl)
.form("_userId", uid)
.form("refresh_token", refreshToken)
.execute();
JSONObject refreshTokenObject = JSONObject.parseObject(response.body());
this.checkResponse(refreshTokenObject);
return AuthResponse.builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(refreshTokenObject.getString("access_token"))
.refreshToken(refreshTokenObject.getString("refresh_token"))
.build())
.build();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private void checkResponse(JSONObject object) {
if ((object.containsKey("message") && object.containsKey("name"))) {
throw new AuthException(object.getString("name") + ", " + object.getString("message"));
}
}
}
package me.zhyd.oauth.url;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.text.MessageFormat;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/7/16 22:22
* @since 1.8
*/
public class AuthTeambitionUrlBuilder extends AuthDefaultUrlBuilder {
private static final String TEAMBITION_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code";
@Override
public String getAccessTokenUrl(String code) {
return AuthSource.TEAMBITION.accessToken();
}
@Override
public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
return AuthSource.TEAMBITION.userInfo();
}
@Override
public String getAuthorizeUrl() {
return MessageFormat.format(TEAMBITION_AUTHORIZE_PATTERN, AuthSource.TEAMBITION.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState()));
}
@Override
public String getRefreshUrl(String refreshToken) {
return AuthSource.TEAMBITION.refresh();
}
@Override
public String getRevokeUrl(String accessToken) {
return null;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册