mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2025-12-06 16:58:24 +08:00
integrate stack overflow
This commit is contained in:
parent
44d4323fb2
commit
698c922b6b
10
example.md
10
example.md
@ -84,6 +84,8 @@ _注:非全部平台,部分平台可能不存在图例_
|
|||||||
|
|
||||||
暂无
|
暂无
|
||||||
|
|
||||||
|
_请知悉:经咨询CSDN官方客服得知,CSDN的授权开放平台已经下线。如果以前申请过的应用,可以继续使用,但是不再支持申请新的应用。so, 本项目中的CSDN登录只能针对少部分用户使用了_
|
||||||
|
|
||||||
#### 授权Pinterest
|
#### 授权Pinterest
|
||||||
|
|
||||||

|

|
||||||
@ -92,4 +94,10 @@ _注:非全部平台,部分平台可能不存在图例_
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
_请知悉:经咨询CSDN官方客服得知,CSDN的授权开放平台已经下线。如果以前申请过的应用,可以继续使用,但是不再支持申请新的应用。so, 本项目中的CSDN登录只能针对少部分用户使用了_
|
#### 授权Stack Overflow
|
||||||
|
|
||||||
|
暂无
|
||||||
|
|
||||||
|
#### 授权Twitter
|
||||||
|
|
||||||
|
暂无
|
||||||
|
|||||||
@ -51,4 +51,11 @@ public class AuthConfig {
|
|||||||
* 1.8.0版本新增参数
|
* 1.8.0版本新增参数
|
||||||
*/
|
*/
|
||||||
private String state;
|
private String state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stack Overflow Key
|
||||||
|
* <p>
|
||||||
|
* 1.9.0版本新增参数
|
||||||
|
*/
|
||||||
|
private String stackOverflowKey;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -489,6 +489,26 @@ public enum AuthSource {
|
|||||||
public String userInfo() {
|
public String userInfo() {
|
||||||
return "https://api.pinterest.com/v1/me";
|
return "https://api.pinterest.com/v1/me";
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stack Overflow
|
||||||
|
*/
|
||||||
|
STACK_OVERFLOW {
|
||||||
|
@Override
|
||||||
|
public String authorize() {
|
||||||
|
return "https://stackoverflow.com/oauth";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String accessToken() {
|
||||||
|
return "https://stackoverflow.com/oauth/access_token/json";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String userInfo() {
|
||||||
|
return "https://api.stackexchange.com/2.2/me";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -0,0 +1,78 @@
|
|||||||
|
package me.zhyd.oauth.request;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import me.zhyd.oauth.config.AuthConfig;
|
||||||
|
import me.zhyd.oauth.exception.AuthException;
|
||||||
|
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.url.AuthStackOverflowUrlBuilder;
|
||||||
|
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static me.zhyd.oauth.config.AuthSource.STACK_OVERFLOW;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stack Overflow登录
|
||||||
|
*
|
||||||
|
* @author hongwei.peng (pengisgood(at)gmail(dot)com)
|
||||||
|
* @version 1.9.0
|
||||||
|
* @since 1.9.0
|
||||||
|
*/
|
||||||
|
public class AuthStackOverflowRequest extends AuthDefaultRequest {
|
||||||
|
|
||||||
|
public AuthStackOverflowRequest(AuthConfig config) {
|
||||||
|
super(config, STACK_OVERFLOW, new AuthStackOverflowUrlBuilder());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
||||||
|
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
|
||||||
|
HttpResponse response = HttpRequest.post(accessTokenUrl)
|
||||||
|
.contentType("application/x-www-form-urlencoded")
|
||||||
|
.form(buildBody(accessTokenUrl))
|
||||||
|
.execute();
|
||||||
|
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||||
|
if (!response.isOk()) {
|
||||||
|
throw new AuthException("Unable to get token from Stack Overflow using code [" + authCallback.getCode() + "]: " + accessTokenObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return AuthToken.builder()
|
||||||
|
.accessToken(accessTokenObject.getString("access_token"))
|
||||||
|
.expireIn(accessTokenObject.getIntValue("expires"))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||||
|
String accessToken = authToken.getAccessToken();
|
||||||
|
HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
|
||||||
|
.accessToken(accessToken)
|
||||||
|
.build())).execute();
|
||||||
|
JSONObject userObj = JSONObject.parseObject(response.body()).getJSONArray("items").getJSONObject(0);
|
||||||
|
|
||||||
|
return AuthUser.builder()
|
||||||
|
.uuid(userObj.getString("user_id"))
|
||||||
|
.avatar(userObj.getString("profile_image"))
|
||||||
|
.location(userObj.getString("location"))
|
||||||
|
.nickname(userObj.getString("display_name"))
|
||||||
|
.blog(userObj.getString("website_url"))
|
||||||
|
.gender(AuthUserGender.UNKNOWN)
|
||||||
|
.token(authToken)
|
||||||
|
.source(STACK_OVERFLOW)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> buildBody(String accessTokenUrl) {
|
||||||
|
Map<String, Object> paramMap = new HashMap<>();
|
||||||
|
HttpUtil.decodeParamMap(accessTokenUrl, "UTF-8").forEach(paramMap::put);
|
||||||
|
return paramMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package me.zhyd.oauth.url;
|
||||||
|
|
||||||
|
import me.zhyd.oauth.exception.AuthException;
|
||||||
|
import me.zhyd.oauth.model.AuthResponseStatus;
|
||||||
|
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import static me.zhyd.oauth.config.AuthSource.STACK_OVERFLOW;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stack Overflow相关的URL构建类
|
||||||
|
*
|
||||||
|
* @author hongwei.peng (pengisgood(at)gmail(dot)com)
|
||||||
|
* @version 1.9.0
|
||||||
|
* @since 1.9.0
|
||||||
|
*/
|
||||||
|
public class AuthStackOverflowUrlBuilder extends AuthDefaultUrlBuilder {
|
||||||
|
|
||||||
|
private static final String SO_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}";
|
||||||
|
private static final String SO_USER_INFO_PATTERN = "{0}?access_token={1}&site=stackoverflow&key={2}";
|
||||||
|
private static final String SO_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenUrl(String code) {
|
||||||
|
return MessageFormat.format(SO_ACCESS_TOKEN_PATTERN, STACK_OVERFLOW.accessToken(), config.getClientId(), config.getClientSecret(), config.getRedirectUri(), code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
|
||||||
|
return MessageFormat.format(SO_USER_INFO_PATTERN, STACK_OVERFLOW.userInfo(), userInfoEntity.getAccessToken(), config.getStackOverflowKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizeUrl() {
|
||||||
|
return MessageFormat.format(SO_AUTHORIZE_PATTERN, STACK_OVERFLOW.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRefreshUrl(String refreshToken) {
|
||||||
|
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRevokeUrl(String accessToken) {
|
||||||
|
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user