Warning: Can't synchronize with the repository (GIT backend not available). Look in the Trac log for more information.
|
File console.diff, 3.7 KB
(added by daniel, 2 years ago)
|
|
Patch for authentication on console
|
-
|
|
|
|
| 461 | 461 | |
| 462 | 462 | private void printHelpConnect() { |
| 463 | 463 | writeln("Usage:"); |
| 464 | | writeln("connect default Opens the default repository set for this console"); |
| 465 | | writeln("connect <dataDirectory> Opens the repository set in the specified data dir"); |
| 466 | | writeln("connect <serverURL> Connects to a Sesame server"); |
| | 464 | writeln("connect default Opens the default repository set for this console"); |
| | 465 | writeln("connect <dataDirectory> Opens the repository set in the specified data dir"); |
| | 466 | writeln("connect <serverURL> [user] [password] Connects to a Sesame server"); |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | private void connect(String[] tokens) { |
| 470 | | if (tokens.length != 2) { |
| | 470 | if (tokens.length > 4) { |
| 471 | 471 | printHelpConnect(); |
| 472 | 472 | return; |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | String target = tokens[1]; |
| | 476 | String username = (tokens.length > 2) ? tokens[2] : null; |
| | 477 | String password = (tokens.length > 3) ? tokens[3] : null; |
| 476 | 478 | |
| 477 | 479 | if ("default".equalsIgnoreCase(target)) { |
| 478 | 480 | connectDefault(); |
| … |
… |
|
| 481 | 483 | try { |
| 482 | 484 | new URL(target); |
| 483 | 485 | // target is a valid URL |
| 484 | | connectRemote(target); |
| | 486 | connectRemote(target, username, password); |
| 485 | 487 | } |
| 486 | 488 | catch (MalformedURLException e) { |
| 487 | 489 | // assume target is a directory path |
| … |
… |
|
| 505 | 507 | } |
| 506 | 508 | |
| 507 | 509 | private boolean connectRemote(String url) { |
| | 510 | return connectRemote(url, null, null); |
| | 511 | } |
| | 512 | |
| | 513 | private boolean connectRemote(String url, String user, String pass) { |
| 508 | 514 | try { |
| 509 | 515 | // Ping server |
| 510 | 516 | HTTPClient httpClient = new HTTPClient(); |
| 511 | 517 | httpClient.setServerURL(url); |
| | 518 | |
| | 519 | // Check for the password if none is given |
| | 520 | if(user != null && pass == null) { |
| | 521 | try { |
| | 522 | pass = readPassword("Connection password:"); |
| | 523 | } catch(IOException e) { |
| | 524 | writeError("Error reading password."); |
| | 525 | logger.warn("Error reading password.", e); |
| | 526 | } |
| | 527 | } |
| | 528 | // Set authentication (null will disable it correctly) |
| | 529 | httpClient.setUsernameAndPassword(user, pass); |
| | 530 | |
| | 531 | // Ping the server |
| 512 | 532 | httpClient.getServerProtocol(); |
| 513 | | |
| 514 | | return installNewManager(new RemoteRepositoryManager(url), url); |
| | 533 | |
| | 534 | RemoteRepositoryManager remoteManager = new RemoteRepositoryManager(url); |
| | 535 | if(user != null) { |
| | 536 | remoteManager.setUsernameAndPassword(user, pass); |
| | 537 | } |
| | 538 | |
| | 539 | return installNewManager(remoteManager, url); |
| 515 | 540 | } |
| 516 | 541 | catch (UnauthorizedException e) { |
| 517 | | // FIXME: handle authentication |
| 518 | | writeError("Not authorized to access the server"); |
| | 542 | if(user != null) { |
| | 543 | writeError("Not authorized to access the server"); |
| | 544 | logger.warn("Failed to access the server", e); |
| | 545 | } else { |
| | 546 | try { |
| | 547 | writeln("Authentication required"); |
| | 548 | String username = readln("User name:"); |
| | 549 | connectRemote(url, username, null); |
| | 550 | } catch(IOException ioe) { |
| | 551 | writeError("Error on getting user name for connection."); |
| | 552 | logger.warn("Could not get user for connection", ioe); |
| | 553 | } |
| | 554 | } |
| 519 | 555 | } |
| 520 | 556 | catch (IOException e) { |
| 521 | 557 | writeError("Failed to access the server: " + e.getMessage()); |
| … |
… |
|
| 1798 | 1834 | return buf.toString().trim(); |
| 1799 | 1835 | } |
| 1800 | 1836 | |
| | 1837 | private String readln() throws IOException { |
| | 1838 | return readln(null); |
| | 1839 | } |
| | 1840 | |
| | 1841 | private String readln(String message) throws IOException { |
| | 1842 | if(message != null) { |
| | 1843 | write(message + " "); |
| | 1844 | } |
| | 1845 | return in.readLine(); |
| | 1846 | } |
| | 1847 | |
| | 1848 | private String readPassword(String message) throws IOException { |
| | 1849 | // TODO: Proper password reader |
| | 1850 | return readln(message); |
| | 1851 | } |
| | 1852 | |
| 1801 | 1853 | private void write(String s) { |
| 1802 | 1854 | out.print(s); |
| 1803 | 1855 | } |
Download in other formats: