Search in sources :

Example 1 with ExtensiblePrincipal

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();
}
Also used : ExtensiblePrincipal(org.eclipse.californium.elements.auth.ExtensiblePrincipal) Device(org.eclipse.hono.auth.Device) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Principal(java.security.Principal) ExtensiblePrincipal(org.eclipse.californium.elements.auth.ExtensiblePrincipal)

Example 2 with ExtensiblePrincipal

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);
}
Also used : ExtensiblePrincipal(org.eclipse.californium.elements.auth.ExtensiblePrincipal) Principal(java.security.Principal) ExtensiblePrincipal(org.eclipse.californium.elements.auth.ExtensiblePrincipal)

Aggregations

Principal (java.security.Principal)2 ExtensiblePrincipal (org.eclipse.californium.elements.auth.ExtensiblePrincipal)2 Device (org.eclipse.hono.auth.Device)1 ClientErrorException (org.eclipse.hono.client.ClientErrorException)1