Changeset 37236 for main


Ignore:
Timestamp:
2023-02-01T16:38:58+13:00 (15 months ago)
Author:
kjdon
Message:

first stab at doing some authentication in webswing gli. firstly, library_url must be passed in as an arg. we prompt for a username and password very early on. If no collection editing groups, or invalid user, then the gli won't start. open collection dialog only shows collections that the user has permission for

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r37188 r37236  
    9292    static private Dimension size = new Dimension(800, 540);
    9393    static public RemoteGreenstoneServer remoteGreenstoneServer = null;
     94    static public WebswingAuthenticator webswingAuthenticator = null;
     95   
    9496
    9597    /** Has the exit flag been set? */
     
    273275    {
    274276   
    275 
     277        if (isWebswing) {
     278        // library_url must be defined in the options
     279        if (library_url_string == null) {
     280            System.err.println("Please set library_url option as a webswing option");
     281            System.exit(0);
     282        }
     283        // we need to authenticate the user
     284        webswingAuthenticator = new WebswingAuthenticator();
     285        if (!webswingAuthenticator.authenticate(library_url_string)) {
     286            System.err.println("authentication error");
     287            System.exit(0);
     288        }
     289        }
    276290        // Create the debug stream if required
    277291        if (debug_enabled) {
     
    19471961    }
    19481962   
     1963
     1964    static public class WebswingAuthenticator
     1965        extends GAuthenticator
     1966    {
     1967        static protected String username;
     1968        static protected HashSet<String> groups;
     1969
     1970        public boolean authenticate(String library_url_string) {
     1971        PasswordAuthentication pa = getPasswordAuthentication();
     1972        if (pa == null) {
     1973            // user cancelled - how do we show error?
     1974            System.err.println("Authentication cancelled. You cannot access webswing gli without Greenstone user account");
     1975            return false;
     1976
     1977        }
     1978        username = pa.getUserName();
     1979       
     1980        String password = new String(pa.getPassword());
     1981        String result;
     1982        try {
     1983            String new_url = library_url_string+"?a=s&sa=authenticated-ping&excerptid=gs_content&un="+username+"&pw="+password;
     1984            URL authenticationURL = new URL(new_url);
     1985            HttpURLConnection conn = (HttpURLConnection)authenticationURL.openConnection();     
     1986            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     1987            result = "";
     1988            String line = null;
     1989           
     1990            while((line = reader.readLine()) != null) {
     1991            result += line;
     1992            }
     1993        } catch (Exception e) {
     1994            System.err.println("There was an exception "+e.getMessage());
     1995            return false;
     1996        }
     1997        // Parse out the content nested inside <div ... id="gs_content"> </div>
     1998        int start = result.indexOf("id=\"gs_content\"");
     1999        if(start != -1) {
     2000            start = result.indexOf(">", start);
     2001            int end = result.indexOf("<", start);
     2002            result = result.substring(start+1, end);
     2003            result = result.trim();
     2004        }       
     2005        if (result.startsWith("Authentication failed:")) {
     2006            System.err.println("Authentication Error: "+result);
     2007            return false;
     2008        }
     2009        groups = new HashSet<String>();
     2010        String[] contents = result.split(",");
     2011        for (int i=0; i<contents.length; i++) {
     2012            String g = contents[i];
     2013            if (g.equals("all-collections-editor") || g.equals("personal-collections-editor") || g.endsWith("-collection-editor")) {
     2014            groups.add(g);
     2015            }
     2016        }
     2017        if (groups.size()==0) {
     2018            return false; // user has no editing priveleges
     2019        }
     2020        return true;
     2021        }
     2022       
     2023        public boolean canEditCollection(String collection) {
     2024        if (groups.contains("all-collections-editor")) {
     2025            return true;
     2026        }
     2027        if (groups.contains("personal-collections-editor") && collection.startsWith(username+"-")) {
     2028            return true;
     2029        }
     2030        if (groups.contains(collection+"-collection-editor")) {
     2031            return true;
     2032        }
     2033        return false;
     2034        }
     2035       
     2036       
     2037       
     2038        public PasswordAuthentication getAuthentication(String username, String password)
     2039        {
     2040        return getPasswordAuthentication(username,password);
     2041        }
     2042
     2043        public PasswordAuthentication getAuthentication()
     2044        {
     2045        return getPasswordAuthentication();
     2046        }
     2047
     2048       
     2049        protected String getMessageString()
     2050        {
     2051        return Dictionary.get("RemoteGreenstoneServer.Authentication_Message");
     2052        }
     2053
     2054       
     2055    }
     2056
     2057   
    19492058}
  • main/trunk/gli/src/org/greenstone/gatherer/gui/OpenCollectionDialog.java

    r29016 r37236  
    324324
    325325        if(collection_folder.isDirectory() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
     326           
    326327            // this 'file_name' has already been prefixed by 'etc'
    327328            String file_name;
     
    345346            else {
    346347              // add this collection. We want to allow non gli collections, so add anything that has a config file.
    347               data.add(collection_configuration);
     348                // webswing - check whether we are allowed to edit this coll
     349                if (Gatherer.isWebswing) {
     350                if (Gatherer.webswingAuthenticator.canEditCollection(collection_foldername)) {
     351                    data.add(collection_configuration);
     352                }
     353                }
     354                else {
     355                data.add(collection_configuration);
     356                }
    348357            }
    349358            }
Note: See TracChangeset for help on using the changeset viewer.