예제 #1
0
 public DeregisterResponse deregister(DeregisterRequest deregisterRequest) {
   Client unregistered = clientRegistry.deregisterClient(deregisterRequest.getRegistrationID());
   if (unregistered != null) {
     return DeregisterResponse.success();
   } else {
     LOG.debug("Invalid deregistration");
     return DeregisterResponse.notFound();
   }
 }
예제 #2
0
 public UpdateResponse update(UpdateRequest updateRequest) {
   Client client =
       clientRegistry.updateClient(
           new ClientUpdate(
               updateRequest.getRegistrationId(),
               updateRequest.getAddress(),
               updateRequest.getPort(),
               updateRequest.getLifeTimeInSec(),
               updateRequest.getSmsNumber(),
               updateRequest.getBindingMode(),
               updateRequest.getObjectLinks()));
   if (client == null) {
     return UpdateResponse.notFound();
   } else {
     return UpdateResponse.success();
   }
 }
예제 #3
0
  public RegisterResponse register(RegisterRequest registerRequest) {

    if (registerRequest.getEndpointName() == null || registerRequest.getEndpointName().isEmpty()) {
      return RegisterResponse.badRequest(null);
    } else {
      // register
      String registrationId = RegistrationHandler.createRegistrationId();

      // do we have security information for this client?
      SecurityInfo securityInfo = securityStore.getByEndpoint(registerRequest.getEndpointName());

      // which end point did the client post this request to?
      InetSocketAddress registrationEndpoint = registerRequest.getRegistrationEndpoint();

      // if this is a secure end-point, we must check that the registering client is using the right
      // identity.
      if (registerRequest.isSecure()) {
        PublicKey rpk = registerRequest.getSourcePublicKey();
        String pskIdentity = registerRequest.getPskIdentity();

        if (securityInfo == null) {
          LOG.debug(
              "A client {} without security info try to connect through the secure endpont",
              registerRequest.getEndpointName());
          return RegisterResponse.forbidden(null);
        } else if (pskIdentity != null) {
          // Manage PSK authentication
          // ----------------------------------------------------
          LOG.debug(
              "Registration request received using the secure endpoint {} with identity {}",
              registrationEndpoint,
              pskIdentity);

          if (pskIdentity == null || !pskIdentity.equals(securityInfo.getIdentity())) {
            LOG.warn(
                "Invalid identity for client {}: expected '{}' but was '{}'",
                registerRequest.getEndpointName(),
                securityInfo.getIdentity(),
                pskIdentity);
            return RegisterResponse.forbidden(null);
          } else {
            LOG.debug("authenticated client {} using DTLS PSK", registerRequest.getEndpointName());
          }
        } else if (rpk != null) {
          // Manage RPK authentication
          // ----------------------------------------------------
          if (LOG.isDebugEnabled()) {
            LOG.debug(
                "Registration request received using the secure endpoint {} with rpk {}",
                registrationEndpoint,
                Hex.encodeHexString(rpk.getEncoded()));
          }

          if (rpk == null || !rpk.equals(securityInfo.getRawPublicKey())) {
            if (LOG.isWarnEnabled()) {
              LOG.warn(
                  "Invalid rpk for client {}: expected \n'{}'\n but was \n'{}'",
                  registerRequest.getEndpointName(),
                  Hex.encodeHexString(securityInfo.getRawPublicKey().getEncoded()),
                  Hex.encodeHexString(rpk.getEncoded()));
            }
            return RegisterResponse.forbidden(null);
          } else {
            LOG.debug("authenticated client {} using DTLS RPK", registerRequest.getEndpointName());
          }
        } else {
          LOG.warn(
              "Unable to authenticate client {}: unknown authentication mode.",
              registerRequest.getEndpointName());
          return RegisterResponse.forbidden(null);
        }
      } else {
        if (securityInfo != null) {
          LOG.warn("client {} must connect using DTLS ", registerRequest.getEndpointName());
          return RegisterResponse.badRequest(null);
        }
      }

      Client client =
          new Client(
              registrationId,
              registerRequest.getEndpointName(),
              registerRequest.getSourceAddress(),
              registerRequest.getSourcePort(),
              registerRequest.getLwVersion(),
              registerRequest.getLifetime(),
              registerRequest.getSmsNumber(),
              registerRequest.getBindingMode(),
              registerRequest.getObjectLinks(),
              registrationEndpoint);

      if (clientRegistry.registerClient(client)) {
        LOG.debug("New registered client: {}", client);
        return RegisterResponse.success(client.getRegistrationId());
      } else {
        return RegisterResponse.forbidden(null);
      }
    }
  }