Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
My_csdo
JustAuth
提交
ea1aa53a
J
JustAuth
项目概览
My_csdo
/
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看板
提交
ea1aa53a
编写于
2月 21, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
👌
更新代码
上级
e6e016b0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
46 addition
and
18 deletion
+46
-18
src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
+2
-2
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
+9
-2
src/main/java/me/zhyd/oauth/utils/GlobalAuthUtil.java
src/main/java/me/zhyd/oauth/utils/GlobalAuthUtil.java
+34
-13
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+1
-1
未找到文件。
src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
浏览文件 @
ea1aa53a
...
...
@@ -9,7 +9,7 @@ import me.zhyd.oauth.model.AuthDingTalkErrorCode;
import
me.zhyd.oauth.model.AuthResponse
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.utils.
DingTalkSignature
Util
;
import
me.zhyd.oauth.utils.
GlobalAuth
Util
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
java.util.Objects
;
...
...
@@ -31,7 +31,7 @@ public class AuthDingTalkRequest extends BaseAuthRequest {
protected
AuthUser
getUserInfo
(
String
code
)
{
// 根据timestamp, appSecret计算签名值
String
stringToSign
=
System
.
currentTimeMillis
()
+
""
;
String
urlEncodeSignature
=
DingTalkSignatureUtil
.
compute
Signature
(
config
.
getClientSecret
(),
stringToSign
);
String
urlEncodeSignature
=
GlobalAuthUtil
.
generateDingTalk
Signature
(
config
.
getClientSecret
(),
stringToSign
);
HttpResponse
response
=
HttpRequest
.
post
(
UrlBuilder
.
getDingTalkUserInfoUrl
(
urlEncodeSignature
,
stringToSign
,
config
.
getClientId
()))
.
body
(
Objects
.
requireNonNull
(
new
JSONObject
().
put
(
"tmp_auth_code"
,
code
)))
.
execute
();
...
...
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
浏览文件 @
ea1aa53a
...
...
@@ -4,10 +4,14 @@ import cn.hutool.http.HttpRequest;
import
cn.hutool.http.HttpResponse
;
import
com.alibaba.fastjson.JSONObject
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.utils.GlobalAuthUtil
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
java.util.Map
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
...
...
@@ -25,8 +29,11 @@ public class AuthGithubRequest extends BaseAuthRequest {
protected
String
getAccessToken
(
String
code
)
{
String
accessTokenUrl
=
UrlBuilder
.
getGithubAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
code
,
config
.
getRedirectUri
());
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
return
accessTokenStr
.
split
(
"&"
)[
0
];
Map
<
String
,
String
>
res
=
GlobalAuthUtil
.
parseStringToMap
(
response
.
body
());
if
(
res
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
res
.
get
(
"error"
)
+
":"
+
res
.
get
(
"error_description"
));
}
return
res
.
get
(
"access_token"
);
}
@Override
...
...
src/main/java/me/zhyd/oauth/utils/
DingTalkSignature
Util.java
→
src/main/java/me/zhyd/oauth/utils/
GlobalAuth
Util.java
浏览文件 @
ea1aa53a
...
...
@@ -6,28 +6,27 @@ import me.zhyd.oauth.exception.AuthException;
import
javax.crypto.Mac
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.net.URLEncoder
;
import
java.security.InvalidKeyException
;
import
java.security.NoSuchAlgorithmException
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class
DingTalkSignatureUtil
{
/* The default encoding. */
public
class
GlobalAuthUtil
{
private
static
final
String
DEFAULT_ENCODING
=
"UTF-8"
;
/* Signature method. */
private
static
final
String
ALGORITHM
=
"HmacSHA256"
;
public
static
String
compute
Signature
(
String
canonicalString
,
String
secret
)
{
public
static
String
generateDingTalk
Signature
(
String
canonicalString
,
String
secret
)
{
try
{
byte
[]
signData
=
sign
(
canonicalString
.
getBytes
(
DEFAULT_ENCODING
),
secret
.
getBytes
(
DEFAULT_ENCODING
));
return
urlEncode
(
new
String
(
Base64
.
encode
(
signData
,
false
))
,
DEFAULT_ENCODING
);
return
urlEncode
(
new
String
(
Base64
.
encode
(
signData
,
false
)));
}
catch
(
UnsupportedEncodingException
ex
)
{
throw
new
AuthException
(
"Unsupported algorithm: "
+
DEFAULT_ENCODING
,
ex
);
}
}
private
static
byte
[]
sign
(
byte
[]
key
,
byte
[]
data
)
{
try
{
Mac
mac
=
Mac
.
getInstance
(
ALGORITHM
);
...
...
@@ -40,20 +39,42 @@ public class DingTalkSignatureUtil {
}
}
/**
* Encode a URL segment with special chars replaced.
*/
private
static
String
urlEncode
(
String
value
,
String
encoding
)
{
private
static
String
urlEncode
(
String
value
)
{
if
(
value
==
null
)
{
return
""
;
}
try
{
String
encoded
=
URLEncoder
.
encode
(
value
,
encoding
);
String
encoded
=
URLEncoder
.
encode
(
value
,
GlobalAuthUtil
.
DEFAULT_ENCODING
);
return
encoded
.
replace
(
"+"
,
"%20"
).
replace
(
"*"
,
"%2A"
)
.
replace
(
"~"
,
"%7E"
).
replace
(
"/"
,
"%2F"
);
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
AuthException
(
"FailedToEncodeUri"
,
e
);
throw
new
AuthException
(
"Failed To Encode Uri"
,
e
);
}
}
public
static
String
urlDecode
(
String
value
)
{
if
(
value
==
null
)
{
return
""
;
}
try
{
return
URLDecoder
.
decode
(
value
,
GlobalAuthUtil
.
DEFAULT_ENCODING
);
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
AuthException
(
"Failed To Decode Uri"
,
e
);
}
}
public
static
Map
<
String
,
String
>
parseStringToMap
(
String
accessTokenStr
)
{
Map
<
String
,
String
>
res
=
new
HashMap
<>();
if
(
accessTokenStr
.
contains
(
"&"
))
{
String
[]
fields
=
accessTokenStr
.
split
(
"&"
);
for
(
String
field
:
fields
)
{
if
(
field
.
contains
(
"="
))
{
String
[]
keyValue
=
field
.
split
(
"="
);
res
.
put
(
GlobalAuthUtil
.
urlDecode
(
keyValue
[
0
]),
keyValue
.
length
==
2
?
GlobalAuthUtil
.
urlDecode
(
keyValue
[
1
])
:
null
);
}
}
}
return
res
;
}
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
ea1aa53a
...
...
@@ -16,7 +16,7 @@ import java.text.MessageFormat;
public
class
UrlBuilder
{
private
static
final
String
GITHUB_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}"
;
private
static
final
String
GITHUB_USER_INFO_PATTERN
=
"{0}?{1}"
;
private
static
final
String
GITHUB_USER_INFO_PATTERN
=
"{0}?
access_token=
{1}"
;
private
static
final
String
GITHUB_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&state=1&redirect_uri={2}"
;
private
static
final
String
WEIBO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录