Esempio n. 1
0
 /**
  * Build Authorization header for final user.
  *
  * @param username target username
  * @param password target password
  * @return target header
  */
 public static String buildAuthorization4UserName(String username, String password) {
   return " Basic " + new String(Base64.encodeAsString(username + ":" + password));
 }
Esempio n. 2
0
  /**
   * The response entity from message service is
   * {"statusCode":"000000","templateSMS":{"dateCreated":"20140827105250",
   * "smsMessageSid":"20140827105250065847"}}
   *
   * @param phoneNumber
   * @return
   * @throws NoSuchAlgorithmException
   * @throws UnsupportedEncodingException
   */
  @GET
  @Path("registration/sms")
  @Produces(MediaType.APPLICATION_JSON)
  public Response sendVerifyMessageToMobile(@QueryParam("phoneNumber") String phoneNumber)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    if (phoneNumber == null) {
      return Response.ok(
              new PhoneVerifyResponse(
                  AuthenticationConstants.MESSAGESENDFAILURE,
                  "Parameter phoneNumber provided is null",
                  null))
          .build();
    }
    Properties tzProperties = PropertiesUtils.getProperties(servletConfig.getServletContext());
    SslConfigurator sslConfig =
        SslConfigurator.newInstance()
            .trustStoreFile(tzProperties.getProperty("ssl.trust.store.file"))
            .trustStorePassword(tzProperties.getProperty("ssl.trust.store.pass"))
            .trustStoreType("JKS")
            .trustManagerFactoryAlgorithm("PKIX")
            .keyStoreFile(tzProperties.getProperty("ssl.key.store.file"))
            .keyPassword(tzProperties.getProperty("ssl.key.store.pass"))
            .keyStoreType("JKS")
            .keyManagerFactoryAlgorithm("SunX509")
            .keyStoreProvider("SUN")
            .securityProtocol("SSL");

    SSLContext sslContext = sslConfig.createSSLContext();
    Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();

    WebTarget target = client.target(tzProperties.getProperty("mobile.verify.service"));
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String accountSid = tzProperties.getProperty("mobile.account.sid");
    String encodeString = accountSid + tzProperties.getProperty("mobile.auth.token") + timeStamp;
    String authenticationString = accountSid + ":" + timeStamp;
    String sig = new EncryptUtil().md5Digest(encodeString);

    JsonObjectBuilder messageBuilder = Json.createObjectBuilder();
    JsonArrayBuilder datasBuilder = Json.createArrayBuilder();
    String verificationCode = String.valueOf(VerificationCodeGenerator.randInt(100000, 999999));
    datasBuilder.add(verificationCode).add(tzProperties.getProperty("mobile.code.active.time"));
    messageBuilder
        .add("to", phoneNumber)
        .add("appId", tzProperties.getProperty("mobile.appid"))
        .add("templateId", tzProperties.getProperty("mobile.templateid"))
        .add("datas", datasBuilder);

    Response providerResponse =
        target
            .queryParam("sig", sig)
            .request(MediaType.APPLICATION_JSON)
            .header(HttpHeaders.AUTHORIZATION, Base64.encodeAsString(authenticationString))
            .post(Entity.entity(messageBuilder.build(), MediaType.APPLICATION_JSON));
    JsonObject jsonObject = providerResponse.readEntity(JsonObject.class);
    if (jsonObject.getString("statusCode").equals("000000")) {
      TokenHolder.verificationCodeMap.put(phoneNumber, verificationCode);
      return Response.ok(
              new PhoneVerifyResponse(
                  AuthenticationConstants.MESSAGESENDSUCCESS,
                  jsonObject.toString(),
                  verificationCode))
          .build();
    } else {
      return Response.ok(
              new PhoneVerifyResponse(
                  AuthenticationConstants.MESSAGESENDFAILURE, jsonObject.toString(), null))
          .build();
    }
  }