77import com .docusign .core .model .AuthType ;
88import com .docusign .core .model .Session ;
99import com .docusign .core .model .User ;
10+ import com .docusign .core .security .acg .ACGAuthenticationMethod ;
1011import com .docusign .core .security .jwt .JWTAuthenticationMethod ;
1112import org .apache .commons .lang3 .StringUtils ;
1213import org .springframework .beans .factory .annotation .Autowired ;
@@ -94,7 +95,7 @@ public String index(ModelMap model, HttpServletResponse response) throws Excepti
9495 }
9596
9697 if (config .getQuickstart ().equals ("true" ) && config .getSelectedApiIndex ().equals (ApiIndex .ESIGNATURE ) &&
97- !(SecurityContextHolder .getContext ().getAuthentication () instanceof OAuth2AuthenticationToken )) {
98+ !(SecurityContextHolder .getContext ().getAuthentication () instanceof OAuth2AuthenticationToken )) {
9899 String site = ApiIndex .ESIGNATURE .getPathOfFirstExample ();
99100 response .setStatus (response .SC_MOVED_TEMPORARILY );
100101 response .setHeader (LOCATION_HEADER , site );
@@ -112,7 +113,8 @@ public String index(ModelMap model, HttpServletResponse response) throws Excepti
112113 }
113114
114115 @ 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 {
116118 model .addAttribute (LAUNCHER_TEXTS , config .getCodeExamplesText ().SupportingTexts );
117119 model .addAttribute (ATTR_TITLE , config .getCodeExamplesText ().SupportingTexts .LoginPage .LoginButton );
118120
@@ -125,7 +127,8 @@ public ModelAndView mustAuthenticateController(ModelMap model, HttpServletReques
125127 return new ModelAndView (new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL ));
126128 }
127129
128- boolean isRedirectToMonitor = redirectURL .toLowerCase ().contains ("/m" ) && !redirectURL .toLowerCase ().contains ("/mae" );
130+ boolean isRedirectToMonitor = redirectURL .toLowerCase ().contains ("/m" ) &&
131+ !redirectURL .toLowerCase ().contains ("/mae" );
129132 if (session .isRefreshToken () || config .getQuickstart ().equals ("true" )) {
130133 config .setQuickstart ("false" );
131134
@@ -148,32 +151,52 @@ private ModelAndView checkForMonitorRedirects(String redirectURL) {
148151 return new ModelAndView (new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL ));
149152 }
150153
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+
151169 @ 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 {
153172 if (!formParams .containsKey ("selectAuthType" )) {
154173 model .addAttribute ("message" , "Select option with selectAuthType name must be provided." );
155174 return new RedirectView ("pages/error" );
156175 }
157176
158177 String redirectURL = getRedirectURLForJWTAuthentication (req , resp );
159178
160- List <String > selectAuthTypeObject = formParams .get ("selectAuthType" );
179+ List <String > selectAuthTypeObject = formParams .get ("selectAuthType" );
161180 AuthType authTypeSelected = AuthType .valueOf (selectAuthTypeObject .get (0 ));
162181
163182 if (authTypeSelected .equals (AuthType .JWT )) {
164183 this .session .setAuthTypeSelected (AuthType .JWT );
165184 return new JWTAuthenticationMethod ().loginUsingJWT (config , session , redirectURL );
166185 } else {
167186 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+ }
169192 }
170193 }
171194
172195 private String getRedirectURLForJWTAuthentication (HttpServletRequest req , HttpServletResponse resp ) {
173196 SavedRequest savedRequest = requestCache .getRequest (req , resp );
174197
175- String [] examplesCodes = new String []{
176- ApiIndex .CLICK .getExamplesPathCode (),
198+ String [] examplesCodes = new String [] {
199+ ApiIndex .CLICK .getExamplesPathCode (),
177200 ApiIndex .ESIGNATURE .getExamplesPathCode (),
178201 ApiIndex .MONITOR .getExamplesPathCode (),
179202 ApiIndex .ADMIN .getExamplesPathCode (),
@@ -185,10 +208,10 @@ private String getRedirectURLForJWTAuthentication(HttpServletRequest req, HttpSe
185208 Integer indexOfExampleCodeInRedirect = StringUtils .indexOfAny (savedRequest .getRedirectUrl (), examplesCodes );
186209
187210 if (indexOfExampleCodeInRedirect != -1 ) {
188- Boolean hasNumbers = savedRequest .getRedirectUrl ().substring (indexOfExampleCodeInRedirect ).matches (".*\\ d.*" );
211+ Boolean hasNumbers = savedRequest .getRedirectUrl ().substring (indexOfExampleCodeInRedirect )
212+ .matches (".*\\ d.*" );
189213
190- return "GET" .equals (savedRequest .getMethod ()) && hasNumbers ?
191- savedRequest .getRedirectUrl () : "/" ;
214+ return "GET" .equals (savedRequest .getMethod ()) && hasNumbers ? savedRequest .getRedirectUrl () : "/" ;
192215 }
193216 }
194217
@@ -197,8 +220,8 @@ private String getRedirectURLForJWTAuthentication(HttpServletRequest req, HttpSe
197220
198221 @ GetMapping (path = "/ds-return" )
199222 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 ) {
202225 model .addAttribute (LAUNCHER_TEXTS , config .getCodeExamplesText ().SupportingTexts );
203226 model .addAttribute (ATTR_TITLE , "Return from DocuSign" );
204227 model .addAttribute (ATTR_EVENT , event );
@@ -221,4 +244,4 @@ private String getLoginPath(AuthType authTypeSelected) {
221244 }
222245 return loginPath ;
223246 }
224- }
247+ }
0 commit comments