Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
鲸落万物生...
JustAuth
提交
3a04098e
J
JustAuth
项目概览
鲸落万物生...
/
JustAuth
与 Fork 源项目一致
Fork自
justauth / JustAuth
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JustAuth
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3a04098e
编写于
7月 19, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
⚡
优化代码
上级
81691a34
变更
26
隐藏空白更改
内联
并排
Showing
26 changed file
with
394 addition
and
187 deletion
+394
-187
src/main/java/me/zhyd/oauth/config/AuthSource.java
src/main/java/me/zhyd/oauth/config/AuthSource.java
+5
-0
src/main/java/me/zhyd/oauth/exception/AuthException.java
src/main/java/me/zhyd/oauth/exception/AuthException.java
+4
-0
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
+2
-2
src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java
src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java
+22
-24
src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java
src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java
+13
-6
src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java
src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java
+14
-7
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
+3
-0
src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
+13
-18
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
+28
-19
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
+14
-4
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
+19
-13
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
+14
-7
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
+51
-20
src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
...main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
+9
-3
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
+14
-7
src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
...main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
+18
-7
src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
+24
-13
src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java
src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java
+1
-1
src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
.../java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
+16
-5
src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
...n/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
+15
-8
src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
+17
-10
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
+5
-5
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
+2
-3
src/test/java/me/zhyd/oauth/utils/CustomTest.java
src/test/java/me/zhyd/oauth/utils/CustomTest.java
+63
-0
src/test/java/me/zhyd/oauth/utils/UrlBuilderTest.java
src/test/java/me/zhyd/oauth/utils/UrlBuilderTest.java
+6
-5
update.md
update.md
+2
-0
未找到文件。
src/main/java/me/zhyd/oauth/config/AuthSource.java
浏览文件 @
3a04098e
...
...
@@ -229,6 +229,11 @@ public enum AuthSource {
public
String
userInfo
()
{
return
"https://graph.qq.com/user/get_user_info"
;
}
@Override
public
String
refresh
()
{
return
"https://graph.qq.com/oauth2.0/token"
;
}
},
/**
* 微信
...
...
src/main/java/me/zhyd/oauth/exception/AuthException.java
浏览文件 @
3a04098e
...
...
@@ -30,6 +30,10 @@ public class AuthException extends RuntimeException {
super
(
message
,
cause
);
}
public
AuthException
(
Throwable
cause
)
{
super
(
cause
);
}
public
int
getErrorCode
()
{
return
errorCode
;
}
...
...
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
浏览文件 @
3a04098e
...
...
@@ -9,11 +9,11 @@ import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import
com.alipay.api.response.AlipayUserInfoShareResponse
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
...
...
@@ -43,7 +43,7 @@ public class AuthAlipayRequest extends AuthDefaultRequest {
try
{
response
=
this
.
alipayClient
.
execute
(
request
);
}
catch
(
Exception
e
)
{
throw
new
AuthException
(
"Unable to get token from alipay using code ["
+
authCallback
.
getAuth_code
()
+
"]"
,
e
);
throw
new
AuthException
(
e
);
}
if
(!
response
.
isSuccess
())
{
throw
new
AuthException
(
response
.
getSubMsg
());
...
...
src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java
浏览文件 @
3a04098e
...
...
@@ -26,14 +26,7 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
refreshToken
(
accessTokenObject
.
getString
(
"refresh_token"
))
.
scope
(
accessTokenObject
.
getString
(
"scope"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
.
build
();
return
getAuthToken
(
response
);
}
@Override
...
...
@@ -58,12 +51,7 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
public
AuthResponse
revoke
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetRevoke
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error_code"
))
{
return
AuthResponse
.
builder
()
.
code
(
AuthResponseStatus
.
FAILURE
.
getCode
())
.
msg
(
object
.
getString
(
"error_msg"
))
.
build
();
}
this
.
checkResponse
(
object
);
// 返回1表示取消授权成功,否则失败
AuthResponseStatus
status
=
object
.
getIntValue
(
"result"
)
==
1
?
AuthResponseStatus
.
SUCCESS
:
AuthResponseStatus
.
FAILURE
;
return
AuthResponse
.
builder
().
code
(
status
.
getCode
()).
msg
(
status
.
getMsg
()).
build
();
...
...
@@ -78,16 +66,9 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
.
queryParam
(
"client_secret"
,
this
.
config
.
getClientSecret
())
.
build
();
HttpResponse
response
=
HttpRequest
.
get
(
refreshUrl
).
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
object
);
return
AuthResponse
.
builder
()
.
code
(
AuthResponseStatus
.
SUCCESS
.
getCode
())
.
data
(
AuthToken
.
builder
()
.
accessToken
(
object
.
getString
(
"access_token"
))
.
refreshToken
(
object
.
getString
(
"refresh_token"
))
.
scope
(
object
.
getString
(
"scope"
))
.
expireIn
(
object
.
getIntValue
(
"expires_in"
))
.
build
())
.
data
(
this
.
getAuthToken
(
response
))
.
build
();
}
...
...
@@ -107,9 +88,26 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
if
(
object
.
containsKey
(
"error"
)
||
object
.
containsKey
(
"error_code"
))
{
String
msg
=
object
.
containsKey
(
"error_description"
)
?
object
.
getString
(
"error_description"
)
:
object
.
getString
(
"error_msg"
);
throw
new
AuthException
(
msg
);
}
}
private
AuthToken
getAuthToken
(
HttpResponse
response
)
{
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
refreshToken
(
accessTokenObject
.
getString
(
"refresh_token"
))
.
scope
(
accessTokenObject
.
getString
(
"scope"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java
浏览文件 @
3a04098e
...
...
@@ -28,9 +28,7 @@ public class AuthCodingRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doGetAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
"Unable to get token from coding using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
...
...
@@ -42,9 +40,7 @@ public class AuthCodingRequest extends AuthDefaultRequest {
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
object
.
getString
(
"msg"
));
}
this
.
checkResponse
(
object
);
object
=
object
.
getJSONObject
(
"data"
);
return
AuthUser
.
builder
()
...
...
@@ -63,6 +59,17 @@ public class AuthCodingRequest extends AuthDefaultRequest {
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
object
.
getString
(
"msg"
));
}
}
/**
* 返回认证url,可自行跳转页面
*
...
...
src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
/**
* CSDN登录
...
...
@@ -28,9 +28,7 @@ public class AuthCsdnRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
"Unable to get token from csdn using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
().
accessToken
(
accessTokenObject
.
getString
(
"access_token"
)).
build
();
}
...
...
@@ -38,9 +36,7 @@ public class AuthCsdnRequest extends AuthDefaultRequest {
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
object
.
getString
(
"error"
));
}
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"username"
))
.
username
(
object
.
getString
(
"username"
))
...
...
@@ -51,4 +47,15 @@ public class AuthCsdnRequest extends AuthDefaultRequest {
.
source
(
AuthSource
.
CSDN
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
object
.
getString
(
"error"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
浏览文件 @
3a04098e
...
...
@@ -3,6 +3,7 @@ package me.zhyd.oauth.request;
import
cn.hutool.http.HttpRequest
;
import
cn.hutool.http.HttpResponse
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.exception.AuthException
;
...
...
@@ -20,6 +21,7 @@ import me.zhyd.oauth.utils.UrlBuilder;
* @since 1.8
*/
@Data
@Slf4j
public
abstract
class
AuthDefaultRequest
implements
AuthRequest
{
protected
AuthConfig
config
;
protected
AuthSource
source
;
...
...
@@ -48,6 +50,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
AuthUser
user
=
this
.
getUserInfo
(
authToken
);
return
AuthResponse
.
builder
().
code
(
AuthResponseStatus
.
SUCCESS
.
getCode
()).
data
(
user
).
build
();
}
catch
(
Exception
e
)
{
log
.
error
(
"Failed to login with oauth authorization."
,
e
);
return
this
.
responseError
(
e
);
}
}
...
...
src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
浏览文件 @
3a04098e
...
...
@@ -32,10 +32,8 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
JSONObject
userInfoObject
=
this
.
checkResponse
(
object
);
JSONObject
userInfoObject
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
userInfoObject
);
return
AuthUser
.
builder
()
.
uuid
(
userInfoObject
.
getString
(
"union_id"
))
.
username
(
userInfoObject
.
getString
(
"nickname"
))
...
...
@@ -52,7 +50,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
public
AuthResponse
refresh
(
AuthToken
oldToken
)
{
return
AuthResponse
.
builder
()
.
code
(
AuthResponseStatus
.
SUCCESS
.
getCode
())
.
data
(
refreshTokenUrl
(
oldToken
.
getRefreshToken
(
)))
.
data
(
getToken
(
refreshTokenUrl
(
oldToken
.
getRefreshToken
()
)))
.
build
();
}
...
...
@@ -60,16 +58,14 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
* 检查响应内容是否正确
*
* @param object 请求响应内容
* @return 实际请求数据的json对象
*/
private
JSONObject
checkResponse
(
JSONObject
object
)
{
private
void
checkResponse
(
JSONObject
object
)
{
String
message
=
object
.
getString
(
"message"
);
JSONObject
data
=
object
.
getJSONObject
(
"data"
);
int
errorCode
=
data
.
getIntValue
(
"error_code"
);
if
(
"error"
.
equals
(
message
)
||
errorCode
!=
0
)
{
throw
new
AuthException
(
errorCode
,
data
.
getString
(
"description"
));
}
return
data
;
}
/**
...
...
@@ -82,14 +78,13 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
accessTokenStr
);
JSONObject
accessTokenObject
=
this
.
checkResponse
(
object
);
this
.
checkResponse
(
object
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenO
bject
.
getString
(
"access_token"
))
.
openId
(
accessTokenO
bject
.
getString
(
"open_id"
))
.
expireIn
(
accessTokenO
bject
.
getIntValue
(
"expires_in"
))
.
refreshToken
(
accessTokenO
bject
.
getString
(
"refresh_token"
))
.
scope
(
accessTokenO
bject
.
getString
(
"scope"
))
.
accessToken
(
o
bject
.
getString
(
"access_token"
))
.
openId
(
o
bject
.
getString
(
"open_id"
))
.
expireIn
(
o
bject
.
getIntValue
(
"expires_in"
))
.
refreshToken
(
o
bject
.
getString
(
"refresh_token"
))
.
scope
(
o
bject
.
getString
(
"scope"
))
.
build
();
}
...
...
@@ -112,7 +107,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
/**
* 返回获取accessToken的url
*
* @param code
* @param code
oauth的授权码
* @return 返回获取accessToken的url
*/
@Override
...
...
@@ -128,7 +123,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
/**
* 返回获取userInfo的url
*
* @param authToken
* @param authToken
oauth返回的token
* @return 返回获取userInfo的url
*/
@Override
...
...
@@ -142,7 +137,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
/**
* 返回获取accessToken的url
*
* @param refreshToken
* @param refreshToken
oauth返回的refreshtoken
* @return 返回获取accessToken的url
*/
@Override
...
...
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -28,11 +28,7 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
accessTokenObject
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
...
...
@@ -45,22 +41,12 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
HttpResponse
response
=
doGetUserInfo
(
authToken
);
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
String
picture
=
null
;
if
(
object
.
containsKey
(
"picture"
))
{
JSONObject
pictureObj
=
object
.
getJSONObject
(
"picture"
);
pictureObj
=
pictureObj
.
getJSONObject
(
"data"
);
if
(
null
!=
pictureObj
)
{
picture
=
pictureObj
.
getString
(
"url"
);
}
}
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
username
(
object
.
getString
(
"name"
))
.
nickname
(
object
.
getString
(
"name"
))
.
avatar
(
picture
)
.
avatar
(
getUserPicture
(
object
)
)
.
location
(
object
.
getString
(
"locale"
))
.
email
(
object
.
getString
(
"email"
))
.
gender
(
AuthUserGender
.
getRealGender
(
object
.
getString
(
"gender"
)))
...
...
@@ -69,10 +55,22 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
.
build
();
}
private
String
getUserPicture
(
JSONObject
object
)
{
String
picture
=
null
;
if
(
object
.
containsKey
(
"picture"
))
{
JSONObject
pictureObj
=
object
.
getJSONObject
(
"picture"
);
pictureObj
=
pictureObj
.
getJSONObject
(
"data"
);
if
(
null
!=
pictureObj
)
{
picture
=
pictureObj
.
getString
(
"url"
);
}
}
return
picture
;
}
/**
* 返回获取userInfo的url
*
* @param authToken
* @param authToken
用户token
* @return 返回获取userInfo的url
*/
@Override
...
...
@@ -82,4 +80,15 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
.
queryParam
(
"fields"
,
"id,name,birthday,gender,hometown,email,devices,picture.width(400)"
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
/**
* Gitee登录
...
...
@@ -27,9 +27,7 @@ public class AuthGiteeRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
"Unable to get token from gitee using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
refreshToken
(
accessTokenObject
.
getString
(
"refresh_token"
))
...
...
@@ -44,6 +42,7 @@ public class AuthGiteeRequest extends AuthDefaultRequest {
HttpResponse
response
=
doGetUserInfo
(
authToken
);
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
username
(
object
.
getString
(
"login"
))
...
...
@@ -59,4 +58,15 @@ public class AuthGiteeRequest extends AuthDefaultRequest {
.
source
(
AuthSource
.
GITEE
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,14 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.GlobalAuthUtil
;
import
java.util.Map
;
/**
* Github登录
...
...
@@ -29,22 +26,20 @@ public class AuthGithubRequest extends AuthDefaultRequest {
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
Map
<
String
,
String
>
res
=
GlobalAuthUtil
.
parseStringToMap
(
response
.
body
());
if
(
res
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
res
.
get
(
"error"
)
+
":"
+
res
.
get
(
"error_description"
));
}
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
res
.
get
(
"access_token"
))
.
scope
(
res
.
get
(
"scope"
))
.
tokenType
(
res
.
get
(
"token_type"
))
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
scope
(
accessTokenObject
.
getString
(
"scope"
))
.
tokenType
(
accessTokenObject
.
getString
(
"token_type"
))
.
build
();
}
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
String
userInfo
=
response
.
body
(
);
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
()
);
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
username
(
object
.
getString
(
"login"
))
...
...
@@ -60,4 +55,15 @@ public class AuthGithubRequest extends AuthDefaultRequest {
.
source
(
AuthSource
.
GITHUB
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -28,12 +28,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error"
)
||
accessTokenObject
.
containsKey
(
"error_description"
))
{
throw
new
AuthException
(
"get google access_token has error:["
+
accessTokenObject
.
getString
(
"error"
)
+
"], error_description:["
+
accessTokenObject
.
getString
(
"error_description"
)
+
"]"
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
...
...
@@ -48,6 +43,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
HttpResponse
response
=
doGetUserInfo
(
authToken
);
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"sub"
))
.
username
(
object
.
getString
(
"name"
))
...
...
@@ -87,4 +83,15 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
protected
String
userInfoUrl
(
AuthToken
authToken
)
{
return
UrlBuilder
.
fromBaseUrl
(
source
.
userInfo
()).
queryParam
(
"id_token"
,
authToken
.
getAccessToken
()).
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
)
||
object
.
containsKey
(
"error_description"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
浏览文件 @
3a04098e
...
...
@@ -43,7 +43,32 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
this
.
checkResponse
(
userInfoObject
);
// 组装用户名
String
userName
=
getUserName
(
userInfoObject
);
// 获取用户头像
String
avatar
=
this
.
getAvatar
(
userInfoObject
);
// 获取用户邮箱地址
String
email
=
this
.
getUserEmail
(
accessToken
);
return
AuthUser
.
builder
()
.
uuid
(
userInfoObject
.
getString
(
"id"
))
.
username
(
userName
)
.
nickname
(
userName
)
.
avatar
(
avatar
)
.
email
(
email
)
.
token
(
authToken
)
.
gender
(
AuthUserGender
.
UNKNOWN
)
.
source
(
AuthSource
.
LINKEDIN
)
.
build
();
}
/**
* 获取用户的真实名
*
* @param userInfoObject 用户json对象
* @return 用户名
*/
private
String
getUserName
(
JSONObject
userInfoObject
)
{
String
firstName
,
lastName
;
// 获取firstName
if
(
userInfoObject
.
containsKey
(
"localizedFirstName"
))
{
...
...
@@ -57,9 +82,16 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
}
else
{
lastName
=
getUserName
(
userInfoObject
,
"lastName"
);
}
String
userName
=
firstName
+
" "
+
lastName
;
return
firstName
+
" "
+
lastName
;
}
// 获取用户头像
/**
* 获取用户的头像
*
* @param userInfoObject 用户json对象
* @return 用户的头像地址
*/
private
String
getAvatar
(
JSONObject
userInfoObject
)
{
String
avatar
=
null
;
JSONObject
profilePictureObject
=
userInfoObject
.
getJSONObject
(
"profilePicture"
);
if
(
profilePictureObject
.
containsKey
(
"displayImage~"
))
{
...
...
@@ -70,21 +102,15 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
avatar
=
largestImageObj
.
getJSONArray
(
"identifiers"
).
getJSONObject
(
0
).
getString
(
"identifier"
);
}
}
// 获取用户邮箱地址
String
email
=
this
.
getUserEmail
(
accessToken
);
return
AuthUser
.
builder
()
.
uuid
(
userInfoObject
.
getString
(
"id"
))
.
username
(
userName
)
.
nickname
(
userName
)
.
avatar
(
avatar
)
.
email
(
email
)
.
token
(
authToken
)
.
gender
(
AuthUserGender
.
UNKNOWN
)
.
source
(
AuthSource
.
LINKEDIN
)
.
build
();
return
avatar
;
}
/**
* 获取用户的email
*
* @param accessToken 用户授权后返回的token
* @return 用户的邮箱地址
*/
private
String
getUserEmail
(
String
accessToken
)
{
String
email
=
null
;
HttpResponse
emailResponse
=
HttpRequest
.
get
(
"https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))"
)
...
...
@@ -92,8 +118,8 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
.
header
(
"Connection"
,
"Keep-Alive"
)
.
header
(
"Authorization"
,
"Bearer "
+
accessToken
)
.
execute
();
System
.
out
.
println
(
emailResponse
.
body
());
JSONObject
emailObj
=
JSONObject
.
parseObject
(
emailResponse
.
body
());
this
.
checkResponse
(
emailObj
);
if
(
emailObj
.
containsKey
(
"elements"
))
{
email
=
emailObj
.
getJSONArray
(
"elements"
)
.
getJSONObject
(
0
)
...
...
@@ -125,9 +151,14 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
.
build
();
}
private
void
checkResponse
(
JSONObject
userInfoObject
)
{
if
(
userInfoObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
userInfoObject
.
getString
(
"error_description"
));
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
...
...
src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
浏览文件 @
3a04098e
...
...
@@ -55,9 +55,14 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
.
build
();
}
private
void
checkResponse
(
JSONObject
response
)
{
if
(
response
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
response
.
getString
(
"error_description"
));
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
...
...
@@ -69,6 +74,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
HttpResponse
response
=
HttpRequest
.
get
(
userInfoUrl
(
authToken
)).
header
(
"Authorization"
,
jwt
).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
username
(
object
.
getString
(
"userPrincipalName"
))
...
...
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -28,9 +28,7 @@ public class AuthOschinaRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
"Unable to get token from oschina using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
refreshToken
(
accessTokenObject
.
getString
(
"refresh_token"
))
...
...
@@ -43,9 +41,7 @@ public class AuthOschinaRequest extends AuthDefaultRequest {
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
this
.
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
username
(
object
.
getString
(
"name"
))
...
...
@@ -91,4 +87,15 @@ public class AuthOschinaRequest extends AuthDefaultRequest {
.
queryParam
(
"dataType"
,
"json"
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
java.util.Objects
;
...
...
@@ -24,6 +24,8 @@ import static me.zhyd.oauth.config.AuthSource.PINTEREST;
*/
public
class
AuthPinterestRequest
extends
AuthDefaultRequest
{
private
static
final
String
FAILURE
=
"failure"
;
public
AuthPinterestRequest
(
AuthConfig
config
)
{
super
(
config
,
PINTEREST
);
}
...
...
@@ -32,10 +34,7 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doPostAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(!
response
.
isOk
())
{
throw
new
AuthException
(
"Unable to get token from Pinterest using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
tokenType
(
accessTokenObject
.
getString
(
"token_type"
))
...
...
@@ -48,8 +47,9 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
.
queryParam
(
"fields"
,
"id,username,first_name,last_name,bio,image"
)
.
build
();
HttpResponse
response
=
HttpRequest
.
post
(
userinfoUrl
).
execute
();
JSONObject
userObj
=
JSONObject
.
parseObject
(
response
.
body
()).
getJSONObject
(
"data"
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
object
);
JSONObject
userObj
=
object
.
getJSONObject
(
"data"
);
return
AuthUser
.
builder
()
.
uuid
(
userObj
.
getString
(
"id"
))
.
avatar
(
getAvatarUrl
(
userObj
))
...
...
@@ -82,4 +82,15 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(!
object
.
containsKey
(
"status"
)
&&
FAILURE
.
equals
(
object
.
getString
(
"status"
)))
{
throw
new
AuthException
(
object
.
getString
(
"message"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthQqRequest.java
浏览文件 @
3a04098e
...
...
@@ -6,11 +6,9 @@ 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.AuthCallback
;
import
me.zhyd.oauth.model.AuthToken
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.enums.AuthUserGender
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.*
;
import
me.zhyd.oauth.utils.GlobalAuthUtil
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
...
...
@@ -33,14 +31,15 @@ public class AuthQqRequest extends AuthDefaultRequest {
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doGetAuthorizationCode
(
authCallback
.
getCode
());
Map
<
String
,
String
>
accessTokenObject
=
GlobalAuthUtil
.
parseStringToMap
(
response
.
body
());
if
(!
accessTokenObject
.
containsKey
(
"access_token"
))
{
throw
new
AuthException
(
"Unable to get token from qq using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
);
}
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
get
(
"access_token"
))
.
expireIn
(
Integer
.
valueOf
(
accessTokenObject
.
get
(
"expires_in"
)))
.
refreshToken
(
accessTokenObject
.
get
(
"refresh_token"
))
return
getAuthToken
(
response
);
}
@Override
public
AuthResponse
refresh
(
AuthToken
authToken
)
{
HttpResponse
response
=
HttpRequest
.
get
(
refreshTokenUrl
(
authToken
.
getRefreshToken
())).
execute
();
return
AuthResponse
.
builder
()
.
code
(
AuthResponseStatus
.
SUCCESS
.
getCode
())
.
data
(
getAuthToken
(
response
))
.
build
();
}
...
...
@@ -97,7 +96,7 @@ public class AuthQqRequest extends AuthDefaultRequest {
/**
* 返回获取userInfo的url
*
* @param authToken
* @param authToken
用户授权token
* @return 返回获取userInfo的url
*/
@Override
...
...
@@ -108,4 +107,16 @@ public class AuthQqRequest extends AuthDefaultRequest {
.
queryParam
(
"openid"
,
authToken
.
getOpenId
())
.
build
();
}
private
AuthToken
getAuthToken
(
HttpResponse
response
)
{
Map
<
String
,
String
>
accessTokenObject
=
GlobalAuthUtil
.
parseStringToMap
(
response
.
body
());
if
(!
accessTokenObject
.
containsKey
(
"access_token"
)
||
accessTokenObject
.
containsKey
(
"code"
))
{
throw
new
AuthException
(
accessTokenObject
.
get
(
"msg"
));
}
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
get
(
"access_token"
))
.
expireIn
(
Integer
.
valueOf
(
accessTokenObject
.
get
(
"expires_in"
)))
.
refreshToken
(
accessTokenObject
.
get
(
"refresh_token"
))
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/AuthRenrenRequest.java
浏览文件 @
3a04098e
...
...
@@ -60,7 +60,7 @@ public class AuthRenrenRequest extends AuthDefaultRequest {
private
AuthToken
getToken
(
String
url
)
{
HttpResponse
response
=
HttpRequest
.
post
(
url
).
execute
();
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
!
response
.
isOk
(
))
{
if
(
jsonObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
"Failed to get token from Renren: "
+
jsonObject
);
}
...
...
src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
static
me
.
zhyd
.
oauth
.
config
.
AuthSource
.
STACK_OVERFLOW
;
...
...
@@ -35,9 +35,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.
form
(
parseQueryToMap
(
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
);
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
...
...
@@ -53,7 +51,9 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.
queryParam
(
"key"
,
this
.
config
.
getStackOverflowKey
())
.
build
();
HttpResponse
response
=
HttpRequest
.
get
(
userInfoUrl
).
execute
();
JSONObject
userObj
=
JSONObject
.
parseObject
(
response
.
body
()).
getJSONArray
(
"items"
).
getJSONObject
(
0
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
object
);
JSONObject
userObj
=
object
.
getJSONArray
(
"items"
).
getJSONObject
(
0
);
return
AuthUser
.
builder
()
.
uuid
(
userObj
.
getString
(
"user_id"
))
...
...
@@ -77,4 +77,15 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.
queryParam
(
"scope"
,
"read_inbox"
)
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
}
}
}
src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
浏览文件 @
3a04098e
...
...
@@ -4,11 +4,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -28,10 +28,7 @@ public class AuthTencentCloudRequest extends AuthDefaultRequest {
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
HttpResponse
response
=
doGetAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
"Unable to get token from tencent cloud using code ["
+
authCallback
.
getCode
()
+
"]: "
+
accessTokenObject
.
get
(
"msg"
));
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
...
...
@@ -43,9 +40,8 @@ public class AuthTencentCloudRequest extends AuthDefaultRequest {
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
HttpResponse
response
=
doGetUserInfo
(
authToken
);
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
object
.
getString
(
"msg"
));
}
this
.
checkResponse
(
object
);
object
=
object
.
getJSONObject
(
"data"
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
...
...
@@ -63,6 +59,17 @@ public class AuthTencentCloudRequest extends AuthDefaultRequest {
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
getIntValue
(
"code"
)
!=
0
)
{
throw
new
AuthException
(
object
.
getString
(
"msg"
));
}
}
/**
* 返回认证url,可自行跳转页面
*
...
...
src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
浏览文件 @
3a04098e
...
...
@@ -5,11 +5,11 @@ import com.alibaba.fastjson.JSONObject;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.enums.AuthToutiaoErrorCode
;
import
me.zhyd.oauth.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -30,10 +30,7 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
HttpResponse
response
=
doGetAuthorizationCode
(
authCallback
.
getCode
());
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
AuthToutiaoErrorCode
.
getErrorCode
(
accessTokenObject
.
getIntValue
(
"error_code"
))
.
getDesc
());
}
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
...
...
@@ -48,9 +45,7 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
JSONObject
userProfile
=
JSONObject
.
parseObject
(
userResponse
.
body
());
if
(
userProfile
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
AuthToutiaoErrorCode
.
getErrorCode
(
userProfile
.
getIntValue
(
"error_code"
)).
getDesc
());
}
this
.
checkResponse
(
userProfile
);
JSONObject
user
=
userProfile
.
getJSONObject
(
"data"
);
...
...
@@ -89,7 +84,7 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
/**
* 返回获取accessToken的url
*
* @param code
* @param code
授权码
* @return 返回获取accessToken的url
*/
@Override
...
...
@@ -105,7 +100,7 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
/**
* 返回获取userInfo的url
*
* @param authToken
* @param authToken
用户授权后的token
* @return 返回获取userInfo的url
*/
@Override
...
...
@@ -115,4 +110,16 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
.
queryParam
(
"access_token"
,
authToken
.
getAccessToken
())
.
build
();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private
void
checkResponse
(
JSONObject
object
)
{
if
(
object
.
containsKey
(
"error_code"
))
{
throw
new
AuthException
(
AuthToutiaoErrorCode
.
getErrorCode
(
object
.
getIntValue
(
"error_code"
))
.
getDesc
());
}
}
}
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
浏览文件 @
3a04098e
...
...
@@ -118,7 +118,7 @@ public class AuthWeChatRequest extends AuthDefaultRequest {
/**
* 返回获取accessToken的url
*
* @param code
* @param code
授权码
* @return 返回获取accessToken的url
*/
@Override
...
...
@@ -134,7 +134,7 @@ public class AuthWeChatRequest extends AuthDefaultRequest {
/**
* 返回获取userInfo的url
*
* @param authToken
* @param authToken
用户授权后的token
* @return 返回获取userInfo的url
*/
@Override
...
...
@@ -147,10 +147,10 @@ public class AuthWeChatRequest extends AuthDefaultRequest {
}
/**
* 返回获取
accessToken
的url
* 返回获取
userInfo
的url
*
* @param refreshToken
* @return 返回获取
accessToken
的url
* @param refreshToken
getAccessToken方法返回的refreshToken
* @return 返回获取
userInfo
的url
*/
@Override
protected
String
refreshTokenUrl
(
String
refreshToken
)
{
...
...
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
浏览文件 @
3a04098e
...
...
@@ -5,11 +5,11 @@ 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.enums.AuthUserGender
;
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.enums.AuthUserGender
;
import
me.zhyd.oauth.utils.IpUtils
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
...
...
@@ -34,8 +34,7 @@ public class AuthWeiboRequest extends AuthDefaultRequest {
String
accessTokenStr
=
response
.
body
();
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
accessTokenStr
);
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
"Unable to get token from weibo using code ["
+
authCallback
.
getCode
()
+
"]:"
+
accessTokenObject
.
getString
(
"error_description"
));
throw
new
AuthException
(
accessTokenObject
.
getString
(
"error_description"
));
}
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
...
...
src/test/java/me/zhyd/oauth/utils/CustomTest.java
0 → 100644
浏览文件 @
3a04098e
package
me.zhyd.oauth.utils
;
import
org.junit.Test
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/7/19 15:52
* @since 1.8
*/
public
class
CustomTest
{
/**
* 1000000: 23135ms
* 100000: 3016ms
* 10000: 328ms
* 1000: 26ms
*/
@Test
public
void
test
()
{
long
start
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
1000
;
i
++)
{
callMethod
();
}
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
((
end
-
start
)
+
"ms"
);
}
/**
* 1000000: 19058ms
* 100000: 2772ms
* 10000: 323ms
* 1000: 29ms
*/
@Test
public
void
test2
()
{
long
end
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
1000
;
i
++)
{
callMethod2
();
}
long
end2
=
System
.
currentTimeMillis
();
System
.
out
.
println
((
end2
-
end
)
+
"ms"
);
}
public
String
callMethod
()
{
StackTraceElement
[]
stackTrace
=
Thread
.
currentThread
().
getStackTrace
();
// for (StackTraceElement stackTraceElement : stackTrace) {
// System.out.println(stackTraceElement.getMethodName());
// }
return
stackTrace
[
2
].
getMethodName
();
}
public
String
callMethod2
()
{
StackTraceElement
[]
stackTrace
=
(
new
Throwable
()).
getStackTrace
();
// for (StackTraceElement stackTraceElement : stackTrace) {
// System.out.println(stackTraceElement.getMethodName());
// }
return
stackTrace
[
2
].
getMethodName
();
}
}
src/test/java/me/zhyd/oauth/utils/UrlBuilderTest.java
浏览文件 @
3a04098e
...
...
@@ -17,11 +17,12 @@ import org.junit.Test;
public
class
UrlBuilderTest
{
@Test
public
void
testUrlBuilder
()
{
AuthConfig
config
=
new
AuthConfig
();
config
.
setClientId
(
"appid-110110110"
);
config
.
setClientSecret
(
"secret-110110110"
);
config
.
setRedirectUri
(
"https://xkcoding.com"
);
config
.
setState
(
AuthState
.
create
(
AuthSource
.
WECHAT
));
AuthConfig
config
=
AuthConfig
.
builder
()
.
clientId
(
"appid-110110110"
)
.
clientSecret
(
"secret-110110110"
)
.
redirectUri
(
"https://xkcoding.com"
)
.
state
(
AuthState
.
create
(
AuthSource
.
WECHAT
))
.
build
();
String
build
=
UrlBuilder
.
fromBaseUrl
(
AuthSource
.
WECHAT
.
authorize
())
.
queryParam
(
"appid"
,
config
.
getClientId
())
.
queryParam
(
"redirect_uri"
,
config
.
getRedirectUri
())
...
...
update.md
浏览文件 @
3a04098e
...
...
@@ -6,6 +6,8 @@
4.
优化百度授权流程,增加refresh token的方法
5.
优化
`AuthConfig`
、
`AuthResponse`
类,去掉不必要的lombonk注解,减少编译后的代码量
6.
使用lombok注解优化枚举类
7.
`AuthQqRequest`
增加refresh方法
8.
优化代码
### 2019/07/18
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录