use of org.eclipse.californium.elements.auth.ExtensiblePrincipal in project hono by eclipse.
the class TracingSupportingHonoResource method getAuthenticatedDevice.
/**
* Gets an authenticated device's identity for a CoAP request.
*
* @param exchange The CoAP exchange with the authenticated device's principal.
* @return A future indicating the outcome of the operation.
* The future will be succeeded if the authenticated device can be determined from the CoAP exchange,
* otherwise the future will be failed with a {@link ClientErrorException}.
*/
public static Future<Device> getAuthenticatedDevice(final CoapExchange exchange) {
final Promise<Device> result = Promise.promise();
final Principal peerIdentity = exchange.advanced().getRequest().getSourceContext().getPeerIdentity();
if (peerIdentity instanceof ExtensiblePrincipal) {
final ExtensiblePrincipal<?> extPrincipal = (ExtensiblePrincipal<?>) peerIdentity;
final Device authenticatedDevice = extPrincipal.getExtendedInfo().get(DeviceInfoSupplier.EXT_INFO_KEY_HONO_DEVICE, Device.class);
if (authenticatedDevice != null) {
result.complete(authenticatedDevice);
} else {
result.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "DTLS session does not contain authenticated Device"));
}
} else {
result.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "DTLS session does not contain ExtensiblePrincipal"));
}
return result.future();
}
use of org.eclipse.californium.elements.auth.ExtensiblePrincipal in project hono by eclipse.
the class TracingSupportingHonoResource method getAuthId.
/**
* Gets the authentication identifier of a CoAP request.
*
* @param exchange The CoAP exchange with the authenticated device's principal.
* @return The authentication identifier or {@code null} if it could not be determined.
*/
public static String getAuthId(final CoapExchange exchange) {
final Principal peerIdentity = exchange.advanced().getRequest().getSourceContext().getPeerIdentity();
if (!(peerIdentity instanceof ExtensiblePrincipal)) {
return null;
}
final ExtensiblePrincipal<?> extPrincipal = (ExtensiblePrincipal<?>) peerIdentity;
return extPrincipal.getExtendedInfo().get(DeviceInfoSupplier.EXT_INFO_KEY_HONO_AUTH_ID, String.class);
}
Aggregations