Skip to content

Commit 527d97d

Browse files
committed
added consent request builder and redirect in error.jsp views file
1 parent f64cf23 commit 527d97d

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎src/main/java/com/docusign/security/jwt/JWTOAuth2RestTemplate.java‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import com.docusign.esign.client.auth.OAuth;
66
import com.docusign.esign.client.auth.OAuth.OAuthToken;
77
import com.docusign.exception.LauncherException;
8+
89
import java.io.IOException;
910
import java.util.ArrayList;
1011
import java.util.Date;
1112
import java.util.List;
13+
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
1416
import org.springframework.security.oauth2.client.OAuth2ClientContext;
@@ -17,6 +19,7 @@
1719
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
1820
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
1921
import org.springframework.security.oauth2.common.OAuth2AccessToken;
22+
import org.springframework.web.servlet.view.RedirectView;
2023

2124
public class JWTOAuth2RestTemplate extends OAuth2RestTemplate {
2225

@@ -44,16 +47,20 @@ public OAuth2AccessToken getAccessToken() throws UserRedirectRequiredException {
4447
context.setAccessToken(accessToken);
4548
} catch (Exception e) {
4649
context.setAccessToken(null);
47-
throw e;
50+
try {
51+
throw e;
52+
} catch (LauncherException ex) {
53+
ex.printStackTrace();
54+
}
4855
}
4956
}
5057
return accessToken;
5158
}
5259

53-
private OAuth2AccessToken accessToken() {
60+
private OAuth2AccessToken accessToken() throws LauncherException {
5461
logger.info("Fetching an access token via JWT grant...");
5562
List<String> scopes = new ArrayList<>();
56-
// Only signature scope is needed. Impersonation scope is implied.
63+
// Only signature scope is needed. Impersonation scope is implied.
5764
scopes.add(OAuth.Scope_SIGNATURE);
5865
byte[] privateKeyBytes;
5966
try {
@@ -71,11 +78,32 @@ private OAuth2AccessToken accessToken() {
7178
privateKeyBytes,
7279
TOKEN_EXPIRATION_IN_SECONDS);
7380
} catch (IOException | ApiException e) {
81+
// Special handling for consent_required
82+
String message = e.getMessage();
83+
String consent_url = "";
84+
if (message != null && message.contains("consent_required")) {
85+
consent_url = String.format("https://%s/oauth/auth?prompt=login&response_type=code&scope=%s" +
86+
"&client_id=%s" +
87+
"&redirect_uri=%s",
88+
resource.getBaseUrl(),
89+
"signature%20impersonation",
90+
resource.getClientId(),
91+
"http://localhost:8080/login%26type%3Djwt");
92+
System.err.println("\nC O N S E N T R E Q U I R E D" +
93+
"\nAsk the user who will be impersonated to run the following url: " +
94+
"\n" + consent_url +
95+
"\n\nIt will ask the user to login and to approve access by your application." +
96+
"\nAlternatively, an Administrator can use Organization Administration to" +
97+
"\npre-approve one or more users.");
98+
throw new AccessTokenRequiredException(consent_url, resource);
99+
}
100+
74101
throw new AccessTokenRequiredException(e.getMessage(), resource);
102+
75103
}
76104
apiClient.setAccessToken(oAuthToken.getAccessToken(), oAuthToken.getExpiresIn());
77105
logger.info("Done. Continuing...");
78-
return convert(oAuthToken);
106+
return convert(oAuthToken);
79107
}
80108

81109
private static OAuth2AccessToken convert(OAuthToken oAuthToken) {

‎src/main/webapp/WEB-INF/templates/views/error.jsp‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
23
<jsp:include page="./partials/head.jsp"/>
34

4-
<p><strong>Example page failure.</strong></p>
55

6-
<p><em>Error information: </em>${error}</p>
76

8-
<p>Something wrong with your account credentials.</p>
7+
<c:set var="message" value='${requestScope["message"]}' />
8+
<c:if test='${fn:contains(message, "docusign.com/oauth/auth") }'>
9+
10+
<c:redirect url="${message}" />
11+
12+
</c:if>
13+
14+
15+
<p><strong>Oops, we have encountered a problem.</strong></p>
16+
17+
<p><em>Error information: </em>${message}</p>
18+
19+
<p>Please check your account configuration. If you are unable to resolve the problem, file an issue on github.</p>
920

1021

1122
<p><a href="/">Continue</a></p>

0 commit comments

Comments
 (0)