Skip to end of metadata
Go to start of metadata

How to determine if a userid is a member of a group

Simplest possible example

Prerequisite

  • You must have a keystore with a UWCA certificate for your application. This will be used to authenticate to the groups web service.
  • You must have a truststore with the UWCA certificate. The web service uses a UWCA certificate to authenticate to you.

Program

MembershipTest.java

/* Simple test if an id is a member of a group */

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class MembershipTest {

   private static String baseUrl;

   public MembershipTest(String base) {
       baseUrl = base;
   }

   // do the test
   public boolean isMember(String group, String user) {

      String urltext = baseUrl + "/group/" + group + "/member/" + user;
      try {
         URL url = new URL(urltext);
         HttpsURLConnection gwsConnection = (HttpsURLConnection) url.openConnection();
         int resp = gwsConnection.getResponseCode();
         gwsConnection.disconnect();
         if (resp==200) return true;
         if (resp==404) return false;
         System.err.println("Got an unexpected return: " + resp);
      } catch (Exception e) {
         System.err.println("got error: " + e);
      }
      return (false);
   }

   /* Set keystore and truststore.
      These are often specified on the java command line or in a config file.
      instead of in the code.  */

   private static void setKeys() {

      String keyStore = "your_keystore.jks";
      String keyStorePW = "your_keystore_password";
      String trustStore = "your_truststore.jks";
      String trustStorePW = "your_truststore_password";

      // Set the security credential properties
      System.setProperty("javax.net.ssl.keyStore", keyStore);
      System.setProperty("javax.net.ssl.keyStoreType", "JKS");
      System.setProperty("javax.net.ssl.keyStorePassword", keyStorePW);

      System.setProperty("javax.net.ssl.trustStore", trustStore);
      System.setProperty("javax.net.ssl.trustStoreType", "JKS");
      System.setProperty("javax.net.ssl.trustStorePassword", trustStorePW);
   }

   public static void main (String[] args) throws Exception {
      String gwsUrl = "https://iam-ws.u.washington.edu:7443/group_sws/v1";
      setKeys();
      MembershipTest tester = new MembershipTest(gwsUrl);
      boolean result = tester.isMember("group_to_test", "uwnetid_to_test");
      if (result) System.out.println("is a member");
      else System.out.println("not a member");
   }

}