Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
497498db
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看板
未验证
提交
497498db
编写于
9月 20, 2021
作者:
智布道
👁
提交者:
GitHub
9月 20, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #134 from verils/remove-provider-specific-properties
把Alipay独有的配置从AuthConfig里提取出来
上级
62cd7af6
4c205a99
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
137 addition
and
49 deletion
+137
-49
src/main/java/me/zhyd/oauth/config/AuthConfig.java
src/main/java/me/zhyd/oauth/config/AuthConfig.java
+3
-0
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
+94
-8
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
+5
-1
src/main/java/me/zhyd/oauth/utils/AuthChecker.java
src/main/java/me/zhyd/oauth/utils/AuthChecker.java
+3
-13
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
+32
-27
未找到文件。
src/main/java/me/zhyd/oauth/config/AuthConfig.java
浏览文件 @
497498db
...
@@ -38,7 +38,10 @@ public class AuthConfig {
...
@@ -38,7 +38,10 @@ public class AuthConfig {
/**
/**
* 支付宝公钥:当选择支付宝登录时,该值可用
* 支付宝公钥:当选择支付宝登录时,该值可用
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
*
* @deprecated 请使用AuthAlipayRequest的构造方法设置"alipayPublicKey"
*/
*/
@Deprecated
private
String
alipayPublicKey
;
private
String
alipayPublicKey
;
/**
/**
...
...
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
浏览文件 @
497498db
...
@@ -18,6 +18,8 @@ import me.zhyd.oauth.model.AuthCallback;
...
@@ -18,6 +18,8 @@ import me.zhyd.oauth.model.AuthCallback;
import
me.zhyd.oauth.model.AuthResponse
;
import
me.zhyd.oauth.model.AuthResponse
;
import
me.zhyd.oauth.model.AuthToken
;
import
me.zhyd.oauth.model.AuthToken
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.utils.AuthChecker
;
import
me.zhyd.oauth.utils.GlobalAuthUtils
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
me.zhyd.oauth.utils.UrlBuilder
;
...
@@ -31,31 +33,115 @@ import java.net.InetSocketAddress;
...
@@ -31,31 +33,115 @@ import java.net.InetSocketAddress;
*/
*/
public
class
AuthAlipayRequest
extends
AuthDefaultRequest
{
public
class
AuthAlipayRequest
extends
AuthDefaultRequest
{
private
AlipayClient
alipayClient
;
/**
* 支付宝公钥:当选择支付宝登录时,该值可用
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
*/
private
final
String
alipayPublicKey
;
private
final
AlipayClient
alipayClient
;
/**
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String)
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
*/
@Deprecated
public
AuthAlipayRequest
(
AuthConfig
config
)
{
public
AuthAlipayRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthDefaultSource
.
ALIPAY
);
this
(
config
,
(
String
)
null
);
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
"json"
,
"UTF-8"
,
config
.
getAlipayPublicKey
(),
"RSA2"
);
}
}
/**
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String, me.zhyd.oauth.cache.AuthStateCache)
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
*/
@Deprecated
public
AuthAlipayRequest
(
AuthConfig
config
,
AuthStateCache
authStateCache
)
{
public
AuthAlipayRequest
(
AuthConfig
config
,
AuthStateCache
authStateCache
)
{
this
(
config
,
null
,
authStateCache
);
}
/**
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String, me.zhyd.oauth.cache.AuthStateCache, java.lang.String, java.lang.Integer)
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
*/
@Deprecated
public
AuthAlipayRequest
(
AuthConfig
config
,
AuthStateCache
authStateCache
,
String
proxyHost
,
Integer
proxyPort
)
{
this
(
config
,
null
,
authStateCache
,
proxyHost
,
proxyPort
);
}
/**
* 构造方法,需要设置"alipayPublicKey"
*
* @param config 公共的OAuth配置
* @param alipayPublicKey 支付宝公钥
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig)
*/
public
AuthAlipayRequest
(
AuthConfig
config
,
String
alipayPublicKey
)
{
super
(
config
,
AuthDefaultSource
.
ALIPAY
);
this
.
alipayPublicKey
=
determineAlipayPublicKey
(
alipayPublicKey
,
config
);
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
"json"
,
"UTF-8"
,
alipayPublicKey
,
"RSA2"
);
check
(
config
);
}
/**
* 构造方法,需要设置"alipayPublicKey"
*
* @param config 公共的OAuth配置
* @param alipayPublicKey 支付宝公钥
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, me.zhyd.oauth.cache.AuthStateCache)
*/
public
AuthAlipayRequest
(
AuthConfig
config
,
String
alipayPublicKey
,
AuthStateCache
authStateCache
)
{
super
(
config
,
AuthDefaultSource
.
ALIPAY
,
authStateCache
);
super
(
config
,
AuthDefaultSource
.
ALIPAY
,
authStateCache
);
this
.
alipayPublicKey
=
determineAlipayPublicKey
(
alipayPublicKey
,
config
);
if
(
config
.
getHttpConfig
()
!=
null
&&
config
.
getHttpConfig
().
getProxy
()
!=
null
if
(
config
.
getHttpConfig
()
!=
null
&&
config
.
getHttpConfig
().
getProxy
()
!=
null
&&
config
.
getHttpConfig
().
getProxy
().
address
()
instanceof
InetSocketAddress
)
{
&&
config
.
getHttpConfig
().
getProxy
().
address
()
instanceof
InetSocketAddress
)
{
InetSocketAddress
address
=
(
InetSocketAddress
)
config
.
getHttpConfig
().
getProxy
().
address
();
InetSocketAddress
address
=
(
InetSocketAddress
)
config
.
getHttpConfig
().
getProxy
().
address
();
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
"json"
,
"UTF-8"
,
config
.
getAlipayPublicKey
()
,
"RSA2"
,
address
.
getHostName
(),
address
.
getPort
());
"json"
,
"UTF-8"
,
alipayPublicKey
,
"RSA2"
,
address
.
getHostName
(),
address
.
getPort
());
}
else
{
}
else
{
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
"json"
,
"UTF-8"
,
config
.
getAlipayPublicKey
()
,
"RSA2"
);
"json"
,
"UTF-8"
,
alipayPublicKey
,
"RSA2"
);
}
}
check
(
config
);
}
}
public
AuthAlipayRequest
(
AuthConfig
config
,
AuthStateCache
authStateCache
,
String
proxyHost
,
Integer
proxyPort
)
{
/**
* 构造方法,需要设置"alipayPublicKey"
*
* @param config 公共的OAuth配置
* @param alipayPublicKey 支付宝公钥
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, me.zhyd.oauth.cache.AuthStateCache, java.lang.String, java.lang.Integer)
*/
public
AuthAlipayRequest
(
AuthConfig
config
,
String
alipayPublicKey
,
AuthStateCache
authStateCache
,
String
proxyHost
,
Integer
proxyPort
)
{
super
(
config
,
AuthDefaultSource
.
ALIPAY
,
authStateCache
);
super
(
config
,
AuthDefaultSource
.
ALIPAY
,
authStateCache
);
this
.
alipayPublicKey
=
determineAlipayPublicKey
(
alipayPublicKey
,
config
);
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
this
.
alipayClient
=
new
DefaultAlipayClient
(
AuthDefaultSource
.
ALIPAY
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
"json"
,
"UTF-8"
,
config
.
getAlipayPublicKey
(),
"RSA2"
,
proxyHost
,
proxyPort
);
"json"
,
"UTF-8"
,
alipayPublicKey
,
"RSA2"
,
proxyHost
,
proxyPort
);
check
(
config
);
}
private
String
determineAlipayPublicKey
(
String
alipayPublicKey
,
AuthConfig
config
)
{
return
alipayPublicKey
!=
null
?
alipayPublicKey
:
config
.
getAlipayPublicKey
();
}
protected
void
check
(
AuthConfig
config
)
{
AuthChecker
.
checkConfig
(
config
,
AuthDefaultSource
.
ALIPAY
);
if
(!
StringUtils
.
isNotEmpty
(
alipayPublicKey
))
{
throw
new
AuthException
(
AuthResponseStatus
.
PARAMETER_INCOMPLETE
,
AuthDefaultSource
.
ALIPAY
);
}
// 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
if
(
GlobalAuthUtils
.
isLocalHost
(
config
.
getRedirectUri
()))
{
// The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
AuthDefaultSource
.
ALIPAY
);
}
}
@Override
protected
void
checkCode
(
AuthCallback
authCallback
)
{
if
(
StringUtils
.
isEmpty
(
authCallback
.
getAuth_code
()))
{
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_CODE
,
source
);
}
}
}
@Override
@Override
...
...
src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
浏览文件 @
497498db
...
@@ -72,7 +72,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
...
@@ -72,7 +72,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
@Override
@Override
public
AuthResponse
login
(
AuthCallback
authCallback
)
{
public
AuthResponse
login
(
AuthCallback
authCallback
)
{
try
{
try
{
AuthChecker
.
checkCode
(
source
,
authCallback
);
checkCode
(
authCallback
);
if
(!
config
.
isIgnoreCheckState
())
{
if
(!
config
.
isIgnoreCheckState
())
{
AuthChecker
.
checkState
(
authCallback
.
getState
(),
source
,
authStateCache
);
AuthChecker
.
checkState
(
authCallback
.
getState
(),
source
,
authStateCache
);
}
}
...
@@ -86,6 +86,10 @@ public abstract class AuthDefaultRequest implements AuthRequest {
...
@@ -86,6 +86,10 @@ public abstract class AuthDefaultRequest implements AuthRequest {
}
}
}
}
protected
void
checkCode
(
AuthCallback
authCallback
)
{
AuthChecker
.
checkCode
(
source
,
authCallback
);
}
/**
/**
* 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
* 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
*
*
...
...
src/main/java/me/zhyd/oauth/utils/AuthChecker.java
浏览文件 @
497498db
...
@@ -27,9 +27,6 @@ public class AuthChecker {
...
@@ -27,9 +27,6 @@ public class AuthChecker {
public
static
boolean
isSupportedAuth
(
AuthConfig
config
,
AuthSource
source
)
{
public
static
boolean
isSupportedAuth
(
AuthConfig
config
,
AuthSource
source
)
{
boolean
isSupported
=
StringUtils
.
isNotEmpty
(
config
.
getClientId
())
boolean
isSupported
=
StringUtils
.
isNotEmpty
(
config
.
getClientId
())
&&
StringUtils
.
isNotEmpty
(
config
.
getClientSecret
());
&&
StringUtils
.
isNotEmpty
(
config
.
getClientSecret
());
if
(
isSupported
&&
AuthDefaultSource
.
ALIPAY
==
source
)
{
isSupported
=
StringUtils
.
isNotEmpty
(
config
.
getAlipayPublicKey
());
}
if
(
isSupported
&&
AuthDefaultSource
.
STACK_OVERFLOW
==
source
)
{
if
(
isSupported
&&
AuthDefaultSource
.
STACK_OVERFLOW
==
source
)
{
isSupported
=
StringUtils
.
isNotEmpty
(
config
.
getStackOverflowKey
());
isSupported
=
StringUtils
.
isNotEmpty
(
config
.
getStackOverflowKey
());
}
}
...
@@ -71,18 +68,13 @@ public class AuthChecker {
...
@@ -71,18 +68,13 @@ public class AuthChecker {
// Facebook's redirect uri must use the HTTPS protocol
// Facebook's redirect uri must use the HTTPS protocol
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
}
}
// 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
if
(
AuthDefaultSource
.
ALIPAY
==
source
&&
GlobalAuthUtils
.
isLocalHost
(
redirectUri
))
{
// The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
}
// 微软的回调地址必须为https的链接或者localhost,不允许使用http
// 微软的回调地址必须为https的链接或者localhost,不允许使用http
if
(
AuthDefaultSource
.
MICROSOFT
==
source
&&
!
GlobalAuthUtils
.
isHttpsProtocolOrLocalHost
(
redirectUri
)
)
{
if
(
AuthDefaultSource
.
MICROSOFT
==
source
&&
!
GlobalAuthUtils
.
isHttpsProtocolOrLocalHost
(
redirectUri
))
{
// Microsoft's redirect uri must use the HTTPS or localhost
// Microsoft's redirect uri must use the HTTPS or localhost
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
}
}
// 微软中国的回调地址必须为https的链接或者localhost,不允许使用http
// 微软中国的回调地址必须为https的链接或者localhost,不允许使用http
if
(
AuthDefaultSource
.
MICROSOFT_CN
==
source
&&
!
GlobalAuthUtils
.
isHttpsProtocolOrLocalHost
(
redirectUri
)
)
{
if
(
AuthDefaultSource
.
MICROSOFT_CN
==
source
&&
!
GlobalAuthUtils
.
isHttpsProtocolOrLocalHost
(
redirectUri
))
{
// Microsoft's redirect uri must use the HTTPS or localhost
// Microsoft's redirect uri must use the HTTPS or localhost
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
throw
new
AuthException
(
AuthResponseStatus
.
ILLEGAL_REDIRECT_URI
,
source
);
}
}
...
@@ -103,9 +95,7 @@ public class AuthChecker {
...
@@ -103,9 +95,7 @@ public class AuthChecker {
return
;
return
;
}
}
String
code
=
callback
.
getCode
();
String
code
=
callback
.
getCode
();
if
(
source
==
AuthDefaultSource
.
ALIPAY
)
{
if
(
source
==
AuthDefaultSource
.
HUAWEI
)
{
code
=
callback
.
getAuth_code
();
}
else
if
(
source
==
AuthDefaultSource
.
HUAWEI
)
{
code
=
callback
.
getAuthorization_code
();
code
=
callback
.
getAuthorization_code
();
}
}
if
(
StringUtils
.
isEmpty
(
code
))
{
if
(
StringUtils
.
isEmpty
(
code
))
{
...
...
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
浏览文件 @
497498db
...
@@ -3,10 +3,7 @@ package me.zhyd.oauth;
...
@@ -3,10 +3,7 @@ package me.zhyd.oauth;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthDefaultSource
;
import
me.zhyd.oauth.config.AuthDefaultSource
;
import
me.zhyd.oauth.config.AuthExtendSource
;
import
me.zhyd.oauth.config.AuthExtendSource
;
import
me.zhyd.oauth.request.AuthExtendRequest
;
import
me.zhyd.oauth.request.*
;
import
me.zhyd.oauth.request.AuthGiteeRequest
;
import
me.zhyd.oauth.request.AuthGithubRequest
;
import
me.zhyd.oauth.request.AuthRequest
;
import
me.zhyd.oauth.utils.AuthStateUtils
;
import
me.zhyd.oauth.utils.AuthStateUtils
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -75,31 +72,39 @@ public class AuthRequestBuilderTest {
...
@@ -75,31 +72,39 @@ public class AuthRequestBuilderTest {
*/
*/
@Test
@Test
public
void
build4
()
{
public
void
build4
()
{
AuthConfig
config
=
AuthConfig
.
builder
()
.
clientId
(
"a"
)
.
clientSecret
(
"a"
)
.
redirectUri
(
"https://www.justauth.cn"
)
.
authServerId
(
"asd"
)
.
agentId
(
"asd"
)
.
domainPrefix
(
"asd"
)
.
stackOverflowKey
(
"asd"
)
.
deviceId
(
"asd"
)
.
clientOsType
(
3
)
.
build
();
for
(
AuthDefaultSource
value
:
AuthDefaultSource
.
values
())
{
for
(
AuthDefaultSource
value
:
AuthDefaultSource
.
values
())
{
if
(
value
==
AuthDefaultSource
.
TWITTER
)
{
switch
(
value
)
{
System
.
out
.
println
(
value
.
getTargetClass
());
case
TWITTER:
System
.
out
.
println
(
"忽略 twitter"
);
System
.
out
.
println
(
value
.
getTargetClass
());
continue
;
System
.
out
.
println
(
"忽略 twitter"
);
continue
;
case
ALIPAY:
{
// 单独给Alipay执行测试
AuthRequest
authRequest
=
new
AuthAlipayRequest
(
config
,
"asd"
);
System
.
out
.
println
(
value
.
getTargetClass
());
System
.
out
.
println
(
authRequest
.
authorize
(
AuthStateUtils
.
createState
()));
continue
;
}
default
:
AuthRequest
authRequest
=
AuthRequestBuilder
.
builder
()
.
source
(
value
.
getName
())
.
authConfig
(
config
)
.
build
();
System
.
out
.
println
(
value
.
getTargetClass
());
System
.
out
.
println
(
authRequest
.
authorize
(
AuthStateUtils
.
createState
()));
}
}
AuthRequest
authRequest
=
AuthRequestBuilder
.
builder
()
.
source
(
value
.
getName
())
.
authConfig
(
AuthConfig
.
builder
()
.
clientId
(
"a"
)
.
clientSecret
(
"a"
)
.
redirectUri
(
"https://www.justauth.cn"
)
.
alipayPublicKey
(
"asd"
)
.
authServerId
(
"asd"
)
.
agentId
(
"asd"
)
.
domainPrefix
(
"asd"
)
.
stackOverflowKey
(
"asd"
)
.
deviceId
(
"asd"
)
.
clientOsType
(
3
)
.
build
())
.
build
();
System
.
out
.
println
(
value
.
getTargetClass
());
System
.
out
.
println
(
authRequest
.
authorize
(
AuthStateUtils
.
createState
()));
}
}
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录