Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
sso
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
sso
Commits
2499d0e4
Commit
2499d0e4
authored
Jul 24, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
8b82e539
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
26 deletions
+25
-26
OAuthApi.java
src/main/java/com/egolm/sso/api/OAuthApi.java
+25
-26
No files found.
src/main/java/com/egolm/sso/api/OAuthApi.java
View file @
2499d0e4
...
...
@@ -17,17 +17,28 @@ import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import
org.apache.oltu.oauth2.client.response.OAuthResourceResponse
;
import
org.apache.oltu.oauth2.common.OAuth
;
import
org.apache.oltu.oauth2.common.exception.OAuthProblemException
;
import
org.apache.oltu.oauth2.common.exception.OAuthSystemException
;
import
org.apache.oltu.oauth2.common.message.types.GrantType
;
public
interface
OAuthApi
{
OAuthConfig
getOAuthConfig
();
HttpServletRequest
getHttpServletRequest
();
HttpServletResponse
getHttpServletResponse
();
final
String
OAUTH_TOKEN_SESSION
=
"OAUTH2_TOKEN_SESSION_KEY"
;
default
boolean
isLogin
()
{
HttpServletRequest
request
=
this
.
getHttpServletRequest
();
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
return
token
!=
null
;
}
default
void
sendOAuthLoginRedirect
(
HttpServletResponse
response
)
{
default
void
sendOAuthLoginRedirect
()
{
try
{
HttpServletResponse
response
=
this
.
getHttpServletResponse
();
OAuthConfig
config
=
this
.
getOAuthConfig
();
AuthenticationRequestBuilder
builder
=
OAuthClientRequest
.
authorizationLocation
(
config
.
getAuthorizeUrl
());
builder
.
setResponseType
(
OAuth
.
OAUTH_CODE
);
...
...
@@ -41,14 +52,9 @@ public interface OAuthApi {
}
}
default
boolean
isLogin
(
HttpServletRequest
request
)
{
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
return
token
!=
null
;
}
default
OAuthToken
getOAuthToken
(
HttpServletRequest
request
)
throws
OAuthProblemException
,
OAuthSystemException
{
default
OAuthToken
getOAuthToken
()
{
try
{
HttpServletRequest
request
=
this
.
getHttpServletRequest
();
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
if
(
token
==
null
)
{
...
...
@@ -78,8 +84,9 @@ public interface OAuthApi {
}
}
default
OAuthToken
refreshOAuthToken
(
HttpServletRequest
request
)
{
default
OAuthToken
refreshOAuthToken
()
{
try
{
HttpServletRequest
request
=
this
.
getHttpServletRequest
();
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
OAuthConfig
config
=
this
.
getOAuthConfig
();
...
...
@@ -104,33 +111,25 @@ public interface OAuthApi {
}
}
default
String
doRequest
(
String
method
,
String
url
,
HttpServletRequest
req
)
{
default
String
doRequest
(
String
method
,
String
url
,
Map
<
String
,
Object
>
parameters
)
{
try
{
HttpSession
session
=
req
.
getSession
();
HttpServletRequest
request
=
this
.
getHttpServletRequest
();
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
Map
<
String
,
String
[]>
parameters
=
req
.
getParameterMap
();
OAuthClient
oAuthClient
=
new
OAuthClient
(
new
URLConnectionClient
());
String
accessToken
=
token
.
getAccessToken
();
OAuthClientRequest
r
equest
=
new
OAuthBearerClientRequest
(
url
).
setAccessToken
(
accessToken
).
buildQueryMessage
();
OAuthClientRequest
clientR
equest
=
new
OAuthBearerClientRequest
(
url
).
setAccessToken
(
accessToken
).
buildQueryMessage
();
String
body
=
StringUtil
.
toQueryString
(
parameters
);
r
equest
.
setBody
(
body
);
OAuthResourceResponse
resourceResponse
=
oAuthClient
.
resource
(
r
equest
,
method
,
OAuthResourceResponse
.
class
);
clientR
equest
.
setBody
(
body
);
OAuthResourceResponse
resourceResponse
=
oAuthClient
.
resource
(
clientR
equest
,
method
,
OAuthResourceResponse
.
class
);
String
resBody
=
resourceResponse
.
getBody
();
return
resBody
;
}
catch
(
OAuthProblemException
e
)
{
this
.
refreshOAuthToken
(
req
);
return
doRequest
(
method
,
url
,
req
);
this
.
refreshOAuthToken
();
return
doRequest
(
method
,
url
,
parameters
);
}
catch
(
Exception
e
)
{
throw
new
OAuthApiException
(
e
.
getMessage
(),
e
);
}
}
default
String
doGetResource
(
String
url
,
HttpServletRequest
req
)
{
return
this
.
doRequest
(
"GET"
,
url
,
req
);
}
default
String
doPostResource
(
String
url
,
HttpServletRequest
req
)
{
return
this
.
doRequest
(
"POST"
,
url
,
req
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment