Warning: Can't synchronize with the repository (GIT backend not available). Look in the Trac log for more information.

SecuringSesame: console.diff

File console.diff, 3.7 KB (added by daniel, 2 years ago)

Patch for authentication on console

  • core/console/src/main/java/org/openrdf/console/Console.java

     
    461461 
    462462        private void printHelpConnect() { 
    463463                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"); 
    467467        } 
    468468 
    469469        private void connect(String[] tokens) { 
    470                 if (tokens.length != 2) { 
     470                if (tokens.length > 4) { 
    471471                        printHelpConnect(); 
    472472                        return; 
    473473                } 
    474474 
    475475                String target = tokens[1]; 
     476                String username = (tokens.length > 2) ? tokens[2] : null; 
     477                String password = (tokens.length > 3) ? tokens[3] : null; 
    476478 
    477479                if ("default".equalsIgnoreCase(target)) { 
    478480                        connectDefault(); 
     
    481483                        try { 
    482484                                new URL(target); 
    483485                                // target is a valid URL 
    484                                 connectRemote(target); 
     486                                connectRemote(target, username, password); 
    485487                        } 
    486488                        catch (MalformedURLException e) { 
    487489                                // assume target is a directory path 
     
    505507        } 
    506508 
    507509        private boolean connectRemote(String url) { 
     510                return connectRemote(url, null, null); 
     511        } 
     512 
     513        private boolean connectRemote(String url, String user, String pass) { 
    508514                try { 
    509515                        // Ping server 
    510516                        HTTPClient httpClient = new HTTPClient(); 
    511517                        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 
    512532                        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); 
    515540                } 
    516541                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                        } 
    519555                } 
    520556                catch (IOException e) { 
    521557                        writeError("Failed to access the server: " + e.getMessage()); 
     
    17981834                return buf.toString().trim(); 
    17991835        } 
    18001836 
     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 
    18011853        private void write(String s) { 
    18021854                out.print(s); 
    18031855        }