Skip to content

Commit 1c0bd69

Browse files
committed
Added token endpoint implementation
1 parent e25e116 commit 1c0bd69

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed

core/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2Authorization.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121

2222
/**
2323
* @author Joe Grandja
24+
* @author Madhu Bhat
2425
*/
2526
public class OAuth2Authorization {
2627
private String registeredClientId;
2728
private String principalName;
2829
private OAuth2AccessToken accessToken;
2930
private Map<String, Object> attributes;
3031

32+
public final void setAccessToken(OAuth2AccessToken accessToken) {
33+
this.accessToken = accessToken;
34+
}
3135
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AccessTokenAuthenticationToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
/**
2727
* @author Joe Grandja
28+
* @author Madhu Bhat
2829
*/
2930
public class OAuth2AccessTokenAuthenticationToken extends AbstractAuthenticationToken {
3031
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@@ -49,4 +50,8 @@ public Object getCredentials() {
4950
public Object getPrincipal() {
5051
return null;
5152
}
53+
54+
public OAuth2AccessToken getAccessToken() {
55+
return accessToken;
56+
}
5257
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeAuthenticationToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* @author Joe Grandja
27+
* @author Madhu Bhat
2728
*/
2829
public class OAuth2AuthorizationCodeAuthenticationToken extends AbstractAuthenticationToken {
2930
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@@ -57,4 +58,8 @@ public Object getCredentials() {
5758
public Object getPrincipal() {
5859
return null;
5960
}
61+
62+
public String getCode() {
63+
return code;
64+
}
6065
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization.authentication;
17+
18+
import org.springframework.security.authentication.AuthenticationManager;
19+
import org.springframework.security.authentication.AuthenticationProvider;
20+
import org.springframework.security.core.Authentication;
21+
import org.springframework.security.core.AuthenticationException;
22+
23+
/**
24+
* @author Madhu Bhat
25+
*/
26+
public class OAuth2ClientAuthenticationManager implements AuthenticationManager {
27+
28+
private AuthenticationProvider authenticationProvider;
29+
30+
/**
31+
* Constructs an {@code OAuth2ClientAuthenticationManager} using
32+
* {@code OAuth2AuthorizationCodeAuthenticationProvider} as the default {@code AuthenticationProvider}
33+
* implementation.
34+
*/
35+
public OAuth2ClientAuthenticationManager() {
36+
this.authenticationProvider = new OAuth2AuthorizationCodeAuthenticationProvider();
37+
}
38+
39+
/**
40+
* Constructs an {@code OAuth2ClientAuthenticationManager} using the provided parameters.
41+
*
42+
* @param authenticationProvider the authorization service implementation
43+
*/
44+
public OAuth2ClientAuthenticationManager(AuthenticationProvider authenticationProvider) {
45+
this.authenticationProvider = authenticationProvider;
46+
}
47+
48+
@Override
49+
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
50+
return this.authenticationProvider.authenticate(authentication);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization.converter;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
20+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken;
21+
22+
import javax.servlet.http.HttpServletRequest;
23+
24+
/**
25+
* Converts from a {@link HttpServletRequest} to an {@link OAuth2AuthorizationCodeAuthenticationToken} that can be authenticated. The
26+
* converter does not validate the request but only performs a conversion.
27+
* @author Madhu Bhat
28+
*/
29+
public class OAuth2AuthorizationCodeAuthenticationTokenConverter implements Converter<HttpServletRequest, OAuth2AuthorizationCodeAuthenticationToken> {
30+
31+
@Override
32+
public OAuth2AuthorizationCodeAuthenticationToken convert(HttpServletRequest request) {
33+
return new OAuth2AuthorizationCodeAuthenticationToken(
34+
request.getParameter(OAuth2ParameterNames.CODE),
35+
request.getParameter(OAuth2ParameterNames.CLIENT_ID),
36+
request.getParameter(OAuth2ParameterNames.REDIRECT_URI)
37+
);
38+
}
39+
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenEndpointFilter.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,24 @@
1616
package org.springframework.security.oauth2.server.authorization.web;
1717

1818
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.http.HttpMethod;
1920
import org.springframework.security.authentication.AuthenticationManager;
20-
import org.springframework.security.core.Authentication;
21+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
22+
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
23+
import org.springframework.security.oauth2.core.OAuth2Error;
24+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
25+
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
26+
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
2127
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
28+
import org.springframework.security.oauth2.server.authorization.TokenType;
29+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken;
30+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken;
31+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationManager;
32+
import org.springframework.security.oauth2.server.authorization.converter.OAuth2AuthorizationCodeAuthenticationTokenConverter;
33+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
34+
import org.springframework.security.web.util.matcher.RequestMatcher;
35+
import org.springframework.util.Assert;
36+
import org.springframework.util.StringUtils;
2237
import org.springframework.web.filter.OncePerRequestFilter;
2338

2439
import javax.servlet.FilterChain;
@@ -29,16 +44,53 @@
2944

3045
/**
3146
* @author Joe Grandja
47+
* @author Madhu Bhat
3248
*/
3349
public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
34-
private Converter<HttpServletRequest, Authentication> authorizationGrantConverter;
50+
private final String DEFAULT_TOKEN_URI = "/oauth2/token";
51+
private Converter<HttpServletRequest, OAuth2AuthorizationCodeAuthenticationToken> authorizationGrantConverter;
3552
private AuthenticationManager authenticationManager;
3653
private OAuth2AuthorizationService authorizationService;
54+
private RequestMatcher uriMatcher;
55+
56+
/**
57+
* Constructs an {@code OAuth2TokenEndpointFilter} using the provided parameter.
58+
*
59+
* @param authorizationService the authorization service implementation
60+
*/
61+
public OAuth2TokenEndpointFilter(OAuth2AuthorizationService authorizationService) {
62+
Assert.notNull(authorizationService, "authorizationService cannot be null");
63+
this.authorizationGrantConverter = new OAuth2AuthorizationCodeAuthenticationTokenConverter();
64+
this.authenticationManager = new OAuth2ClientAuthenticationManager();
65+
this.uriMatcher = new AntPathRequestMatcher(DEFAULT_TOKEN_URI, HttpMethod.POST.name());
66+
this.authorizationService = authorizationService;
67+
}
3768

3869
@Override
3970
protected void doFilterInternal(HttpServletRequest request,
4071
HttpServletResponse response, FilterChain filterChain)
4172
throws ServletException, IOException {
73+
if (uriMatcher.matches(request)) {
74+
if (isValidAccessTokenRequest(request)) {
75+
OAuth2AuthorizationCodeAuthenticationToken authCodeAuthToken = authorizationGrantConverter.convert(request);
76+
OAuth2AccessTokenAuthenticationToken accessTokenAuthenticationToken =
77+
(OAuth2AccessTokenAuthenticationToken) authenticationManager.authenticate(authCodeAuthToken);
78+
OAuth2Authorization authorization = authorizationService
79+
.findByTokenAndTokenType(authCodeAuthToken.getCode(), TokenType.AUTHORIZATION_CODE);
80+
authorization.setAccessToken(accessTokenAuthenticationToken.getAccessToken());
81+
authorizationService.save(authorization);
82+
}
83+
}
84+
filterChain.doFilter(request, response);
85+
}
4286

87+
private boolean isValidAccessTokenRequest(HttpServletRequest request) {
88+
if (!AuthorizationGrantType.AUTHORIZATION_CODE.getValue().equals(request.getParameter(OAuth2ParameterNames.GRANT_TYPE))) {
89+
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE));
90+
} else if (StringUtils.isEmpty(request.getParameter(OAuth2ParameterNames.CODE))
91+
|| StringUtils.isEmpty(request.getParameter(OAuth2ParameterNames.REDIRECT_URI))) {
92+
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
93+
}
94+
return true;
4395
}
4496
}

0 commit comments

Comments
 (0)