File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -32,4 +32,27 @@ public static String hidePassword(String url) {
3232 }
3333 }
3434
35- }
35+ public static String addAuthentication (String url , String basicAuth ) {
36+ if (url == null ) {
37+ return null ;
38+ }
39+ try {
40+ URI uri = URI .create (url );
41+ URI sanitized = new URI (
42+ uri .getScheme (),
43+ basicAuth ,
44+ uri .getHost (),
45+ uri .getPort (),
46+ uri .getPath (),
47+ uri .getQuery (),
48+ uri .getFragment ()
49+ );
50+ return sanitized .toString ();
51+ } catch (Exception e ) {
52+ // on any parse error, return original URL unchanged
53+ return url ;
54+ }
55+ }
56+
57+
58+ }
Original file line number Diff line number Diff line change @@ -11,4 +11,9 @@ public void hidePassword() {
1111 assertThat ( URLUtils .hidePassword ("https://user:password@example.com/path?query=1" )).isEqualTo ("https://user:*****@example.com/path?query=1" );
1212 }
1313
14+ @ Test
15+ public void addPassword () {
16+ assertThat ( URLUtils .addAuthentication ("https://example.com/path?query=1" , "user:password" )).isEqualTo ("https://user:password@example.com/path?query=1" );
17+ }
18+
1419}
You can’t perform that action at this time.
0 commit comments