@@ -68,24 +68,23 @@ public static Map<String, String> transformRequestHeadersForHydra(Map<String, St
6868 }
6969
7070 private static String transformQueryParamsInURLFromHydra (String redirectTo ) {
71- try {
72- URL url = new URL (redirectTo );
73- String query = url .getQuery ();
74- if (query != null ) {
75- String [] queryParams = query .split ("&" );
76- StringBuilder updatedQuery = new StringBuilder ();
77- for (String param : queryParams ) {
78- String [] keyValue = param .split ("=" );
79- if (keyValue .length > 1 && keyValue [1 ].startsWith ("ory_" )) {
80- updatedQuery .append (keyValue [0 ]).append ("=" ).append (keyValue [1 ].replaceFirst ("ory_" , "st_" )).append ("&" );
81- } else {
82- updatedQuery .append (param ).append ("&" );
83- }
71+ if (!redirectTo .contains ("?" )) {
72+ return redirectTo ;
73+ }
74+
75+ String query = redirectTo .split ("\\ ?" )[1 ];
76+ if (query != null ) {
77+ String [] queryParams = query .split ("&" );
78+ StringBuilder updatedQuery = new StringBuilder ();
79+ for (String param : queryParams ) {
80+ String [] keyValue = param .split ("=" );
81+ if (keyValue .length > 1 && keyValue [1 ].startsWith ("ory_" )) {
82+ updatedQuery .append (keyValue [0 ]).append ("=" ).append (keyValue [1 ].replaceFirst ("ory_" , "st_" )).append ("&" );
83+ } else {
84+ updatedQuery .append (param ).append ("&" );
8485 }
85- redirectTo = redirectTo .replace ("?" + query , "?" + updatedQuery .toString ().trim ());
8686 }
87- } catch (MalformedURLException e ) {
88- throw new IllegalStateException (e );
87+ redirectTo = redirectTo .replace ("?" + query , "?" + updatedQuery .toString ().trim ());
8988 }
9089
9190 return redirectTo ;
@@ -153,37 +152,29 @@ private static String transformRedirectUrlFromHydra(Main main, AppIdentifier app
153152 if (!redirectTo .startsWith ("/" )) {
154153 redirectTo = transformQueryParamsInURLFromHydra (redirectTo );
155154
156- try {
157- if (Utils .containsUrl (redirectTo , hydraInternalAddress , true )) {
158- try {
159- URL url = new URL (redirectTo );
160- String query = url .getQuery ();
161- Map <String , String > urlQueryParams = new HashMap <>();
162- if (query != null ) {
163- String [] pairs = query .split ("&" );
164- for (String pair : pairs ) {
165- int idx = pair .indexOf ("=" );
166- urlQueryParams .put (pair .substring (0 , idx ), URLDecoder .decode (pair .substring (idx + 1 ), StandardCharsets .UTF_8 ));
167- }
168- }
169- String error = urlQueryParams .getOrDefault ("error" , null );
170- String errorDescription = urlQueryParams .getOrDefault ("error_description" , null );
171- if (error != null ) {
172- throw new OAuthAPIException (error , errorDescription , 400 );
173- }
174- redirectTo = redirectTo .replace (hydraInternalAddress , "{apiDomain}" );
175-
176- // path to hydra starts with /oauth2 while on the SDK it would be /oauth
177- redirectTo = redirectTo .replace ("oauth2/" , "oauth/" );
178-
179- } catch (MalformedURLException e ) {
180- throw new IllegalStateException (e );
155+ // We do not use the containsURL util to compare these because redirectTo can be a deep link
156+ // Also, we do not mind comparison to internal addresses being strict comparisons
157+ if (redirectTo .startsWith (hydraInternalAddress )) {
158+ String query = redirectTo .contains ("?" ) ? redirectTo .split ("\\ ?" )[1 ] : null ;
159+ Map <String , String > urlQueryParams = new HashMap <>();
160+ if (query != null ) {
161+ String [] pairs = query .split ("&" );
162+ for (String pair : pairs ) {
163+ int idx = pair .indexOf ("=" );
164+ urlQueryParams .put (pair .substring (0 , idx ), URLDecoder .decode (pair .substring (idx + 1 ), StandardCharsets .UTF_8 ));
181165 }
182- } else if (Utils .containsUrl (redirectTo , hydraBaseUrlForConsentAndLogin , true )) {
183- redirectTo = redirectTo .replace (hydraBaseUrlForConsentAndLogin , "{apiDomain}" );
184166 }
185- } catch (MalformedURLException e ) {
186- throw new IllegalStateException (e );
167+ String error = urlQueryParams .getOrDefault ("error" , null );
168+ String errorDescription = urlQueryParams .getOrDefault ("error_description" , null );
169+ if (error != null ) {
170+ throw new OAuthAPIException (error , errorDescription , 400 );
171+ }
172+ redirectTo = redirectTo .replace (hydraInternalAddress , "{apiDomain}" );
173+
174+ // path to hydra starts with /oauth2 while on the SDK it would be /oauth
175+ redirectTo = redirectTo .replace ("oauth2/" , "oauth/" );
176+ } else if (redirectTo .startsWith (hydraBaseUrlForConsentAndLogin )) {
177+ redirectTo = redirectTo .replace (hydraBaseUrlForConsentAndLogin , "{apiDomain}" );
187178 }
188179 }
189180
0 commit comments