Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
My_csdo
JustAuth
提交
4f272348
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看板
提交
4f272348
编写于
7月 16, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
♻
从UrlBuilder中拆分github、google、linkedin和microsoft相关的URL
上级
ec31b7cb
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
264 addition
and
208 deletion
+264
-208
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
+15
-5
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
+15
-6
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
+18
-8
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
+2
-1
src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
...main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
+18
-8
src/main/java/me/zhyd/oauth/url/GithubUrlBuilder.java
src/main/java/me/zhyd/oauth/url/GithubUrlBuilder.java
+48
-0
src/main/java/me/zhyd/oauth/url/GoogleUrlBuilder.java
src/main/java/me/zhyd/oauth/url/GoogleUrlBuilder.java
+48
-0
src/main/java/me/zhyd/oauth/url/LinkedinUrlBuilder.java
src/main/java/me/zhyd/oauth/url/LinkedinUrlBuilder.java
+50
-0
src/main/java/me/zhyd/oauth/url/MicrosoftUrlBuilder.java
src/main/java/me/zhyd/oauth/url/MicrosoftUrlBuilder.java
+50
-0
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+0
-180
未找到文件。
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
浏览文件 @
4f272348
...
...
@@ -10,8 +10,11 @@ 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.GithubUrlBuilder
;
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.GlobalAuthUtil
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
java.util.Map
;
...
...
@@ -25,12 +28,15 @@ import java.util.Map;
public
class
AuthGithubRequest
extends
BaseAuthRequest
{
public
AuthGithubRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
GITHUB
);
super
(
config
,
AuthSource
.
GITHUB
,
new
GithubUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getGithubAccessTokenUrl
(
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
();
Map
<
String
,
String
>
res
=
GlobalAuthUtil
.
parseStringToMap
(
response
.
body
());
if
(
res
.
containsKey
(
"error"
))
{
...
...
@@ -42,7 +48,9 @@ public class AuthGithubRequest extends BaseAuthRequest {
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getAccessToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getGithubUserInfoUrl
(
accessToken
)).
execute
();
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
accessToken
(
accessToken
)
.
build
())).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthUser
.
builder
()
...
...
@@ -68,6 +76,8 @@ public class AuthGithubRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getGithubAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
}
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
浏览文件 @
4f272348
...
...
@@ -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.GoogleUrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthUserInfoEntity
;
/**
* Google登录
...
...
@@ -22,13 +25,15 @@ import me.zhyd.oauth.utils.UrlBuilder;
public
class
AuthGoogleRequest
extends
BaseAuthRequest
{
public
AuthGoogleRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
GOOGLE
);
super
(
config
,
AuthSource
.
GOOGLE
,
new
GoogleUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getGoogleAccessTokenUrl
(
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
());
...
...
@@ -49,7 +54,9 @@ public class AuthGoogleRequest extends BaseAuthRequest {
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getIdToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getGoogleUserInfoUrl
(
accessToken
)).
execute
();
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
accessToken
(
accessToken
)
.
build
())).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthUser
.
builder
()
...
...
@@ -72,6 +79,8 @@ public class AuthGoogleRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getGoogleAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
}
src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
浏览文件 @
4f272348
...
...
@@ -8,8 +8,12 @@ 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.url.LinkedinUrlBuilder
;
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
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
...
...
@@ -22,20 +26,22 @@ import me.zhyd.oauth.utils.UrlBuilder;
public
class
AuthLinkedinRequest
extends
BaseAuthRequest
{
public
AuthLinkedinRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
LINKEDIN
);
super
(
config
,
AuthSource
.
LINKEDIN
,
new
LinkedinUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getLinkedinAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
authCallback
.
getCode
(),
config
.
getRedirectUri
());
String
accessTokenUrl
=
this
.
urlBuilder
.
getAccessTokenUrl
(
AuthAccessTokenEntity
.
builder
()
.
config
(
config
)
.
code
(
authCallback
.
getCode
())
.
build
());
return
this
.
getToken
(
accessTokenUrl
);
}
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getAccessToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getLinkedinUserInfoUrl
(
))
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
().
build
()
))
.
header
(
"Host"
,
"api.linkedin.com"
)
.
header
(
"Connection"
,
"Keep-Alive"
)
.
header
(
"Authorization"
,
"Bearer "
+
accessToken
)
...
...
@@ -93,7 +99,9 @@ public class AuthLinkedinRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getLinkedinAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
private
String
getUserEmail
(
String
accessToken
)
{
...
...
@@ -128,8 +136,10 @@ public class AuthLinkedinRequest extends BaseAuthRequest {
if
(
StringUtils
.
isEmpty
(
oldToken
.
getRefreshToken
()))
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
String
refreshTokenUrl
=
UrlBuilder
.
getLinkedinRefreshUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
oldToken
.
getRefreshToken
());
String
refreshTokenUrl
=
this
.
urlBuilder
.
getRefreshUrl
(
AuthRefreshTokenEntity
.
builder
()
.
config
(
config
)
.
refreshToken
(
oldToken
.
getRefreshToken
())
.
build
());
return
AuthResponse
.
builder
()
.
code
(
ResponseStatus
.
SUCCESS
.
getCode
())
.
data
(
this
.
getToken
(
refreshTokenUrl
))
...
...
src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
浏览文件 @
4f272348
...
...
@@ -8,6 +8,7 @@ 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.url.MiUrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthRefreshTokenEntity
;
...
...
@@ -26,7 +27,7 @@ public class AuthMiRequest extends BaseAuthRequest {
private
static
final
String
PREFIX
=
"&&&START&&&"
;
public
AuthMiRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
MI
);
super
(
config
,
AuthSource
.
MI
,
new
MiUrlBuilder
()
);
}
@Override
...
...
src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
浏览文件 @
4f272348
...
...
@@ -8,7 +8,11 @@ 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.MicrosoftUrlBuilder
;
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.util.HashMap
;
import
java.util.Map
;
...
...
@@ -22,13 +26,15 @@ import java.util.Map;
*/
public
class
AuthMicrosoftRequest
extends
BaseAuthRequest
{
public
AuthMicrosoftRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
MICROSOFT
);
super
(
config
,
AuthSource
.
MICROSOFT
,
new
MicrosoftUrlBuilder
()
);
}
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getMicrosoftAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
authCallback
.
getCode
());
String
accessTokenUrl
=
this
.
urlBuilder
.
getAccessTokenUrl
(
AuthAccessTokenEntity
.
builder
()
.
config
(
config
)
.
code
(
authCallback
.
getCode
())
.
build
());
return
getToken
(
accessTokenUrl
);
}
...
...
@@ -72,7 +78,7 @@ public class AuthMicrosoftRequest extends BaseAuthRequest {
String
token
=
authToken
.
getAccessToken
();
String
tokenType
=
authToken
.
getTokenType
();
String
jwt
=
tokenType
+
" "
+
token
;
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getMicrosoftUserInfoUrl
(
))
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
().
build
()
))
.
header
(
"Authorization"
,
jwt
)
.
execute
();
String
userInfo
=
response
.
body
();
...
...
@@ -96,7 +102,9 @@ public class AuthMicrosoftRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getMicrosoftAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
/**
...
...
@@ -107,8 +115,10 @@ public class AuthMicrosoftRequest extends BaseAuthRequest {
*/
@Override
public
AuthResponse
refresh
(
AuthToken
authToken
)
{
String
refreshTokenUrl
=
UrlBuilder
.
getMicrosoftRefreshUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
authToken
.
getRefreshToken
());
String
refreshTokenUrl
=
this
.
urlBuilder
.
getRefreshUrl
(
AuthRefreshTokenEntity
.
builder
()
.
config
(
config
)
.
refreshToken
(
authToken
.
getRefreshToken
())
.
build
());
return
AuthResponse
.
builder
().
code
(
ResponseStatus
.
SUCCESS
.
getCode
()).
data
(
getToken
(
refreshTokenUrl
)).
build
();
}
...
...
src/main/java/me/zhyd/oauth/url/GithubUrlBuilder.java
0 → 100644
浏览文件 @
4f272348
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
;
/**
* Github相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
GithubUrlBuilder
extends
AbstractUrlBuilder
{
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}?access_token={1}"
;
private
static
final
String
GITHUB_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&state={3}"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
GITHUB_ACCESS_TOKEN_PATTERN
,
AuthSource
.
GITHUB
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
(),
config
.
getRedirectUri
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
GITHUB_USER_INFO_PATTERN
,
AuthSource
.
GITHUB
.
userInfo
(),
userInfoEntity
.
getAccessToken
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
GITHUB_AUTHORIZE_PATTERN
,
AuthSource
.
GITHUB
.
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/GoogleUrlBuilder.java
0 → 100644
浏览文件 @
4f272348
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
;
/**
* Google相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
GoogleUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
GOOGLE_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&scope=openid%20email%20profile&redirect_uri={2}&state={3}"
;
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}"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
GOOGLE_ACCESS_TOKEN_PATTERN
,
AuthSource
.
GOOGLE
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
(),
config
.
getRedirectUri
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
GOOGLE_USER_INFO_PATTERN
,
AuthSource
.
GOOGLE
.
userInfo
(),
userInfoEntity
.
getAccessToken
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
GOOGLE_AUTHORIZE_PATTERN
,
AuthSource
.
GOOGLE
.
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/LinkedinUrlBuilder.java
0 → 100644
浏览文件 @
4f272348
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
;
/**
* Microsoft相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
LinkedinUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
LINKEDIN_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope=r_liteprofile%20r_emailaddress%20w_member_social"
;
private
static
final
String
LINKEDIN_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
LINKEDIN_USER_INFO_PATTERN
=
"{0}?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))"
;
private
static
final
String
LINKEDIN_REFRESH_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
LINKEDIN_ACCESS_TOKEN_PATTERN
,
AuthSource
.
LINKEDIN
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
(),
config
.
getRedirectUri
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
LINKEDIN_USER_INFO_PATTERN
,
AuthSource
.
LINKEDIN
.
userInfo
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
LINKEDIN_AUTHORIZE_PATTERN
,
AuthSource
.
LINKEDIN
.
authorize
(),
config
.
getClientId
(),
config
.
getRedirectUri
(),
this
.
getRealState
(
config
.
getState
()));
}
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
AuthConfig
config
=
refreshTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
LINKEDIN_REFRESH_TOKEN_PATTERN
,
AuthSource
.
LINKEDIN
.
refresh
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
refreshTokenEntity
.
getRefreshToken
());
}
@Override
public
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
)
{
return
null
;
}
}
src/main/java/me/zhyd/oauth/url/MicrosoftUrlBuilder.java
0 → 100644
浏览文件 @
4f272348
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
;
/**
* Microsoft相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
MicrosoftUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
MICROSOFT_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&response_mode=query&scope=offline_access%20user.read%20mail.read&state={3}"
;
private
static
final
String
MICROSOFT_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&scope=user.read%20mail.read&redirect_uri={3}&code={4}&grant_type=authorization_code"
;
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"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
MICROSOFT_ACCESS_TOKEN_PATTERN
,
AuthSource
.
MICROSOFT
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
accessTokenEntity
.
getCode
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
MICROSOFT_USER_INFO_PATTERN
,
AuthSource
.
MICROSOFT
.
userInfo
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
MICROSOFT_AUTHORIZE_PATTERN
,
AuthSource
.
MICROSOFT
.
authorize
(),
config
.
getClientId
(),
config
.
getRedirectUri
(),
this
.
getRealState
(
config
.
getState
()));
}
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
AuthConfig
config
=
refreshTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
MICROSOFT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
MICROSOFT
.
refresh
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
config
.
getRedirectUri
(),
refreshTokenEntity
.
getRefreshToken
());
}
@Override
public
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
)
{
return
null
;
}
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
4f272348
...
...
@@ -13,13 +13,6 @@ 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}?access_token={1}"
;
private
static
final
String
GITHUB_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&state={3}"
;
private
static
final
String
GOOGLE_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&scope=openid%20email%20profile&redirect_uri={2}&state={3}"
;
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
GITEE_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
...
...
@@ -54,16 +47,6 @@ public class UrlBuilder {
private
static
final
String
DOUYIN_USER_INFO_PATTERN
=
"{0}?access_token={1}&open_id={2}"
;
private
static
final
String
DOUYIN_REFRESH_TOKEN_PATTERN
=
"{0}?client_key={1}&refresh_token={2}&grant_type=refresh_token"
;
private
static
final
String
LINKEDIN_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope=r_liteprofile%20r_emailaddress%20w_member_social"
;
private
static
final
String
LINKEDIN_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
LINKEDIN_USER_INFO_PATTERN
=
"{0}?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))"
;
private
static
final
String
LINKEDIN_REFRESH_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token"
;
private
static
final
String
MICROSOFT_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&response_mode=query&scope=offline_access%20user.read%20mail.read&state={3}"
;
private
static
final
String
MICROSOFT_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&scope=user.read%20mail.read&redirect_uri={3}&code={4}&grant_type=authorization_code"
;
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"
;
/**
...
...
@@ -75,41 +58,6 @@ public class UrlBuilder {
return
StringUtils
.
isEmpty
(
state
)
?
String
.
valueOf
(
System
.
currentTimeMillis
())
:
state
;
}
/**
* 获取githubtoken的接口地址
*
* @param clientId github 应用的Client ID
* @param clientSecret github 应用的Client Secret
* @param code github 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getGithubAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
GITHUB_ACCESS_TOKEN_PATTERN
,
AuthSource
.
GITHUB
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取github用户详情的接口地址
*
* @param token github 应用的token
* @return full url
*/
public
static
String
getGithubUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GITHUB_USER_INFO_PATTERN
,
AuthSource
.
GITHUB
.
userInfo
(),
token
);
}
/**
* 获取github授权地址
*
* @param clientId github 应用的Client ID
* @param redirectUrl github 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getGithubAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
GITHUB_AUTHORIZE_PATTERN
,
AuthSource
.
GITHUB
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取gitee token的接口地址
*
...
...
@@ -295,41 +243,6 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
ALIPAY_AUTHORIZE_PATTERN
,
AuthSource
.
ALIPAY
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取Google授权地址
*
* @param clientId google 应用的Client ID
* @param redirectUrl google 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getGoogleAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
GOOGLE_AUTHORIZE_PATTERN
,
AuthSource
.
GOOGLE
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取Google token的接口地址
*
* @param clientId google 应用的Client ID
* @param clientSecret google 应用的Client Secret
* @param code google 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getGoogleAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
GOOGLE_ACCESS_TOKEN_PATTERN
,
AuthSource
.
GOOGLE
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取Google用户详情的接口地址
*
* @param token google 应用的token
* @return full url
*/
public
static
String
getGoogleUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GOOGLE_USER_INFO_PATTERN
,
AuthSource
.
GOOGLE
.
userInfo
(),
token
);
}
/**
* 获取Facebook授权地址
*
...
...
@@ -411,97 +324,4 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
DOUYIN_REFRESH_TOKEN_PATTERN
,
AuthSource
.
DOUYIN
.
refresh
(),
clientId
,
refreshToken
);
}
/**
* 获取Linkedin授权地址
*
* @param clientId Linkedin 应用的Client ID
* @param redirectUrl Linkedin 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getLinkedinAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
LINKEDIN_AUTHORIZE_PATTERN
,
AuthSource
.
LINKEDIN
.
authorize
(),
clientId
,
redirectUrl
,
state
);
}
/**
* 获取Linkedin token的接口地址
*
* @param clientId Linkedin 应用的Client ID
* @param clientSecret Linkedin 应用的Client Secret
* @param code Linkedin 授权前的code,用来换token
* @param redirectUrl Linkedin 应用授权成功后的回调地址
* @return full url
*/
public
static
String
getLinkedinAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
LINKEDIN_ACCESS_TOKEN_PATTERN
,
AuthSource
.
LINKEDIN
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUrl
);
}
/**
* 获取Linkedin用户详情的接口地址
*
* @return full url
*/
public
static
String
getLinkedinUserInfoUrl
()
{
return
MessageFormat
.
format
(
LINKEDIN_USER_INFO_PATTERN
,
AuthSource
.
LINKEDIN
.
userInfo
());
}
/**
* 获取Linkedin 刷新令牌 地址
*
* @param clientId Linkedin 应用的client_key
* @param clientSecret Linkedin 应用的Client Secret
* @param refreshToken Linkedin 应用返回的refresh_token
* @return full url
*/
public
static
String
getLinkedinRefreshUrl
(
String
clientId
,
String
clientSecret
,
String
refreshToken
)
{
return
MessageFormat
.
format
(
LINKEDIN_REFRESH_TOKEN_PATTERN
,
AuthSource
.
LINKEDIN
.
refresh
(),
clientId
,
clientSecret
,
refreshToken
);
}
/**
* 获取微软授权地址
*
* @param clientId 微软 应用的Client ID
* @param redirectUrl 微软 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getMicrosoftAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
MICROSOFT_AUTHORIZE_PATTERN
,
AuthSource
.
MICROSOFT
.
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
getMicrosoftAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
redirectUrl
,
String
code
)
{
return
MessageFormat
.
format
(
MICROSOFT_ACCESS_TOKEN_PATTERN
,
AuthSource
.
MICROSOFT
.
accessToken
(),
clientId
,
clientSecret
,
redirectUrl
,
code
);
}
/**
* 获取微软用户详情的接口地址
*
* @return full url
*/
public
static
String
getMicrosoftUserInfoUrl
()
{
return
MessageFormat
.
format
(
MICROSOFT_USER_INFO_PATTERN
,
AuthSource
.
MICROSOFT
.
userInfo
());
}
/**
* 获取微软 刷新令牌 地址
*
* @param clientId 微软 应用的client_key
* @param clientSecret 微软 应用的Client Secret
* @param redirectUrl 微软 应用授权成功后的回调地址
* @param refreshToken 微软 应用返回的refresh_token
* @return full url
*/
public
static
String
getMicrosoftRefreshUrl
(
String
clientId
,
String
clientSecret
,
String
redirectUrl
,
String
refreshToken
)
{
return
MessageFormat
.
format
(
MICROSOFT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
MICROSOFT
.
refresh
(),
clientId
,
clientSecret
,
redirectUrl
,
refreshToken
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录