Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
晓序
JustAuth
提交
47971438
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,发现更多精彩内容 >>
提交
47971438
编写于
7月 16, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
拆分UrlBuilder工具类,已拆分WeiboRequest
上级
3b80e118
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
249 addition
and
45 deletion
+249
-45
src/main/java/me/zhyd/oauth/config/AuthSource.java
src/main/java/me/zhyd/oauth/config/AuthSource.java
+24
-0
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
+15
-6
src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
+7
-0
src/main/java/me/zhyd/oauth/url/AbstractUrlBuilder.java
src/main/java/me/zhyd/oauth/url/AbstractUrlBuilder.java
+56
-0
src/main/java/me/zhyd/oauth/url/WeiboUrlBuilder.java
src/main/java/me/zhyd/oauth/url/WeiboUrlBuilder.java
+48
-0
src/main/java/me/zhyd/oauth/url/entity/AuthAccessTokenEntity.java
.../java/me/zhyd/oauth/url/entity/AuthAccessTokenEntity.java
+25
-0
src/main/java/me/zhyd/oauth/url/entity/AuthAuthorizeEntity.java
...in/java/me/zhyd/oauth/url/entity/AuthAuthorizeEntity.java
+20
-0
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
...java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
+14
-0
src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java
src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java
+13
-0
src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java
...ain/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java
+26
-0
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+0
-38
src/test/java/me/zhyd/oauth/utils/AuthStateTest.java
src/test/java/me/zhyd/oauth/utils/AuthStateTest.java
+1
-1
未找到文件。
src/main/java/me/zhyd/oauth/config/AuthSource.java
浏览文件 @
47971438
...
...
@@ -420,6 +420,30 @@ public enum AuthSource {
public
String
userInfo
()
{
return
"https://open.snssdk.com/data/user_profile"
;
}
},
/**
* Teambition
*/
TEAMBITION
{
@Override
public
String
authorize
()
{
return
"https://account.teambition.com/oauth2/authorize"
;
}
@Override
public
String
accessToken
()
{
return
"https://account.teambition.com/oauth2/access_token"
;
}
@Override
public
String
refresh
()
{
return
"https://account.teambition.com/oauth2/refresh_token"
;
}
@Override
public
String
userInfo
()
{
return
"https://account.teambition.com/users/me"
;
}
};
/**
...
...
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
浏览文件 @
47971438
...
...
@@ -10,9 +10,12 @@ 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.WeiboUrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthUserInfoEntity
;
import
me.zhyd.oauth.utils.IpUtils
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -25,13 +28,15 @@ import me.zhyd.oauth.utils.UrlBuilder;
public
class
AuthWeiboRequest
extends
BaseAuthRequest
{
public
AuthWeiboRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
WEIBO
);
super
(
config
,
AuthSource
.
WEIBO
,
new
WeiboUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getWeiboAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
authCallback
.
getCode
(),
config
.
getRedirectUri
());
String
accessTokenUrl
=
this
.
urlBuilder
.
getAccessTokenUrl
(
AuthAccessTokenEntity
.
builder
()
.
config
(
config
)
.
code
(
authCallback
.
getCode
())
.
build
());
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
accessTokenStr
);
...
...
@@ -51,7 +56,9 @@ public class AuthWeiboRequest extends BaseAuthRequest {
String
accessToken
=
authToken
.
getAccessToken
();
String
uid
=
authToken
.
getUid
();
String
oauthParam
=
String
.
format
(
"uid=%s&access_token=%s"
,
uid
,
accessToken
);
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getWeiboUserInfoUrl
(
oauthParam
))
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
extra
(
oauthParam
)
.
build
()))
.
header
(
"Authorization"
,
"OAuth2 "
+
oauthParam
)
.
header
(
"API-RemoteIP"
,
IpUtils
.
getIp
())
.
execute
();
...
...
@@ -82,6 +89,8 @@ public class AuthWeiboRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getWeiboAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
}
src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
浏览文件 @
47971438
...
...
@@ -8,6 +8,7 @@ import me.zhyd.oauth.model.AuthCallback;
import
me.zhyd.oauth.model.AuthResponse
;
import
me.zhyd.oauth.model.AuthToken
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.url.AbstractUrlBuilder
;
import
me.zhyd.oauth.utils.AuthChecker
;
/**
...
...
@@ -19,6 +20,7 @@ import me.zhyd.oauth.utils.AuthChecker;
public
abstract
class
BaseAuthRequest
implements
AuthRequest
{
protected
AuthConfig
config
;
protected
AuthSource
source
;
protected
AbstractUrlBuilder
urlBuilder
;
public
BaseAuthRequest
(
AuthConfig
config
,
AuthSource
source
)
{
this
.
config
=
config
;
...
...
@@ -30,6 +32,11 @@ public abstract class BaseAuthRequest implements AuthRequest {
AuthChecker
.
checkConfig
(
config
,
source
);
}
public
BaseAuthRequest
(
AuthConfig
config
,
AuthSource
source
,
AbstractUrlBuilder
urlBuilder
)
{
this
(
config
,
source
);
this
.
urlBuilder
=
urlBuilder
;
}
protected
abstract
AuthToken
getAccessToken
(
AuthCallback
authCallback
);
protected
abstract
AuthUser
getUserInfo
(
AuthToken
authToken
);
...
...
src/main/java/me/zhyd/oauth/url/AbstractUrlBuilder.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url
;
import
me.zhyd.oauth.url.entity.*
;
import
me.zhyd.oauth.utils.StringUtils
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
abstract
class
AbstractUrlBuilder
{
/**
* 获取AccessToken的URL
*
* @return AccessTokenUrl
*/
public
abstract
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
);
/**
* 获取用户信息的URL
*
* @return UserInfoUrl
*/
public
abstract
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
);
/**
* 获取跳转授权页面的URL
*
* @return AuthorizeUrl
*/
public
abstract
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
);
/**
* 获取刷新token的URL
*
* @return RefreshUrl
*/
public
abstract
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
);
/**
* 获取取消授权的URL
*
* @return RevokeUrl
*/
public
abstract
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
);
/**
* 获取state,如果为空, 则默认去当前日期的时间戳
*
* @param state state
*/
protected
String
getRealState
(
String
state
)
{
return
StringUtils
.
isEmpty
(
state
)
?
String
.
valueOf
(
System
.
currentTimeMillis
())
:
state
;
}
}
src/main/java/me/zhyd/oauth/url/WeiboUrlBuilder.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.url.entity.*
;
import
java.text.MessageFormat
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
WeiboUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
WEIBO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
private
static
final
String
WEIBO_USER_INFO_PATTERN
=
"{0}?{1}"
;
private
static
final
String
WEIBO_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
WEIBO_ACCESS_TOKEN_PATTERN
,
AuthSource
.
WEIBO
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
(),
config
.
getRedirectUri
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
WEIBO_USER_INFO_PATTERN
,
AuthSource
.
WEIBO
.
userInfo
(),
userInfoEntity
.
getExtra
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
WEIBO_AUTHORIZE_PATTERN
,
AuthSource
.
WEIBO
.
authorize
(),
config
.
getClientId
(),
config
.
getRedirectUri
(),
this
.
getRealState
(
config
.
getState
()));
}
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
return
null
;
}
@Override
public
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
)
{
return
null
;
}
}
src/main/java/me/zhyd/oauth/url/entity/AuthAccessTokenEntity.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url.entity
;
import
lombok.Builder
;
import
lombok.Getter
;
import
me.zhyd.oauth.config.AuthConfig
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
@Getter
@Builder
public
class
AuthAccessTokenEntity
{
/**
* JustAuth的配置类
*/
private
AuthConfig
config
;
/**
* 访问AuthorizeUrl后回调时带的参数code
*/
private
String
code
;
}
src/main/java/me/zhyd/oauth/url/entity/AuthAuthorizeEntity.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url.entity
;
import
lombok.Builder
;
import
lombok.Getter
;
import
me.zhyd.oauth.config.AuthConfig
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
@Getter
@Builder
public
class
AuthAuthorizeEntity
{
/**
* JustAuth的配置类
*/
private
AuthConfig
config
;
}
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url.entity
;
import
lombok.Builder
;
import
lombok.Getter
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
@Getter
@Builder
public
class
AuthRefreshTokenEntity
{
}
src/main/java/me/zhyd/oauth/url/entity/AuthRevokeEntity.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url.entity
;
import
lombok.*
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
@Getter
@Builder
public
class
AuthRevokeEntity
{
}
src/main/java/me/zhyd/oauth/url/entity/AuthUserInfoEntity.java
0 → 100644
浏览文件 @
47971438
package
me.zhyd.oauth.url.entity
;
import
lombok.Builder
;
import
lombok.Getter
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
@Getter
@Builder
public
class
AuthUserInfoEntity
{
/**
* 授权返回的token
*/
private
String
accessToken
;
/**
* 用户openId
*/
private
String
openId
;
/**
* 额外的属性
*/
private
String
extra
;
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
47971438
...
...
@@ -21,9 +21,6 @@ public class UrlBuilder {
private
static
final
String
GOOGLE_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
GOOGLE_USER_INFO_PATTERN
=
"{0}?id_token={1}"
;
private
static
final
String
WEIBO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
private
static
final
String
WEIBO_USER_INFO_PATTERN
=
"{0}?{1}"
;
private
static
final
String
WEIBO_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"
;
private
static
final
String
GITEE_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
private
static
final
String
GITEE_USER_INFO_PATTERN
=
"{0}?access_token={1}"
;
...
...
@@ -140,41 +137,6 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
GITHUB_AUTHORIZE_PATTERN
,
AuthSource
.
GITHUB
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取weibo token的接口地址
*
* @param clientId weibo 应用的App Key
* @param clientSecret weibo 应用的App Secret
* @param code weibo 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getWeiboAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
WEIBO_ACCESS_TOKEN_PATTERN
,
AuthSource
.
WEIBO
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取weibo用户详情的接口地址
*
* @param token weibo 应用的token
* @return full url
*/
public
static
String
getWeiboUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
WEIBO_USER_INFO_PATTERN
,
AuthSource
.
WEIBO
.
userInfo
(),
token
);
}
/**
* 获取weibo授权地址
*
* @param clientId weibo 应用的Client ID
* @param redirectUrl weibo 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getWeiboAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
WEIBO_AUTHORIZE_PATTERN
,
AuthSource
.
WEIBO
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取gitee token的接口地址
*
...
...
src/test/java/me/zhyd/oauth/utils/AuthStateTest.java
浏览文件 @
47971438
...
...
@@ -26,7 +26,7 @@ public class AuthStateTest {
* null
*/
@Test
public
void
test
()
{
public
void
usage
()
{
String
source
=
"github"
;
System
.
out
.
println
(
"\nstep1 生成state: 预期创建一个新的state..."
);
String
state
=
AuthState
.
create
(
source
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录