Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
dce2bd1e
J
JustAuth
项目概览
justauth
/
JustAuth
大约 1 年 前同步成功
通知
379
Star
15210
Fork
2709
代码
文件
提交
分支
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,发现更多精彩内容 >>
未验证
提交
dce2bd1e
编写于
12月 09, 2020
作者:
智布道
👁
提交者:
GitHub
12月 09, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #101 from zwzch/dev
支持喜马拉雅登录
上级
442332be
bc309714
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
187 addition
and
1 deletion
+187
-1
src/main/java/me/zhyd/oauth/config/AuthConfig.java
src/main/java/me/zhyd/oauth/config/AuthConfig.java
+14
-0
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
+25
-1
src/main/java/me/zhyd/oauth/request/AuthXmlyRequest.java
src/main/java/me/zhyd/oauth/request/AuthXmlyRequest.java
+121
-0
src/main/java/me/zhyd/oauth/utils/GlobalAuthUtils.java
src/main/java/me/zhyd/oauth/utils/GlobalAuthUtils.java
+27
-0
未找到文件。
src/main/java/me/zhyd/oauth/config/AuthConfig.java
浏览文件 @
dce2bd1e
...
...
@@ -110,4 +110,18 @@ public class AuthConfig {
* @since 1.15.7
*/
private
List
<
String
>
scopes
;
/**
* 设备ID, 设备唯一标识ID
*
* @since 1.15.8
*/
private
String
deviceId
;
/**
* 喜马拉雅:客户端包名,如果client_os_type为1或2时必填。对Android客户端是包名,对IOS客户端是Bundle ID
*
* @since 1.15.8
*/
private
String
packId
;
}
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
浏览文件 @
dce2bd1e
...
...
@@ -786,6 +786,30 @@ public enum AuthDefaultSource implements AuthSource {
public
String
refresh
()
{
return
"https://oauth.aliyun.com/v1/token"
;
}
},
/**
* 喜马拉雅
*/
XMLY
{
@Override
public
String
authorize
()
{
return
"https://api.ximalaya.com/oauth2/js/authorize"
;
}
@Override
public
String
accessToken
()
{
return
"https://api.ximalaya.com/oauth2/v2/access_token"
;
}
@Override
public
String
userInfo
()
{
return
"https://api.ximalaya.com/profile/user_info"
;
}
@Override
public
String
refresh
()
{
return
"https://oauth.aliyun.com/v1/token"
;
}
}
}
src/main/java/me/zhyd/oauth/request/AuthXmlyRequest.java
0 → 100644
浏览文件 @
dce2bd1e
package
me.zhyd.oauth.request
;
import
com.alibaba.fastjson.JSONObject
;
import
com.xkcoding.http.HttpUtil
;
import
me.zhyd.oauth.cache.AuthStateCache
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthDefaultSource
;
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.utils.GlobalAuthUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.TreeMap
;
/**
* 喜马拉雅登录
*
* @author zwzch (zwzch4j@gmail.com)
* @since 1.15.8
*/
public
class
AuthXmlyRequest
extends
AuthDefaultRequest
{
public
AuthXmlyRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthDefaultSource
.
XMLY
);
}
public
AuthXmlyRequest
(
AuthConfig
config
,
AuthStateCache
authStateCache
)
{
super
(
config
,
AuthDefaultSource
.
XMLY
,
authStateCache
);
}
/**
* 获取access token
*
* @param authCallback 授权成功后的回调参数
* @return token
* @see AuthDefaultRequest#authorize(String)
*/
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
Map
<
String
,
String
>
map
=
new
HashMap
<>(
6
);
map
.
put
(
"code"
,
authCallback
.
getCode
());
map
.
put
(
"client_id"
,
config
.
getClientId
());
map
.
put
(
"client_secret"
,
config
.
getClientSecret
());
map
.
put
(
"device_id"
,
config
.
getDeviceId
());
map
.
put
(
"grant_type"
,
"authorization_code"
);
map
.
put
(
"redirect_uri"
,
config
.
getRedirectUri
());
String
response
=
HttpUtil
.
post
(
source
.
accessToken
(),
map
,
true
);
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
response
);
this
.
checkResponse
(
accessTokenObject
);
return
AuthToken
.
builder
()
.
accessToken
(
accessTokenObject
.
getString
(
"access_token"
))
.
refreshToken
(
accessTokenObject
.
getString
(
"refresh_token"
))
.
expireIn
(
accessTokenObject
.
getIntValue
(
"expires_in"
))
.
uid
(
accessTokenObject
.
getString
(
"uid"
))
.
build
();
}
/**
* 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
*
* @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
* @since 1.15.8
*/
@Override
public
String
authorize
(
String
state
)
{
return
UrlBuilder
.
fromBaseUrl
(
source
.
authorize
())
.
queryParam
(
"response_type"
,
"code"
)
.
queryParam
(
"client_id"
,
config
.
getClientId
())
.
queryParam
(
"redirect_uri"
,
config
.
getRedirectUri
())
.
queryParam
(
"state"
,
getRealState
(
state
))
.
queryParam
(
"client_os_type"
,
"3"
)
.
queryParam
(
"device_id"
,
config
.
getDeviceId
())
.
build
();
}
/**
* 使用token换取用户信息
*
* @param authToken token信息
* @return 用户信息
* @see AuthDefaultRequest#getAccessToken(AuthCallback)
*/
@Override
public
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
Map
<
String
,
String
>
map
=
new
TreeMap
<>();
map
.
put
(
"app_key"
,
config
.
getClientId
());
map
.
put
(
"client_os_type"
,
"2"
);
map
.
put
(
"device_id"
,
config
.
getDeviceId
());
map
.
put
(
"pack_id"
,
config
.
getPackId
());
map
.
put
(
"access_token"
,
authToken
.
getAccessToken
());
map
.
put
(
"sig"
,
GlobalAuthUtils
.
generateXmlySignature
(
map
,
config
.
getClientSecret
()));
String
rawUserInfo
=
HttpUtil
.
get
(
source
.
userInfo
(),
map
,
false
);
JSONObject
object
=
JSONObject
.
parseObject
(
rawUserInfo
);
checkResponse
(
object
);
return
AuthUser
.
builder
()
.
uuid
(
object
.
getString
(
"id"
))
.
nickname
(
object
.
getString
(
"nickname"
))
.
avatar
(
object
.
getString
(
"avatar_url"
))
.
rawUserInfo
(
object
)
.
source
(
source
.
toString
())
.
token
(
authToken
)
.
gender
(
AuthUserGender
.
UNKNOWN
)
.
build
();
}
/**
* 校验响应结果
*
* @param object 接口返回的结果
*/
private
void
checkResponse
(
JSONObject
object
){
if
(
object
.
containsKey
(
"errcode"
))
{
throw
new
AuthException
(
object
.
getIntValue
(
"error_no"
),
object
.
getString
(
"error_desc"
));
}
}
}
src/main/java/me/zhyd/oauth/utils/GlobalAuthUtils.java
浏览文件 @
dce2bd1e
...
...
@@ -220,6 +220,33 @@ public class GlobalAuthUtils {
return
new
String
(
Base64Utils
.
encode
(
signature
,
false
));
}
/**
* 喜马拉雅签名算法
* https://open.ximalaya.com/doc/detailApi?categoryId=6&articleId=69
*
* @param params 加密参数
* @param clientSecret 平台应用的授权key
* @return Signature
*/
public
static
String
generateXmlySignature
(
Map
<
String
,
String
>
params
,
String
clientSecret
)
{
TreeMap
<
String
,
String
>
map
=
new
TreeMap
<>(
params
);
String
baseStr
=
Base64Utils
.
encode
(
parseMapToString
(
map
,
false
));
byte
[]
sign
=
sign
(
clientSecret
.
getBytes
(
DEFAULT_ENCODING
),
baseStr
.
getBytes
(
DEFAULT_ENCODING
),
HMAC_SHA1
);
MessageDigest
md5
=
null
;
StringBuilder
builder
=
null
;
try
{
builder
=
new
StringBuilder
();
md5
=
MessageDigest
.
getInstance
(
"MD5"
);
md5
.
update
(
sign
);
byte
[]
byteData
=
md5
.
digest
();
for
(
byte
byteDatum
:
byteData
)
{
builder
.
append
(
Integer
.
toString
((
byteDatum
&
0xff
)
+
0x100
,
16
).
substring
(
1
));
}
}
catch
(
Exception
ignored
)
{
}
return
null
==
builder
?
""
:
builder
.
toString
();
}
/**
* 生成饿了么请求的Signature
* <p>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录