use of org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler in project hono by eclipse.
the class HonoBasicAuthHandlerTest method testPreCredentialsValidationHandlerGetsInvoked.
/**
* Verifies that the PreCredentialsValidationHandler given for the AuthHandler is invoked
* when authenticating.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testPreCredentialsValidationHandlerGetsInvoked() {
final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class);
final DeviceUser deviceUser = new DeviceUser("tenant", "device");
// prepare authProvider
final DeviceCredentialsAuthProvider<AbstractDeviceCredentials> authProvider = mock(DeviceCredentialsAuthProvider.class);
doReturn(deviceCredentials).when(authProvider).getCredentials(any(JsonObject.class));
doAnswer(invocation -> {
final Handler handler = invocation.getArgument(2);
handler.handle(Future.succeededFuture(deviceUser));
return null;
}).when(authProvider).authenticate(any(AbstractDeviceCredentials.class), any(), VertxMockSupport.anyHandler());
// prepare PreCredentialsValidationHandler
final PreCredentialsValidationHandler<HttpContext> preCredValidationHandler = mock(PreCredentialsValidationHandler.class);
when(preCredValidationHandler.handle(eq(deviceCredentials), any(HttpContext.class))).thenReturn(Future.succeededFuture());
// GIVEN an auth handler with a PreCredentialsValidationHandler
final HonoBasicAuthHandler authHandler = new HonoBasicAuthHandler(authProvider, "test", preCredValidationHandler);
// WHEN the auth handler handles a request
final String authorization = "BASIC " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8));
final MultiMap headers = mock(MultiMap.class);
when(headers.get(eq(HttpHeaders.AUTHORIZATION))).thenReturn(authorization);
final HttpServerRequest req = mock(HttpServerRequest.class);
when(req.headers()).thenReturn(headers);
final HttpServerResponse resp = mock(HttpServerResponse.class);
final RoutingContext routingContext = mock(RoutingContext.class);
final Map<String, Object> routingContextMap = new HashMap<>();
when(routingContext.put(any(), any())).thenAnswer(invocation -> {
routingContextMap.put(invocation.getArgument(0), invocation.getArgument(1));
return routingContext;
});
when(routingContext.get(any())).thenAnswer(invocation -> routingContextMap.get(invocation.getArgument(0)));
when(routingContext.request()).thenReturn(req);
when(routingContext.response()).thenReturn(resp);
when(routingContext.currentRoute()).thenReturn(mock(Route.class));
authHandler.handle(routingContext);
// THEN authentication succeeds and the PreCredentialsValidationHandler has been invoked
verify(routingContext).setUser(eq(deviceUser));
verify(preCredValidationHandler).handle(eq(deviceCredentials), any(HttpContext.class));
}
use of org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler in project hono by eclipse.
the class HonoBasicAuthHandler method authenticate.
@Override
public void authenticate(final RoutingContext context, final Handler<AsyncResult<User>> handler) {
parseAuthorization(context, parseAuthorization -> {
if (parseAuthorization.failed()) {
handler.handle(Future.failedFuture(parseAuthorization.cause()));
return;
}
final String suser;
final String spass;
try {
// decode the payload
final String decoded = new String(Base64.getDecoder().decode(parseAuthorization.result()));
final int colonIdx = decoded.indexOf(":");
if (colonIdx != -1) {
suser = decoded.substring(0, colonIdx);
spass = decoded.substring(colonIdx + 1);
} else {
suser = decoded;
spass = null;
}
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(new HttpException(400, e)));
return;
}
final var credentials = new JsonObject().put("username", suser).put("password", spass);
final ExecutionContextAuthHandler<HttpContext> authHandler = new ExecutionContextAuthHandler<>((DeviceCredentialsAuthProvider<?>) authProvider, preCredentialsValidationHandler) {
@Override
public Future<JsonObject> parseCredentials(final HttpContext context) {
return Future.succeededFuture(credentials);
}
};
authHandler.authenticateDevice(HttpContext.from(context)).map(deviceUser -> (User) deviceUser).onComplete(handler);
});
}
Aggregations