public static JsonObject serialize(EndpointContext context) { JsonObject peer = Json.object(); peer.set(KEY_ADDRESS, context.getPeerAddress().getHostString()); peer.set(KEY_PORT, context.getPeerAddress().getPort()); Principal principal = context.getPeerIdentity(); if (principal != null) { if (principal instanceof PreSharedKeyIdentity) { peer.set(KEY_ID, ((PreSharedKeyIdentity) principal).getIdentity()); } else if (principal instanceof RawPublicKeyIdentity) { PublicKey publicKey = ((RawPublicKeyIdentity) principal).getKey(); peer.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded())); } else if (principal instanceof X500Principal || principal instanceof X509CertPath) { peer.set(KEY_DN, principal.getName()); } } /** copy the attributes **/ Map<String, String> attributes = context.entries(); if (!attributes.isEmpty()) { JsonObject attContext = Json.object(); for (String key : attributes.keySet()) { attContext.set(key, attributes.get(key)); } peer.set(KEY_ATTRIBUTES, attContext); } return peer; }
public static Identity extractIdentity(EndpointContext context) { InetSocketAddress peerAddress = context.getPeerAddress(); Principal senderIdentity = context.getPeerIdentity(); if (senderIdentity != null) { if (senderIdentity instanceof PreSharedKeyIdentity) { return Identity.psk(peerAddress, ((PreSharedKeyIdentity) senderIdentity).getIdentity()); } else if (senderIdentity instanceof RawPublicKeyIdentity) { PublicKey publicKey = ((RawPublicKeyIdentity) senderIdentity).getKey(); return Identity.rpk(peerAddress, publicKey); } else if (senderIdentity instanceof X500Principal || senderIdentity instanceof X509CertPath) { // Extract common name String x509CommonName = extractCN(senderIdentity.getName()); return Identity.x509(peerAddress, x509CommonName); } throw new IllegalStateException("Unable to extract sender identity : unexpected type of Principal"); } return Identity.unsecure(peerAddress); }
@Override public void receiveResponse(Response response) { CoapMessageListener listener = listeners.get(toStringAddress(response.getSourceContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(response, true)); } }
@Override public Future<Device> getAuthenticatedDevice(final CoapExchange exchange) { final Principal peer = exchange.advanced().getRequest().getSourceContext().getPeerIdentity(); if (PreSharedKeyIdentity.class.isInstance(peer)) { LOG.debug("authenticate psk identity {}", peer.getName()); final PreSharedKeyDeviceIdentity handshakeIdentity = getHandshakeIdentity( ((PreSharedKeyIdentity) peer).getIdentity()); if (handshakeIdentity != null) { final Device authorizedDevice = devices.getIfPresent(handshakeIdentity); if (authorizedDevice != null) { return Future.succeededFuture(authorizedDevice); } } return Future.failedFuture("missing device for " + peer + "!"); } return Future.failedFuture(new IllegalArgumentException("Principal not supported by this handler!")); } }
@Override public void sendRequest(Request request) { CoapMessageListener listener = listeners.get(toStringAddress(request.getDestinationContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(request, false)); } }
/** * Get authenticated device. * * @param device origin device of message. If {@code null}, the message is sent from the authenticated device. * @param exchange coap exchange with peer's principal. * @param handler handler for determined extended device */ public void getAuthenticatedExtendedDevice(final Device device, final CoapExchange exchange, final Handler<ExtendedDevice> handler) { final Principal peer = exchange.advanced().getRequest().getSourceContext().getPeerIdentity(); final CoapAuthenticationHandler authenticationHandler = getAuthenticationHandler(peer); if (authenticationHandler == null) { log.debug("device authentication handler missing for {}!", peer); exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR); } else { authenticationHandler.getAuthenticatedDevice(exchange) .compose((authorizedDevice) -> { final Device originDevice = device != null ? device : authorizedDevice; final ExtendedDevice extendedDevice = new ExtendedDevice(authorizedDevice, originDevice); log.debug("used {}", extendedDevice); handler.handle(extendedDevice); return Future.succeededFuture(); }).otherwise((error) -> { CoapErrorResponse.respond(exchange, error); return null; }); } }
public static JsonObject serialize(EndpointContext context) { JsonObject peer = Json.object(); peer.set(KEY_ADDRESS, context.getPeerAddress().getHostString()); peer.set(KEY_PORT, context.getPeerAddress().getPort()); Principal principal = context.getPeerIdentity(); if (principal != null) { if (principal instanceof PreSharedKeyIdentity) { peer.set(KEY_ID, ((PreSharedKeyIdentity) principal).getIdentity()); } else if (principal instanceof RawPublicKeyIdentity) { PublicKey publicKey = ((RawPublicKeyIdentity) principal).getKey(); peer.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded())); } else if (principal instanceof X500Principal || principal instanceof X509CertPath) { peer.set(KEY_DN, principal.getName()); } } /** copy the attributes **/ Map<String, String> attributes = context.entries(); if (!attributes.isEmpty()) { JsonObject attContext = Json.object(); for (String key : attributes.keySet()) { attContext.set(key, attributes.get(key)); } peer.set(KEY_ATTRIBUTES, attContext); } return peer; }
@Override public void sendEmptyMessage(EmptyMessage message) { CoapMessageListener listener = listeners.get(toStringAddress(message.getDestinationContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(message, false)); } }
/** * Get extended device. * * @param exchange coap exchange with URI and/or peer's principal. * @param handler handler for determined extended device */ public void getExtendedDevice(final CoapExchange exchange, final Handler<ExtendedDevice> handler) { try { final List<String> pathList = exchange.getRequestOptions().getUriPath(); final String[] path = pathList.toArray(new String[pathList.size()]); final ResourceIdentifier identifier = ResourceIdentifier.fromPath(path); final Device device = new Device(identifier.getTenantId(), identifier.getResourceId()); final Principal peer = exchange.advanced().getRequest().getSourceContext().getPeerIdentity(); if (peer == null) { final ExtendedDevice extendedDevice = new ExtendedDevice(device, device); log.debug("use {}", extendedDevice); handler.handle(extendedDevice); } else { getAuthenticatedExtendedDevice(device, exchange, handler); } } catch (NullPointerException cause) { CoapErrorResponse.respond(exchange, "missing tenant and device!", ResponseCode.BAD_REQUEST); } catch (Throwable cause) { CoapErrorResponse.respond(exchange, cause, ResponseCode.INTERNAL_SERVER_ERROR); } }
@Override public void receiveRequest(Request request) { CoapMessageListener listener = listeners.get(toStringAddress(request.getSourceContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(request, true)); } }
@Override public void receiveEmptyMessage(EmptyMessage message) { CoapMessageListener listener = listeners.get(toStringAddress(message.getSourceContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(message, true)); } }
@Override public void sendResponse(Response response) { CoapMessageListener listener = listeners .get(toStringAddress(response.getDestinationContext().getPeerAddress())); if (listener != null) { listener.trace(new CoapMessage(response, false)); } }