7
7
import com .docusign .core .model .AuthType ;
8
8
import com .docusign .core .model .Session ;
9
9
import com .docusign .core .model .User ;
10
+ import com .docusign .core .security .acg .ACGAuthenticationMethod ;
10
11
import com .docusign .core .security .jwt .JWTAuthenticationMethod ;
11
12
import org .apache .commons .lang3 .StringUtils ;
12
13
import org .springframework .beans .factory .annotation .Autowired ;
@@ -94,7 +95,7 @@ public String index(ModelMap model, HttpServletResponse response) throws Excepti
94
95
}
95
96
96
97
if (config .getQuickstart ().equals ("true" ) && config .getSelectedApiIndex ().equals (ApiIndex .ESIGNATURE ) &&
97
- !(SecurityContextHolder .getContext ().getAuthentication () instanceof OAuth2AuthenticationToken )) {
98
+ !(SecurityContextHolder .getContext ().getAuthentication () instanceof OAuth2AuthenticationToken )) {
98
99
String site = ApiIndex .ESIGNATURE .getPathOfFirstExample ();
99
100
response .setStatus (response .SC_MOVED_TEMPORARILY );
100
101
response .setHeader (LOCATION_HEADER , site );
@@ -112,7 +113,8 @@ public String index(ModelMap model, HttpServletResponse response) throws Excepti
112
113
}
113
114
114
115
@ GetMapping (path = "/ds/mustAuthenticate" )
115
- public ModelAndView mustAuthenticateController (ModelMap model , HttpServletRequest req , HttpServletResponse resp ) throws IOException {
116
+ public ModelAndView mustAuthenticateController (ModelMap model , HttpServletRequest req , HttpServletResponse resp )
117
+ throws IOException {
116
118
model .addAttribute (LAUNCHER_TEXTS , config .getCodeExamplesText ().SupportingTexts );
117
119
model .addAttribute (ATTR_TITLE , config .getCodeExamplesText ().SupportingTexts .LoginPage .LoginButton );
118
120
@@ -125,7 +127,8 @@ public ModelAndView mustAuthenticateController(ModelMap model, HttpServletReques
125
127
return new ModelAndView (new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL ));
126
128
}
127
129
128
- boolean isRedirectToMonitor = redirectURL .toLowerCase ().contains ("/m" ) && !redirectURL .toLowerCase ().contains ("/mae" );
130
+ boolean isRedirectToMonitor = redirectURL .toLowerCase ().contains ("/m" ) &&
131
+ !redirectURL .toLowerCase ().contains ("/mae" );
129
132
if (session .isRefreshToken () || config .getQuickstart ().equals ("true" )) {
130
133
config .setQuickstart ("false" );
131
134
@@ -148,32 +151,52 @@ private ModelAndView checkForMonitorRedirects(String redirectURL) {
148
151
return new ModelAndView (new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL ));
149
152
}
150
153
154
+ @ GetMapping ("/pkce" )
155
+ public RedirectView pkce (String code , String state , HttpServletRequest req , HttpServletResponse resp )
156
+ throws Exception {
157
+ String redirectURL = getRedirectURLForJWTAuthentication (req , resp );
158
+ RedirectView redirect ;
159
+ try {
160
+ redirect = new ACGAuthenticationMethod ().exchangeCodeForToken (code , config , session , redirectURL );
161
+ } catch (Exception e ) {
162
+ redirect = getRedirectView (AuthType .AGC );
163
+ this .session .setIsPKCEWorking (false );
164
+ }
165
+
166
+ return redirect ;
167
+ }
168
+
151
169
@ PostMapping ("/ds/authenticate" )
152
- public RedirectView authenticate (ModelMap model , @ RequestBody MultiValueMap <String , String > formParams , HttpServletRequest req , HttpServletResponse resp ) throws IOException {
170
+ public RedirectView authenticate (ModelMap model , @ RequestBody MultiValueMap <String , String > formParams ,
171
+ HttpServletRequest req , HttpServletResponse resp ) throws Exception {
153
172
if (!formParams .containsKey ("selectAuthType" )) {
154
173
model .addAttribute ("message" , "Select option with selectAuthType name must be provided." );
155
174
return new RedirectView ("pages/error" );
156
175
}
157
176
158
177
String redirectURL = getRedirectURLForJWTAuthentication (req , resp );
159
178
160
- List <String > selectAuthTypeObject = formParams .get ("selectAuthType" );
179
+ List <String > selectAuthTypeObject = formParams .get ("selectAuthType" );
161
180
AuthType authTypeSelected = AuthType .valueOf (selectAuthTypeObject .get (0 ));
162
181
163
182
if (authTypeSelected .equals (AuthType .JWT )) {
164
183
this .session .setAuthTypeSelected (AuthType .JWT );
165
184
return new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL );
166
185
} else {
167
186
this .session .setAuthTypeSelected (AuthType .AGC );
168
- return getRedirectView (authTypeSelected );
187
+ if (this .session .getIsPKCEWorking ()) {
188
+ return new ACGAuthenticationMethod ().initiateAuthorization (config );
189
+ } else {
190
+ return getRedirectView (authTypeSelected );
191
+ }
169
192
}
170
193
}
171
194
172
195
private String getRedirectURLForJWTAuthentication (HttpServletRequest req , HttpServletResponse resp ) {
173
196
SavedRequest savedRequest = requestCache .getRequest (req , resp );
174
197
175
- String [] examplesCodes = new String []{
176
- ApiIndex .CLICK .getExamplesPathCode (),
198
+ String [] examplesCodes = new String [] {
199
+ ApiIndex .CLICK .getExamplesPathCode (),
177
200
ApiIndex .ESIGNATURE .getExamplesPathCode (),
178
201
ApiIndex .MONITOR .getExamplesPathCode (),
179
202
ApiIndex .ADMIN .getExamplesPathCode (),
@@ -185,10 +208,10 @@ private String getRedirectURLForJWTAuthentication(HttpServletRequest req, HttpSe
185
208
Integer indexOfExampleCodeInRedirect = StringUtils .indexOfAny (savedRequest .getRedirectUrl (), examplesCodes );
186
209
187
210
if (indexOfExampleCodeInRedirect != -1 ) {
188
- Boolean hasNumbers = savedRequest .getRedirectUrl ().substring (indexOfExampleCodeInRedirect ).matches (".*\\ d.*" );
211
+ Boolean hasNumbers = savedRequest .getRedirectUrl ().substring (indexOfExampleCodeInRedirect )
212
+ .matches (".*\\ d.*" );
189
213
190
- return "GET" .equals (savedRequest .getMethod ()) && hasNumbers ?
191
- savedRequest .getRedirectUrl () : "/" ;
214
+ return "GET" .equals (savedRequest .getMethod ()) && hasNumbers ? savedRequest .getRedirectUrl () : "/" ;
192
215
}
193
216
}
194
217
@@ -197,8 +220,8 @@ private String getRedirectURLForJWTAuthentication(HttpServletRequest req, HttpSe
197
220
198
221
@ GetMapping (path = "/ds-return" )
199
222
public String returnController (@ RequestParam (value = ATTR_STATE , required = false ) String state ,
200
- @ RequestParam (value = ATTR_EVENT , required = false ) String event ,
201
- @ RequestParam (required = false ) String envelopeId , ModelMap model ) {
223
+ @ RequestParam (value = ATTR_EVENT , required = false ) String event ,
224
+ @ RequestParam (required = false ) String envelopeId , ModelMap model ) {
202
225
model .addAttribute (LAUNCHER_TEXTS , config .getCodeExamplesText ().SupportingTexts );
203
226
model .addAttribute (ATTR_TITLE , "Return from DocuSign" );
204
227
model .addAttribute (ATTR_EVENT , event );
@@ -221,4 +244,4 @@ private String getLoginPath(AuthType authTypeSelected) {
221
244
}
222
245
return loginPath ;
223
246
}
224
- }
247
+ }
0 commit comments