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
8b82e539
Commit
8b82e539
authored
Jul 24, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
c
parent
c95b8117
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
167 additions
and
41 deletions
+167
-41
OAuthApi.java
src/main/java/com/egolm/sso/api/OAuthApi.java
+101
-41
OAuthApiException.java
src/main/java/com/egolm/sso/api/OAuthApiException.java
+15
-0
StringUtil.java
src/main/java/com/egolm/sso/api/StringUtil.java
+51
-0
No files found.
src/main/java/com/egolm/sso/api/OAuthApi.java
View file @
8b82e539
package
com
.
egolm
.
sso
.
api
;
package
com
.
egolm
.
sso
.
api
;
import
java.
io.IOException
;
import
java.
util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
org.apache.oltu.oauth2.client.OAuthClient
;
import
org.apache.oltu.oauth2.client.OAuthClient
;
import
org.apache.oltu.oauth2.client.URLConnectionClient
;
import
org.apache.oltu.oauth2.client.URLConnectionClient
;
...
@@ -23,7 +24,10 @@ public interface OAuthApi {
...
@@ -23,7 +24,10 @@ public interface OAuthApi {
OAuthConfig
getOAuthConfig
();
OAuthConfig
getOAuthConfig
();
default
void
sendOAuthLoginRedirect
(
HttpServletResponse
response
)
throws
IOException
,
OAuthSystemException
{
final
String
OAUTH_TOKEN_SESSION
=
"OAUTH2_TOKEN_SESSION_KEY"
;
default
void
sendOAuthLoginRedirect
(
HttpServletResponse
response
)
{
try
{
OAuthConfig
config
=
this
.
getOAuthConfig
();
OAuthConfig
config
=
this
.
getOAuthConfig
();
AuthenticationRequestBuilder
builder
=
OAuthClientRequest
.
authorizationLocation
(
config
.
getAuthorizeUrl
());
AuthenticationRequestBuilder
builder
=
OAuthClientRequest
.
authorizationLocation
(
config
.
getAuthorizeUrl
());
builder
.
setResponseType
(
OAuth
.
OAUTH_CODE
);
builder
.
setResponseType
(
OAuth
.
OAUTH_CODE
);
...
@@ -32,9 +36,22 @@ public interface OAuthApi {
...
@@ -32,9 +36,22 @@ public interface OAuthApi {
builder
.
setScope
(
config
.
getClientScope
());
builder
.
setScope
(
config
.
getClientScope
());
OAuthClientRequest
oauthResponse
=
builder
.
buildQueryMessage
();
OAuthClientRequest
oauthResponse
=
builder
.
buildQueryMessage
();
response
.
sendRedirect
(
oauthResponse
.
getLocationUri
());
response
.
sendRedirect
(
oauthResponse
.
getLocationUri
());
}
catch
(
Exception
e
)
{
throw
new
OAuthApiException
(
e
.
getMessage
(),
e
);
}
}
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
(
HttpServletRequest
request
)
throws
OAuthProblemException
,
OAuthSystemException
{
try
{
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
if
(
token
==
null
)
{
OAuthConfig
config
=
this
.
getOAuthConfig
();
OAuthConfig
config
=
this
.
getOAuthConfig
();
OAuthAuthzResponse
oauthAuthzResponse
=
OAuthAuthzResponse
.
oauthCodeAuthzResponse
(
request
);
OAuthAuthzResponse
oauthAuthzResponse
=
OAuthAuthzResponse
.
oauthCodeAuthzResponse
(
request
);
String
code
=
oauthAuthzResponse
.
getCode
();
String
code
=
oauthAuthzResponse
.
getCode
();
...
@@ -52,25 +69,68 @@ public interface OAuthApi {
...
@@ -52,25 +69,68 @@ public interface OAuthApi {
Long
expiresIn
=
oAuthResponse
.
getExpiresIn
();
Long
expiresIn
=
oAuthResponse
.
getExpiresIn
();
String
idToken
=
oAuthResponse
.
getParam
(
"id_token"
);
String
idToken
=
oAuthResponse
.
getParam
(
"id_token"
);
String
tokenType
=
oAuthResponse
.
getTokenType
();
String
tokenType
=
oAuthResponse
.
getTokenType
();
OAuthToken
token
=
new
OAuthToken
(
accessToken
,
refreshToken
,
idToken
,
tokenType
,
expiresIn
);
token
=
new
OAuthToken
(
accessToken
,
refreshToken
,
idToken
,
tokenType
,
expiresIn
);
session
.
setAttribute
(
OAUTH_TOKEN_SESSION
,
token
);
}
return
token
;
return
token
;
}
catch
(
Exception
e
)
{
throw
new
OAuthApiException
(
e
.
getMessage
(),
e
);
}
}
default
OAuthToken
refreshOAuthToken
(
HttpServletRequest
request
)
{
try
{
HttpSession
session
=
request
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
OAuthConfig
config
=
this
.
getOAuthConfig
();
TokenRequestBuilder
builder
=
OAuthClientRequest
.
tokenLocation
(
config
.
getAccessTokenUrl
());
builder
.
setGrantType
(
GrantType
.
REFRESH_TOKEN
);
builder
.
setRefreshToken
(
token
.
getRefreshToken
());
builder
.
setClientId
(
config
.
getClientId
());
builder
.
setClientSecret
(
config
.
getClientSecret
());
OAuthClientRequest
oauthClientRequest
=
builder
.
buildQueryMessage
();
OAuthClient
oAuthClient
=
new
OAuthClient
(
new
URLConnectionClient
());
OAuthJSONAccessTokenResponse
oAuthResponse
=
oAuthClient
.
accessToken
(
oauthClientRequest
,
OAuth
.
HttpMethod
.
POST
);
String
accessToken
=
oAuthResponse
.
getAccessToken
();
String
refreshToken
=
oAuthResponse
.
getRefreshToken
();
Long
expiresIn
=
oAuthResponse
.
getExpiresIn
();
String
idToken
=
oAuthResponse
.
getParam
(
"id_token"
);
String
tokenType
=
oAuthResponse
.
getTokenType
();
OAuthToken
newToken
=
new
OAuthToken
(
accessToken
,
refreshToken
,
idToken
,
tokenType
,
expiresIn
);
session
.
setAttribute
(
OAUTH_TOKEN_SESSION
,
newToken
);
return
newToken
;
}
catch
(
Exception
e
)
{
throw
new
OAuthApiException
(
e
.
getMessage
(),
e
);
}
}
}
default
String
getResource
(
String
method
,
String
url
,
OAuthToken
token
)
throws
OAuthSystemException
,
OAuthProblemException
{
default
String
doRequest
(
String
method
,
String
url
,
HttpServletRequest
req
)
{
try
{
HttpSession
session
=
req
.
getSession
();
OAuthToken
token
=
(
OAuthToken
)
session
.
getAttribute
(
OAUTH_TOKEN_SESSION
);
Map
<
String
,
String
[]>
parameters
=
req
.
getParameterMap
();
OAuthClient
oAuthClient
=
new
OAuthClient
(
new
URLConnectionClient
());
OAuthClient
oAuthClient
=
new
OAuthClient
(
new
URLConnectionClient
());
String
accessToken
=
token
.
getAccessToken
();
String
accessToken
=
token
.
getAccessToken
();
OAuthClientRequest
request
=
new
OAuthBearerClientRequest
(
url
).
setAccessToken
(
accessToken
).
buildQueryMessage
();
OAuthClientRequest
request
=
new
OAuthBearerClientRequest
(
url
).
setAccessToken
(
accessToken
).
buildQueryMessage
();
String
body
=
StringUtil
.
toQueryString
(
parameters
);
request
.
setBody
(
body
);
OAuthResourceResponse
resourceResponse
=
oAuthClient
.
resource
(
request
,
method
,
OAuthResourceResponse
.
class
);
OAuthResourceResponse
resourceResponse
=
oAuthClient
.
resource
(
request
,
method
,
OAuthResourceResponse
.
class
);
String
resBody
=
resourceResponse
.
getBody
();
String
resBody
=
resourceResponse
.
getBody
();
return
resBody
;
return
resBody
;
}
catch
(
OAuthProblemException
e
)
{
this
.
refreshOAuthToken
(
req
);
return
doRequest
(
method
,
url
,
req
);
}
catch
(
Exception
e
)
{
throw
new
OAuthApiException
(
e
.
getMessage
(),
e
);
}
}
}
default
String
doGetResource
(
String
url
,
OAuthToken
token
)
throws
OAuthSystemException
,
OAuthProblemException
{
default
String
doGetResource
(
String
url
,
HttpServletRequest
req
)
{
return
this
.
getResource
(
"GET"
,
url
,
token
);
return
this
.
doRequest
(
"GET"
,
url
,
req
);
}
}
default
String
doPostResource
(
String
url
,
OAuthToken
token
)
throws
OAuthSystemException
,
OAuthProblemException
{
default
String
doPostResource
(
String
url
,
HttpServletRequest
req
)
{
return
this
.
getResource
(
"POST"
,
url
,
token
);
return
this
.
doRequest
(
"POST"
,
url
,
req
);
}
}
}
}
src/main/java/com/egolm/sso/api/OAuthApiException.java
0 → 100644
View file @
8b82e539
package
com
.
egolm
.
sso
.
api
;
public
class
OAuthApiException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
4300677142149830999L
;
public
OAuthApiException
(
String
message
)
{
super
(
message
);
}
public
OAuthApiException
(
String
message
,
Throwable
e
)
{
super
(
message
,
e
);
}
}
src/main/java/com/egolm/sso/api/StringUtil.java
0 → 100644
View file @
8b82e539
package
com
.
egolm
.
sso
.
api
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
public
class
StringUtil
{
public
static
String
toQueryString
(
Map
<?,
?>
parameters
)
throws
UnsupportedEncodingException
{
return
toQueryString
(
parameters
,
null
);
}
public
static
String
toQueryString
(
Map
<?,
?>
parameters
,
String
encode
)
throws
UnsupportedEncodingException
{
List
<
String
>
params
=
new
ArrayList
<
String
>();
if
(
parameters
!=
null
)
{
for
(
Object
key
:
parameters
.
keySet
())
{
Object
val
=
parameters
.
get
(
key
);
String
sKey
=
String
.
valueOf
(
key
);
Object
[]
sVals
=
(
val
==
null
?
null
:
(
val
instanceof
Object
[]
?
(
Object
[])
val
:
new
Object
[]
{
val
}));
if
(
sVals
!=
null
&&
sVals
.
length
>
0
)
{
for
(
Object
sVal
:
sVals
)
{
params
.
add
(
sKey
+
"="
+
(
sVal
==
null
?
""
:
URLEncoder
.
encode
(
String
.
valueOf
(
sVal
),
encode
==
null
?
"utf-8"
:
encode
)));
}
}
else
{
params
.
add
(
"sKey="
);
}
}
}
return
join
(
"&"
,
""
,
""
,
""
,
params
);
}
public
static
String
join
(
String
sign
,
String
before
,
String
after
,
String
def
,
List
<
String
>
strs
)
{
if
(
strs
==
null
||
strs
.
size
()
==
0
)
{
return
def
;
}
else
{
StringBuffer
sb
=
new
StringBuffer
();
Integer
size
=
strs
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
String
str
=
String
.
valueOf
(
strs
.
get
(
i
));
sb
.
append
((
i
==
0
&&
before
!=
null
)
?
before
:
""
).
append
(
str
==
null
?
""
:
str
).
append
(
i
<
size
-
1
?
(
sign
==
null
?
""
:
sign
)
:
""
).
append
((
i
==
size
-
1
&&
after
!=
null
)
?
after
:
""
);
}
return
String
.
valueOf
(
sb
);
}
}
}
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