Ejemplo n.º 1
0
  public void changePassword(
      String env,
      String cspName,
      String cspPassword,
      String cloudNameString,
      String currentPassword,
      String newPassword) {
    // Step 0: Set up CSP

    CloudNumber cspCloudNumber = null;
    String cspRegistryURL = "";
    String cspUserURL = "";
    XDIDiscoveryClient discoveryClient = null;
    String registrationSvcURL = "";

    if (env.equalsIgnoreCase("prod")) {
      discoveryClient = XDIDiscoveryClient.NEUSTAR_PROD_DISCOVERY_CLIENT;
      cspRegistryURL = "https://mycloud.neustar.biz/" + cspName + "-registry";
      cspUserURL = "https://mycloud.neustar.biz/" + cspName + "-users/";
      registrationSvcURL = "https://registration.respectnetwork.net/registration";
    } else if (env.equalsIgnoreCase("ote")) {
      discoveryClient = XDIDiscoveryClient.NEUSTAR_OTE_DISCOVERY_CLIENT;
      cspRegistryURL = "https://mycloud-ote.neustar.biz/" + cspName + "-registry";
      cspUserURL = "https://mycloud-ote.neustar.biz/" + cspName + "-users/";
      registrationSvcURL = "https://registration-stage.respectnetwork.net/registration";
    } else if (env.equalsIgnoreCase("prod-au")) {
      discoveryClient = XDIDiscoveryClient.NEUSTAR_PROD_DISCOVERY_CLIENT;
      cspRegistryURL = "https://mycloud-au.neustar.biz/" + cspName + "-registry";
      ;
      cspUserURL = "https://mycloud-au.neustar.biz/" + cspName + "-users/";
      registrationSvcURL = "https://registration.respectnetwork.net/registration";
    }
    System.out.println("CSP Resistry URL:" + cspRegistryURL);
    System.out.println("CSP User URL:" + cspUserURL);
    System.out.println("Regn. svc. URL:" + registrationSvcURL);
    System.out.println("Discovery info:" + discoveryClient.toString());
    try {
      XDIDiscoveryResult discResult = discoveryClient.discover(XDIAddress.create("+" + cspName));

      cspCloudNumber = discResult.getCloudNumber();
      System.out.println("CSP Cloud Number : " + cspCloudNumber.toString());

    } catch (Xdi2ClientException e) {
      System.out.println("Xdi2ClientException: " + e.getMessage());
      e.printStackTrace();
    }
    BasicCSPInformation cspInformation =
        new BasicCSPInformation(
            cspCloudNumber,
            cspRegistryURL,
            cspUserURL,
            cspPassword,
            null,
            CloudNumber.create("[+]!:uuid:ca51aeb9-e09e-4305-89d7-87a944a1e1fa"),
            registrationSvcURL,
            XDIAddress.create("([+]!:uuid:ca51aeb9-e09e-4305-89d7-87a944a1e1fa/#registrar)$do"),
            null,
            discoveryClient);

    System.out.println("CSP Information:\n" + cspInformation.toString());
    cspInformation.setRnCspSecretToken(null);
    try {
      cspInformation.retrieveCspSignaturePrivateKey();
    } catch (Xdi2ClientException e2) {
      // TODO Auto-generated catch block
      e2.printStackTrace();
    } catch (GeneralSecurityException e2) {
      // TODO Auto-generated catch block
      e2.printStackTrace();
    }
    if (cspInformation.getCspSignaturePrivateKey() == null) {
      System.err.println("Invalid CSP Information.");
      System.exit(0);
    }

    cloudName = CloudName.create(cloudNameString);

    if (cloudName == null || !cloudNameString.startsWith("=")) {

      System.err.println("Invalid Cloud Name.");
      System.exit(0);
    }
    try {
      XDIDiscoveryResult discResult = discoveryClient.discover(XDIAddress.create(cloudNameString));

      cloudNumber = discResult.getCloudNumber();
      System.out.println("Cloud Number : " + cloudNumber.toString());

    } catch (Xdi2ClientException e) {
      System.out.println("Xdi2ClientException: " + e.getMessage());
      e.printStackTrace();
    }
    try {

      CSP csp = new BasicCSP(cspInformation);

      // Step 1: authenticate user

      try {
        csp.authenticateInCloud(cloudNumber, currentPassword);
      } catch (Xdi2ClientException badpass) {
        System.out.println("Cannot authenticate. Exiting ...");
        System.exit(0);
      }

      // Step 2: Change Secret Token

      csp.setCloudSecretTokenInCSP(cloudNumber, newPassword);

      // done

      System.out.println("Done Changing password for cloudname " + cloudNameString);
    } catch (RuntimeException ex1) {
      ex1.printStackTrace();
    } catch (Xdi2ClientException ex2) {
      ex2.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    String resultFormat = request.getParameter("resultFormat");
    String writeImplied = request.getParameter("writeImplied");
    String writeOrdered = request.getParameter("writeOrdered");
    String writePretty = request.getParameter("writePretty");
    String input = request.getParameter("input");
    String endpoint = request.getParameter("endpoint");
    String authority = request.getParameter("authority");
    String services = request.getParameter("services");
    String output = "";
    String output2 = "";
    String outputId = "";
    String outputId2 = "";
    String stats = "-1";
    String error = null;

    Properties xdiResultWriterParameters = new Properties();

    xdiResultWriterParameters.setProperty(
        XDIWriterRegistry.PARAMETER_IMPLIED, "on".equals(writeImplied) ? "1" : "0");
    xdiResultWriterParameters.setProperty(
        XDIWriterRegistry.PARAMETER_ORDERED, "on".equals(writeOrdered) ? "1" : "0");
    xdiResultWriterParameters.setProperty(
        XDIWriterRegistry.PARAMETER_PRETTY, "on".equals(writePretty) ? "1" : "0");

    XDIWriter xdiResultWriter =
        XDIWriterRegistry.forFormat(resultFormat, xdiResultWriterParameters);

    XDIDiscoveryResult discoveryResultRegistry = null;
    XDIDiscoveryResult discoveryResultAuthority = null;
    Exception exceptionAuthority = null;

    //		LoggingTrustManager loggingTrustManager = null;

    long start = System.currentTimeMillis();
    long startRegistry = 0, stopRegistry = 0, startAuthority = 0, stopAuthority = 0;

    try {

      // start discovery

      XDIDiscoveryClient discoveryClient = new XDIDiscoveryClient(endpoint);

      discoveryClient.setDiscoveryCacheProvider(null);

      // from registry

      //			loggingTrustManager = new LoggingTrustManager();

      startRegistry = System.currentTimeMillis();
      discoveryResultRegistry = discoveryClient.discoverFromRegistry(XDIAddress.create(input));
      stopRegistry = System.currentTimeMillis();

      // output result from registry

      StringWriter writer = new StringWriter();

      if (discoveryResultRegistry != null) {

        writer.write(
            "Discovery result from registry: ("
                + Long.toString(stopRegistry - startRegistry)
                + " ms time"
                + ")\n\n");

        /*			if (loggingTrustManager.getBuffer().length() > 0) {

        	writer.write(loggingTrustManager.getBuffer().toString() + "\n");
        }*/

        writer.write("Cloud Number: " + discoveryResultRegistry.getCloudNumber() + "\n");
        writer.write(
            "Cloud Names: "
                + (discoveryResultRegistry.getCloudNames() == null
                    ? null
                    : Arrays.asList(discoveryResultRegistry.getCloudNames()))
                + "\n");
        writer.write("XDI Endpoint URI: " + discoveryResultRegistry.getXdiEndpointUri() + "\n");
        writer.write(
            "Default Endpoint URI: " + discoveryResultRegistry.getDefaultEndpointUri() + "\n");
        writer.write(
            "Signature Public Key: " + discoveryResultRegistry.getSignaturePublicKey() + "\n");
        writer.write(
            "Encryption Public Key: " + discoveryResultRegistry.getEncryptionPublicKey() + "\n");

        if (discoveryResultRegistry.getEndpointUris().isEmpty()) {

          writer.write("Services: (none)\n");
        } else {

          for (Map.Entry<XDIAddress, URI> endpointUri :
              discoveryResultRegistry.getEndpointUris().entrySet()) {

            writer.write("Service " + endpointUri.getKey() + ": " + endpointUri.getValue() + "\n");
          }
        }

        writer.write("\n");

        writer.write("Message envelope to registry:\n\n");

        if (discoveryResultRegistry.getMessageEnvelope() != null)
          xdiResultWriter.write(discoveryResultRegistry.getMessageEnvelope().getGraph(), writer);
        else writer.write("(null)");

        writer.write("\n\n");

        writer.write("Message result from registry:\n\n");

        if (discoveryResultRegistry.getMessagingResponse() != null)
          xdiResultWriter.write(discoveryResultRegistry.getMessagingResponse().getGraph(), writer);
        else writer.write("(null)\n");
      } else {

        writer.write("No discovery result from registry.\n");
      }

      // from authority

      if ("on".equals(authority)) {

        if (discoveryResultRegistry != null
            && discoveryResultRegistry.getXdiEndpointUri() != null) {

          //					loggingTrustManager = new LoggingTrustManager();

          String[] endpointUriTypesString =
              services.trim().isEmpty() ? new String[0] : services.trim().split("[, ]");
          XDIAddress[] endpointUriTypes = new XDIAddress[endpointUriTypesString.length];
          for (int i = 0; i < endpointUriTypes.length; i++)
            endpointUriTypes[i] = XDIAddress.create(endpointUriTypesString[i].trim());

          try {

            startAuthority = System.currentTimeMillis();
            discoveryResultAuthority =
                discoveryClient.discoverFromAuthority(
                    discoveryResultRegistry.getXdiEndpointUri(),
                    discoveryResultRegistry.getCloudNumber(),
                    endpointUriTypes);
            stopAuthority = System.currentTimeMillis();
          } catch (Exception ex) {

            exceptionAuthority = ex;
            discoveryResultAuthority = null;
          }
        }
      }

      // output result from authority

      StringWriter writer2 = new StringWriter();

      if (discoveryResultAuthority != null) {

        writer2.write(
            "Discovery result from authority: ("
                + Long.toString(stopAuthority - startAuthority)
                + " ms time"
                + ")\n\n");

        /*				if (loggingTrustManager.getBuffer().length() > 0) {

        	writer2.write(loggingTrustManager.getBuffer().toString() + "\n");
        }*/

        writer2.write("Cloud Number: " + discoveryResultAuthority.getCloudNumber() + "\n");
        writer2.write(
            "Cloud Names: "
                + (discoveryResultAuthority.getCloudNames() == null
                    ? null
                    : Arrays.asList(discoveryResultAuthority.getCloudNames()))
                + "\n");
        writer2.write("XDI Endpoint URI: " + discoveryResultAuthority.getXdiEndpointUri() + "\n");
        writer2.write(
            "Default Endpoint URI: " + discoveryResultAuthority.getDefaultEndpointUri() + "\n");
        writer2.write(
            "Signature Public Key: " + discoveryResultAuthority.getSignaturePublicKey() + "\n");
        writer2.write(
            "Encryption Public Key: " + discoveryResultAuthority.getEncryptionPublicKey() + "\n");

        if (discoveryResultAuthority.getEndpointUris().isEmpty()) {

          writer2.write("Services: (none)\n");
        } else {

          for (Map.Entry<XDIAddress, URI> endpointUri :
              discoveryResultAuthority.getEndpointUris().entrySet()) {

            writer2.write("Service " + endpointUri.getKey() + ": " + endpointUri.getValue() + "\n");
          }
        }

        writer2.write("\n");

        writer2.write("Message envelope to authority:\n\n");

        if (discoveryResultAuthority.getMessageEnvelope() != null)
          xdiResultWriter.write(discoveryResultAuthority.getMessageEnvelope().getGraph(), writer2);
        else writer2.write("(null)");

        writer2.write("\n\n");

        writer2.write("Message result from authority:\n\n");

        if (discoveryResultAuthority.getMessagingResponse() != null)
          xdiResultWriter.write(
              discoveryResultAuthority.getMessagingResponse().getGraph(), writer2);
        else writer2.write("(null)");
      } else if (exceptionAuthority != null) {

        writer2.write("Exception from authority: " + exceptionAuthority.getMessage() + "\n");
      } else {

        writer2.write("No discovery result from authority.\n");
      }

      output = StringEscapeUtils.escapeHtml(writer.getBuffer().toString());
      output2 = StringEscapeUtils.escapeHtml(writer2.getBuffer().toString());

      if (discoveryResultRegistry != null
          && discoveryResultRegistry.getMessagingResponse() != null) {

        outputId = UUID.randomUUID().toString();
        OutputCache.put(outputId, discoveryResultRegistry.getMessagingResponse().getGraph());
      }

      if (discoveryResultAuthority != null
          && discoveryResultAuthority.getMessagingResponse() != null) {

        outputId2 = UUID.randomUUID().toString();
        OutputCache.put(outputId2, discoveryResultAuthority.getMessagingResponse().getGraph());
      }
    } catch (Exception ex) {

      if (ex instanceof Xdi2ClientException) {

        MessagingResponse messagingResponse = ((Xdi2ClientException) ex).getMessagingResponse();

        // output the message result

        if (messagingResponse != null) {

          StringWriter writer = new StringWriter();
          xdiResultWriter.write(messagingResponse.getGraph(), writer);
          output = StringEscapeUtils.escapeHtml(writer.getBuffer().toString());

          outputId = UUID.randomUUID().toString();
          OutputCache.put(outputId, messagingResponse.getGraph());
        }
      }

      log.error(ex.getMessage(), ex);
      error = ex.getMessage();
      if (error == null) error = ex.getClass().getName();
    } finally {

      /*			LoggingTrustManager.disable();

      try {

      	XDI2X509TrustManager.enableTrustAll();
      } catch (Exception ex) {

      	throw new RuntimeException(ex.getMessage(), ex);
      }*/
    }

    long stop = System.currentTimeMillis();

    stats = "";
    stats += Long.toString(stop - start) + " ms time. ";
    if (discoveryResultRegistry != null && discoveryResultRegistry.getMessagingResponse() != null)
      stats +=
          Long.toString(
                  discoveryResultRegistry
                      .getMessagingResponse()
                      .getGraph()
                      .getRootContextNode(true)
                      .getAllStatementCount())
              + " result statement(s) from registry. ";
    if (discoveryResultAuthority != null && discoveryResultAuthority.getMessagingResponse() != null)
      stats +=
          Long.toString(
                  discoveryResultAuthority
                      .getMessagingResponse()
                      .getGraph()
                      .getRootContextNode(true)
                      .getAllStatementCount())
              + " result statement(s) from authority. ";

    // display results

    request.setAttribute("sampleInputs", Integer.valueOf(sampleInputs.size()));
    request.setAttribute("resultFormat", resultFormat);
    request.setAttribute("writeImplied", writeImplied);
    request.setAttribute("writeOrdered", writeOrdered);
    request.setAttribute("writePretty", writePretty);
    request.setAttribute("input", input);
    request.setAttribute("endpoint", endpoint);
    request.setAttribute("authority", authority);
    request.setAttribute("services", services);
    request.setAttribute("output", output);
    request.setAttribute("output2", output2);
    request.setAttribute("outputId", outputId);
    request.setAttribute("outputId2", outputId2);
    request.setAttribute("discoveryResultRegistry", discoveryResultRegistry);
    request.setAttribute("discoveryResultAuthority", discoveryResultAuthority);
    request.setAttribute("stats", stats);
    request.setAttribute("error", error);

    request.getRequestDispatcher("/XDIDiscoverer.jsp").forward(request, response);
  }