66import com .docusign .core .model .User ;
77import java .io .IOException ;
88import java .util .*;
9+ import javax .servlet .http .HttpServletRequest ;
10+ import javax .servlet .http .HttpServletResponse ;
11+ import com .docusign .core .security .acg .ACGAuthenticationMethod ;
912
1013import com .docusign .core .utils .AccountsConverter ;
1114import org .apache .commons .lang3 .StringUtils ;
2629import com .docusign .esign .client .auth .OAuth ;
2730import java .util .stream .Collectors ;
2831import org .springframework .security .core .Authentication ;
29- import org .springframework .web .bind .annotation .ControllerAdvice ;
30- import org .springframework .web .bind .annotation .ModelAttribute ;
31- import javax .servlet .http .HttpServletResponse ;
3232
3333@ Controller
3434@ ControllerAdvice
3535@ Scope (WebApplicationContext .SCOPE_SESSION )
3636public class IndexController {
37- private static final String ATTR_ENVELOPE_ID = "qpEnvelopeId" ;
37+ private static final List < String > ESIGNATURE_SCOPES = Arrays . asList ( "signature" ) ;
3838 private static final String ATTR_STATE = "state" ;
3939 private static final String ATTR_EVENT = "event" ;
40- private static final String ATTR_TITLE = "title" ;
4140
4241 private static final String ERROR_ACCOUNT_NOT_FOUND = "Could not find account information for the user" ;
43- private static final String SELECTED_API_NOT_SUPPORTED = "Currently selected api is not supported by launcher. Please, check appsettings.json file." ;
4442 private final DSConfiguration config ;
4543 private final Session session ;
4644 private final User user ;
@@ -65,22 +63,45 @@ public String index(ModelMap model, HttpServletResponse response) throws IOExcep
6563 }
6664
6765 @ GetMapping (path = "/ds/mustAuthenticate" )
68- public ModelAndView mustAuthenticateController (ModelMap model ) throws IOException {
66+ public ModelAndView mustAuthenticateController (ModelMap model ) throws IOException , Exception {
6967 return new ModelAndView (getRedirectView ());
7068 }
7169
7270 @ GetMapping (path = "/ds-return" )
7371 public String returnController (@ RequestParam (value = ATTR_STATE , required = false ) String state ,
7472 @ RequestParam (value = ATTR_EVENT , required = false ) String event ,
75- @ RequestParam (required = false ) String envelopeId , ModelMap model , HttpServletResponse response ) throws IOException {
73+ @ RequestParam (required = false ) String envelopeId , ModelMap model , HttpServletResponse response )
74+ throws IOException {
7675 String site = "/eg001" ;
7776 response .setStatus (response .SC_MOVED_TEMPORARILY );
7877 response .setHeader ("Location" , site );
7978 return null ;
8079 }
8180
82- private RedirectView getRedirectView () {
83- RedirectView redirect = new RedirectView (getLoginPath ());
81+ @ GetMapping ("/pkce" )
82+ public RedirectView pkce (String code , String state , HttpServletRequest req , HttpServletResponse resp )
83+ throws Exception {
84+ String redirectURL = "/" ;
85+ RedirectView redirect ;
86+ try {
87+ redirect = new ACGAuthenticationMethod ().exchangeCodeForToken (code , config , session , redirectURL ,
88+ ESIGNATURE_SCOPES );
89+ } catch (Exception e ) {
90+ redirect = new RedirectView (getLoginPath ());
91+ this .session .setIsPKCEWorking (false );
92+ }
93+
94+ return redirect ;
95+ }
96+
97+ private RedirectView getRedirectView () throws Exception {
98+ RedirectView redirect ;
99+ if (this .session .getIsPKCEWorking ()) {
100+ redirect = new ACGAuthenticationMethod ().initiateAuthorization (config , ESIGNATURE_SCOPES );
101+ } else {
102+ redirect = new RedirectView (getLoginPath ());
103+ }
104+
84105 redirect .setExposeModelAttributes (false );
85106 return redirect ;
86107 }
@@ -111,12 +132,16 @@ public Object populateLocals() throws IOException {
111132 OAuth2User oauthUser = oauth .getPrincipal ();
112133 OAuth2AuthorizedClient oauthClient = authorizedClientService .loadAuthorizedClient (
113134 oauth .getAuthorizedClientRegistrationId (),
114- oauthUser .getName ()
115- );
135+ oauthUser .getName ());
116136
117137 if (oauth .isAuthenticated ()) {
118138 user .setName (oauthUser .getAttribute ("name" ));
119- user .setAccessToken (oauthClient .getAccessToken ().getTokenValue ());
139+
140+ if (oauthClient != null ) {
141+ user .setAccessToken (oauthClient .getAccessToken ().getTokenValue ());
142+ } else {
143+ user .setAccessToken (((OAuth .OAuthToken ) oauthUser .getAttribute ("access_token" )).getAccessToken ());
144+ }
120145
121146 if (account .isEmpty ()) {
122147 account = Optional .ofNullable (getDefaultAccountInfo (getOAuthAccounts (oauthUser )));
@@ -139,7 +164,7 @@ private String getBaseUrl(OAuth.Account oauthAccount) {
139164
140165 private static List <OAuth .Account > getOAuthAccounts (OAuth2User user ) {
141166 List <Map <String , Object >> oauthAccounts = user .getAttribute ("accounts" );
142- if (oauthAccounts == null ){
167+ if (oauthAccounts == null ) {
143168 return new ArrayList <>();
144169 }
145170
@@ -152,7 +177,7 @@ private OAuth.Account getDefaultAccountInfo(List<OAuth.Account> accounts) {
152177 String targetAccountId = config .getTargetAccountId ();
153178 if (StringUtils .isNotBlank (targetAccountId )) {
154179 OAuth .Account account = getAccountById (accounts , targetAccountId );
155- if (account != null ) {
180+ if (account != null ) {
156181 return account ;
157182 }
158183 }
0 commit comments