Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
ec31b7cb
J
JustAuth
项目概览
justauth
/
JustAuth
1 年多 前同步成功
通知
391
Star
15212
Fork
2708
代码
文件
提交
分支
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看板
提交
ec31b7cb
编写于
7月 16, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
♻
从UrlBuilder中拆分小米和OSChina相关的URL
上级
6b196456
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
138 addition
and
106 deletion
+138
-106
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
+19
-6
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
+15
-6
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
+1
-1
src/main/java/me/zhyd/oauth/url/MiUrlBuilder.java
src/main/java/me/zhyd/oauth/url/MiUrlBuilder.java
+50
-0
src/main/java/me/zhyd/oauth/url/OschinaUrlBuilder.java
src/main/java/me/zhyd/oauth/url/OschinaUrlBuilder.java
+50
-0
src/main/java/me/zhyd/oauth/url/WechatUrlBuilder.java
src/main/java/me/zhyd/oauth/url/WechatUrlBuilder.java
+1
-1
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
...java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
+2
-1
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+0
-91
未找到文件。
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
浏览文件 @
ec31b7cb
...
...
@@ -8,7 +8,10 @@ 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.utils.UrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthRefreshTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthUserInfoEntity
;
import
java.text.MessageFormat
;
...
...
@@ -28,7 +31,10 @@ public class AuthMiRequest extends BaseAuthRequest {
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getMiAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
authCallback
.
getCode
());
String
accessTokenUrl
=
this
.
urlBuilder
.
getAccessTokenUrl
(
AuthAccessTokenEntity
.
builder
()
.
config
(
config
)
.
code
(
authCallback
.
getCode
())
.
build
());
return
getToken
(
accessTokenUrl
);
}
...
...
@@ -56,7 +62,10 @@ public class AuthMiRequest extends BaseAuthRequest {
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
// 获取用户信息
HttpResponse
userResponse
=
HttpRequest
.
get
(
UrlBuilder
.
getMiUserInfoUrl
(
config
.
getClientId
(),
authToken
.
getAccessToken
()))
HttpResponse
userResponse
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
clientId
(
config
.
getClientId
())
.
accessToken
(
authToken
.
getAccessToken
())
.
build
()))
.
execute
();
JSONObject
userProfile
=
JSONObject
.
parseObject
(
userResponse
.
body
());
...
...
@@ -98,7 +107,9 @@ public class AuthMiRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getMiAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
/**
...
...
@@ -109,8 +120,10 @@ public class AuthMiRequest extends BaseAuthRequest {
*/
@Override
public
AuthResponse
refresh
(
AuthToken
authToken
)
{
String
miRefreshUrl
=
UrlBuilder
.
getMiRefreshUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
authToken
.
getRefreshToken
());
String
miRefreshUrl
=
this
.
urlBuilder
.
getRefreshUrl
(
AuthRefreshTokenEntity
.
builder
()
.
config
(
config
)
.
refreshToken
(
authToken
.
getRefreshToken
())
.
build
());
return
AuthResponse
.
builder
().
code
(
ResponseStatus
.
SUCCESS
.
getCode
()).
data
(
getToken
(
miRefreshUrl
)).
build
();
}
...
...
src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java
浏览文件 @
ec31b7cb
...
...
@@ -10,7 +10,10 @@ 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.utils.UrlBuilder
;
import
me.zhyd.oauth.url.OschinaUrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthUserInfoEntity
;
/**
* oschina登录
...
...
@@ -22,13 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder;
public
class
AuthOschinaRequest
extends
BaseAuthRequest
{
public
AuthOschinaRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
OSCHINA
);
super
(
config
,
AuthSource
.
OSCHINA
,
new
OschinaUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getOschinaAccessTokenUrl
(
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
();
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
...
...
@@ -45,7 +50,9 @@ public class AuthOschinaRequest extends BaseAuthRequest {
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getAccessToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getOschinaUserInfoUrl
(
accessToken
)).
execute
();
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
accessToken
(
accessToken
)
.
build
())).
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getString
(
"error_description"
));
...
...
@@ -71,6 +78,8 @@ public class AuthOschinaRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getOschinaAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
}
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
浏览文件 @
ec31b7cb
...
...
@@ -81,7 +81,7 @@ public class AuthWeChatRequest extends BaseAuthRequest {
@Override
public
AuthResponse
refresh
(
AuthToken
oldToken
)
{
String
refreshTokenUrl
=
this
.
urlBuilder
.
getRefreshUrl
(
AuthRefreshTokenEntity
.
builder
()
.
c
lientId
(
config
.
getClientId
()
)
.
c
onfig
(
config
)
.
refreshToken
(
oldToken
.
getRefreshToken
())
.
build
());
return
AuthResponse
.
builder
()
...
...
src/main/java/me/zhyd/oauth/url/MiUrlBuilder.java
0 → 100644
浏览文件 @
ec31b7cb
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
;
/**
* 小米相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
MiUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
MI_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&response_type=code&scope=1%203%204%206&state={3}&skip_confirm=false"
;
private
static
final
String
MI_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}&grant_type=authorization_code"
;
private
static
final
String
MI_USER_INFO_PATTERN
=
"{0}?clientId={1}&token={2}"
;
private
static
final
String
MI_REFRESH_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
MI_ACCESS_TOKEN_PATTERN
,
AuthSource
.
MI
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
accessTokenEntity
.
getCode
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
MI_USER_INFO_PATTERN
,
AuthSource
.
MI
.
userInfo
(),
userInfoEntity
.
getClientId
(),
userInfoEntity
.
getAccessToken
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
MI_AUTHORIZE_PATTERN
,
AuthSource
.
MI
.
authorize
(),
config
.
getClientId
(),
config
.
getRedirectUri
(),
this
.
getRealState
(
config
.
getState
()));
}
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
AuthConfig
config
=
refreshTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
MI_REFRESH_TOKEN_PATTERN
,
AuthSource
.
MI
.
refresh
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
refreshTokenEntity
.
getRefreshToken
());
}
@Override
public
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
)
{
return
null
;
}
}
src/main/java/me/zhyd/oauth/url/OschinaUrlBuilder.java
0 → 100644
浏览文件 @
ec31b7cb
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
;
/**
* OSChina相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
OschinaUrlBuilder
extends
AbstractUrlBuilder
{
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"
;
private
static
final
String
OSCHINA_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
(
OSCHINA_ACCESS_TOKEN_PATTERN
,
AuthSource
.
OSCHINA
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
(),
config
.
getRedirectUri
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
OSCHINA_USER_INFO_PATTERN
,
AuthSource
.
OSCHINA
.
userInfo
(),
userInfoEntity
.
getAccessToken
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
OSCHINA_AUTHORIZE_PATTERN
,
AuthSource
.
OSCHINA
.
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/WechatUrlBuilder.java
浏览文件 @
ec31b7cb
...
...
@@ -39,7 +39,7 @@ public class WechatUrlBuilder extends AbstractUrlBuilder {
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
return
MessageFormat
.
format
(
WECHAT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
refresh
(),
refreshTokenEntity
.
getClientId
(),
refreshTokenEntity
.
getRefreshToken
());
return
MessageFormat
.
format
(
WECHAT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
refresh
(),
refreshTokenEntity
.
getC
onfig
().
getC
lientId
(),
refreshTokenEntity
.
getRefreshToken
());
}
@Override
...
...
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
浏览文件 @
ec31b7cb
...
...
@@ -2,6 +2,7 @@ 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)
...
...
@@ -12,6 +13,6 @@ import lombok.Getter;
@Builder
public
class
AuthRefreshTokenEntity
{
private
String
clientId
;
private
AuthConfig
config
;
private
String
refreshToken
;
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
ec31b7cb
...
...
@@ -42,10 +42,6 @@ public class UrlBuilder {
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}&scope=user&state={3}"
;
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"
;
private
static
final
String
OSCHINA_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"
;
private
static
final
String
ALIPAY_AUTHORIZE_PATTERN
=
"{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state={3}"
;
...
...
@@ -68,10 +64,6 @@ public class UrlBuilder {
private
static
final
String
MICROSOFT_USER_INFO_PATTERN
=
"{0}"
;
private
static
final
String
MICROSOFT_REFRESH_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&scope=user.read%20mail.read&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token"
;
private
static
final
String
MI_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&response_type=code&scope=1%203%204%206&state={3}&skip_confirm=false"
;
private
static
final
String
MI_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}&grant_type=authorization_code"
;
private
static
final
String
MI_USER_INFO_PATTERN
=
"{0}?clientId={1}&token={2}"
;
private
static
final
String
MI_REFRESH_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&redirect_uri={3}&refresh_token={4}&grant_type=refresh_token"
;
/**
...
...
@@ -291,41 +283,6 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
CODING_AUTHORIZE_PATTERN
,
AuthSource
.
CODING
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取oschina token的接口地址
*
* @param clientId oschina 应用的App Key
* @param clientSecret oschina 应用的App Secret
* @param code oschina 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getOschinaAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
OSCHINA_ACCESS_TOKEN_PATTERN
,
AuthSource
.
OSCHINA
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取oschina用户详情的接口地址
*
* @param token oschina 应用的token
* @return full url
*/
public
static
String
getOschinaUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
OSCHINA_USER_INFO_PATTERN
,
AuthSource
.
OSCHINA
.
userInfo
(),
token
);
}
/**
* 获取oschina授权地址
*
* @param clientId oschina 应用的Client ID
* @param redirectUrl oschina 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getOschinaAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
OSCHINA_AUTHORIZE_PATTERN
,
AuthSource
.
OSCHINA
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取alipay授权地址
*
...
...
@@ -547,52 +504,4 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
MICROSOFT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
MICROSOFT
.
refresh
(),
clientId
,
clientSecret
,
redirectUrl
,
refreshToken
);
}
/**
* 获取小米授权地址
*
* @param clientId 小米 应用的Client ID
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getMiAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
MI_AUTHORIZE_PATTERN
,
AuthSource
.
MI
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取小米 token的接口地址
*
* @param clientId 小米 应用的Client ID
* @param clientSecret 小米 应用的Client Secret
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param code 小米 授权前的code,用来换token
* @return full url
*/
public
static
String
getMiAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
redirectUrl
,
String
code
)
{
return
MessageFormat
.
format
(
MI_ACCESS_TOKEN_PATTERN
,
AuthSource
.
MI
.
accessToken
(),
clientId
,
clientSecret
,
redirectUrl
,
code
);
}
/**
* 获取小米用户详情的接口地址
*
* @param clientId 小米 应用的client_key
* @param token token
* @return full url
*/
public
static
String
getMiUserInfoUrl
(
String
clientId
,
String
token
)
{
return
MessageFormat
.
format
(
MI_USER_INFO_PATTERN
,
AuthSource
.
MI
.
userInfo
(),
clientId
,
token
);
}
/**
* 获取小米 刷新令牌 地址
*
* @param clientId 小米 应用的client_key
* @param clientSecret 小米 应用的Client Secret
* @param redirectUrl 小米 应用授权成功后的回调地址
* @param refreshToken 小米 应用返回的refresh_token
* @return full url
*/
public
static
String
getMiRefreshUrl
(
String
clientId
,
String
clientSecret
,
String
redirectUrl
,
String
refreshToken
)
{
return
MessageFormat
.
format
(
MI_REFRESH_TOKEN_PATTERN
,
AuthSource
.
MI
.
refresh
(),
clientId
,
clientSecret
,
redirectUrl
,
refreshToken
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录