Skip to content

Commit 0cea90b

Browse files
committed
removed the need to use /pkce redirect
1 parent 69dc5fb commit 0cea90b

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/main/java/com/docusign/WebSecurityConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
1111
import org.springframework.security.web.savedrequest.RequestCache;
1212

13+
import com.docusign.core.security.CustomAuthenticationFailureHandler;
14+
1315
@EnableWebSecurity
1416
public class WebSecurityConfig {
1517

@@ -40,7 +42,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4042
}
4143
})
4244
.requestCache().requestCache(requestCache()).and()
43-
.oauth2Login(Customizer.withDefaults())
45+
.oauth2Login(login -> login.failureHandler(new CustomAuthenticationFailureHandler()))
4446
.oauth2Client(Customizer.withDefaults())
4547
.logout(logout -> logout
4648
.logoutSuccessUrl("/"))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.docusign.core.security;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
10+
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
11+
12+
@Override
13+
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
14+
AuthenticationException exception) throws IOException, ServletException {
15+
String code = request.getParameter("code");
16+
String state = request.getParameter("state");
17+
18+
if (code != null) {
19+
response.sendRedirect("/pkce?code=" + code + "&state=" + state);
20+
} else {
21+
response.sendRedirect("/login?error=true");
22+
}
23+
}
24+
}

src/main/java/com/docusign/core/security/acg/ACGAuthenticationMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.docusign.esign.client.auth.OAuth;
1919

2020
public class ACGAuthenticationMethod {
21-
private static final String REDIRECT_URI = "/pkce";
21+
private static final String REDIRECT_URI = "/login/oauth2/code/acg";
2222
private static final String STATE = "random_state_string";
2323
private static String codeVerifier;
2424
private static String codeChallenge;

src/main/java/com/docusign/core/security/jwt/JWTAuthenticationMethod.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.web.servlet.view.RedirectView;
1111

1212
import java.io.IOException;
13+
import java.net.URLEncoder;
14+
import java.nio.charset.StandardCharsets;
1315
import java.nio.file.Files;
1416
import java.nio.file.Paths;
1517
import java.util.List;
@@ -20,7 +22,7 @@ public class JWTAuthenticationMethod {
2022

2123
public static final String REQUEST_CONSENT_LINK = "https://%s/oauth/auth?prompt=login&response_type=code&scope=%s&client_id=%s&redirect_uri=%s";
2224

23-
public static final String CONSENT_REDIRECT_URL = "http://localhost:8080/login/oauth2/code/jwt";
25+
public static final String CONSENT_REDIRECT_URL = "/login/oauth2/code/jwt";
2426

2527
private static final long TOKEN_EXPIRATION_IN_SECONDS = 3600;
2628

@@ -58,7 +60,8 @@ public RedirectView loginUsingJWT(
5860
configuration.getBaseURL(),
5961
consent_scopes,
6062
configuration.getUserId(),
61-
CONSENT_REDIRECT_URL);
63+
URLEncoder.encode(configuration.getAppUrl() + CONSENT_REDIRECT_URL,
64+
StandardCharsets.UTF_8));
6265

6366
System.err.println("\nC O N S E N T R E Q U I R E D" +
6467
"\nAsk the user who will be impersonated to run the following URL: " +

0 commit comments

Comments
 (0)