mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2026-01-07 19:31:48 +08:00
✨ 支持腾讯云开发者平台(Coding升级变为了腾讯云开发者平台)
This commit is contained in:
parent
299d90b8e7
commit
579c6ecefd
@ -27,6 +27,7 @@
|
||||
<td align="center" width="200"><a href="https://developer.baidu.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.csdn.net/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://coding.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://dev.tencent.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.oschina.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://connect.qq.com/devuser.html#/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="30"></a></td>
|
||||
@ -41,6 +42,7 @@
|
||||
<td align="center" width="200"><a href="#授权百度">百度</a></td>
|
||||
<td align="center" width="200"><a href="#授权csdn">CSDN</a></td>
|
||||
<td align="center" width="200"><a href="#授权coding">Coding</a></td>
|
||||
<td align="center" width="200"><a href="#授权腾讯云开发者平台" title="coding升级后就变成腾讯云开发者平台了">腾讯云开发者平台</a></td>
|
||||
<td align="center" width="200"><a href="#授权oschina">OSChina</a></td>
|
||||
<td align="center" width="200"><a href="#授权qq">QQ</a></td>
|
||||
<td align="center" width="200"><a href="#授权微信">微信</a></td>
|
||||
@ -89,6 +91,7 @@ authRequest.login("code");
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="20"> | [AuthBaiduRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java) | <a href="https://developer.baidu.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"> | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="25"> | [AuthCodingRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java) | <a href="https://open.coding.net/references/oauth/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="25"> | [AuthTencentCloudRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java) | <a href="https://dev.tencent.com/help/doc/faq/b4e5b7aee786/oauth" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="20"> | [AuthOschinaRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java) | <a href="https://www.oschina.net/openapi/docs/openapi_user" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"> | AuthQqRequest | <a href="https://connect.qq.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"> | AuthWechatRequest | 待续 |
|
||||
@ -123,6 +126,10 @@ authRequest.login("code");
|
||||
|
||||
待续
|
||||
|
||||
#### 授权腾讯云开发者平台
|
||||
|
||||
待续
|
||||
|
||||
#### 授权oschina
|
||||
|
||||
待续
|
||||
|
||||
@ -116,6 +116,21 @@ public class ApiUrlConst {
|
||||
*/
|
||||
public static final String CODING_AUTHORIZE_URL = "https://coding.net/oauth_authorize.html";
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台 access_token的地址(coding升级后就变成腾讯云开发者平台了)
|
||||
*/
|
||||
public static final String TENCENT_ACCESS_TOKEN_URL = "https://dev.tencent.com/api/oauth/access_token";
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台用户信息的地址(coding升级后就变成腾讯云开发者平台了)
|
||||
*/
|
||||
public static final String TENCENT_USER_INFO_URL = "https://dev.tencent.com/api/account/current_user";
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台授权地址(coding升级后就变成腾讯云开发者平台了)
|
||||
*/
|
||||
public static final String TENCENT_AUTHORIZE_URL = "https://dev.tencent.com/oauth_authorize.html";
|
||||
|
||||
/**
|
||||
* 获取oschina access_token的地址
|
||||
*/
|
||||
|
||||
@ -16,6 +16,7 @@ public enum AuthSource {
|
||||
CSDN,
|
||||
CODING,
|
||||
OSCHINA,
|
||||
TENCEN_CLOUD,
|
||||
|
||||
QQ,
|
||||
WECHAT,
|
||||
|
||||
@ -26,7 +26,7 @@ public class AuthCodingRequest extends BaseAuthRequest {
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getCodingAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (accessTokenObject.getIntValue("code") != 0) {
|
||||
throw new AuthException("Unable to get token from coding using code [" + code + "]");
|
||||
@ -41,9 +41,10 @@ public class AuthCodingRequest extends BaseAuthRequest {
|
||||
if (object.getIntValue("code") != 0) {
|
||||
throw new AuthException(object.getString("msg"));
|
||||
}
|
||||
object = object.getJSONObject("data");
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("name"))
|
||||
.avatar(object.getString("avatar"))
|
||||
.avatar("https://coding.net/" + object.getString("avatar"))
|
||||
.blog("https://coding.net/" + object.getString("path"))
|
||||
.nickname(object.getString("name"))
|
||||
.company(object.getString("company"))
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
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.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/23 15:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthTencentCloudRequest extends BaseAuthRequest {
|
||||
|
||||
public AuthTencentCloudRequest(AuthConfig config) {
|
||||
super(config, AuthSource.TENCEN_CLOUD);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getTencentCloudAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
|
||||
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("code") != 0) {
|
||||
throw new AuthException("Unable to get token from tencent cloud using code [" + code + "]: " + object.get("msg"));
|
||||
}
|
||||
return object.getString("access_token");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getTencentCloudUserInfoUrl(accessToken)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("code") != 0) {
|
||||
throw new AuthException(object.getString("msg"));
|
||||
}
|
||||
object = object.getJSONObject("data");
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("name"))
|
||||
.avatar("https://dev.tencent.com/" + object.getString("avatar"))
|
||||
.blog("https://dev.tencent.com/" + object.getString("path"))
|
||||
.nickname(object.getString("name"))
|
||||
.company(object.getString("company"))
|
||||
.location(object.getString("location"))
|
||||
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
||||
.email(object.getString("email"))
|
||||
.remark(object.getString("slogan"))
|
||||
.accessToken(accessToken)
|
||||
.source(AuthSource.TENCEN_CLOUD)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@ -73,6 +73,12 @@ public abstract class BaseAuthRequest implements AuthRequest {
|
||||
case CODING:
|
||||
authorizeUrl = UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case TENCEN_CLOUD:
|
||||
authorizeUrl = UrlBuilder.getTencentCloudAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case OSCHINA:
|
||||
authorizeUrl = UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case QQ:
|
||||
break;
|
||||
case WECHAT:
|
||||
|
||||
@ -41,7 +41,11 @@ public class UrlBuilder {
|
||||
|
||||
private static final String CODING_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}";
|
||||
private static final String CODING_USER_INFO_PATTERN = "{0}?access_token={1}";
|
||||
private static final String CODING_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}";
|
||||
private static final String CODING_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&scope=user";
|
||||
|
||||
private static final String TENCENT_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}";
|
||||
private static final String TENCENT_USER_INFO_PATTERN = "{0}?access_token={1}";
|
||||
private static final String TENCENT_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&scope=user";
|
||||
|
||||
private static final String OSCHINA_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}&dataType=json";
|
||||
private static final String OSCHINA_USER_INFO_PATTERN = "{0}?access_token={1}&dataType=json";
|
||||
@ -283,6 +287,39 @@ public class UrlBuilder {
|
||||
return MessageFormat.format(CODING_AUTHORIZE_PATTERN, ApiUrlConst.CODING_AUTHORIZE_URL, clientId, redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台 token的接口地址
|
||||
*
|
||||
* @param clientId coding应用的App Key
|
||||
* @param clientSecret coding应用的App Secret
|
||||
* @param code coding授权前的code,用来换token
|
||||
* @return full url
|
||||
*/
|
||||
public static String getTencentCloudAccessTokenUrl(String clientId, String clientSecret, String code) {
|
||||
return MessageFormat.format(TENCENT_ACCESS_TOKEN_PATTERN, ApiUrlConst.TENCENT_ACCESS_TOKEN_URL, clientId, clientSecret, code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台用户详情的接口地址
|
||||
*
|
||||
* @param token coding 应用的token
|
||||
* @return full url
|
||||
*/
|
||||
public static String getTencentCloudUserInfoUrl(String token) {
|
||||
return MessageFormat.format(TENCENT_USER_INFO_PATTERN, ApiUrlConst.TENCENT_USER_INFO_URL, token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取腾讯云开发者平台授权地址
|
||||
*
|
||||
* @param clientId coding 应用的Client ID
|
||||
* @param redirectUrl coding 应用授权成功后的回调地址
|
||||
* @return full url
|
||||
*/
|
||||
public static String getTencentCloudAuthorizeUrl(String clientId, String redirectUrl) {
|
||||
return MessageFormat.format(TENCENT_AUTHORIZE_PATTERN, ApiUrlConst.TENCENT_AUTHORIZE_URL, clientId, redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取oschina token的接口地址
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user