Search in sources :

Example 6 with AuthoritiesImpl

use of org.eclipse.hono.auth.AuthoritiesImpl in project hono by eclipse.

the class SimpleAuthenticationServerTest method testProcessRemoteOpenAddsClientAuthorities.

@SuppressWarnings("unchecked")
private void testProcessRemoteOpenAddsClientAuthorities(final String version, final Handler<Map<Symbol, Object>> connectionPropertiesAssertion) {
    final AuthoritiesImpl authorities = new AuthoritiesImpl();
    authorities.addResource("telemetry/DEFAULT_TENANT", Activity.READ);
    final HonoUser client = mock(HonoUser.class);
    when(client.getAuthorities()).thenReturn(authorities);
    when(client.getExpirationTime()).thenReturn(Instant.now().plusSeconds(60));
    when(client.getName()).thenReturn("application X");
    final Map<Symbol, Object> properties = Collections.singletonMap(AddressAuthzHelper.PROPERTY_CLIENT_VERSION, version);
    final RecordImpl attachments = new RecordImpl();
    final ProtonConnection con = mock(ProtonConnection.class);
    when(con.getRemoteDesiredCapabilities()).thenReturn(new Symbol[] { AddressAuthzHelper.CAPABILITY_ADDRESS_AUTHZ });
    when(con.attachments()).thenReturn(attachments);
    when(con.getRemoteContainer()).thenReturn("client container");
    when(con.getRemoteProperties()).thenReturn(properties);
    Constants.setClientPrincipal(con, client);
    final Vertx vertx = mock(Vertx.class);
    final SimpleAuthenticationServer server = new SimpleAuthenticationServer();
    server.init(vertx, mock(Context.class));
    server.processRemoteOpen(con);
    final ArgumentCaptor<Symbol[]> offeredCapabilitiesCaptor = ArgumentCaptor.forClass(Symbol[].class);
    verify(con).setOfferedCapabilities(offeredCapabilitiesCaptor.capture());
    assertThat(Arrays.stream(offeredCapabilitiesCaptor.getValue()).anyMatch(symbol -> symbol.equals(AddressAuthzHelper.CAPABILITY_ADDRESS_AUTHZ))).isTrue();
    final ArgumentCaptor<Map<Symbol, Object>> propsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(con).setProperties(propsCaptor.capture());
    final Map<String, String[]> authz = (Map<String, String[]>) propsCaptor.getValue().get(AddressAuthzHelper.PROPERTY_ADDRESS_AUTHZ);
    assertThat(authz).isNotNull();
    assertThat(authz.get("telemetry/DEFAULT_TENANT")).isEqualTo(new String[] { "recv" });
    connectionPropertiesAssertion.handle(propsCaptor.getValue());
}
Also used : Context(io.vertx.core.Context) Symbol(org.apache.qpid.proton.amqp.Symbol) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Vertx(io.vertx.core.Vertx) ProtonConnection(io.vertx.proton.ProtonConnection) HonoUser(org.eclipse.hono.auth.HonoUser) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) Map(java.util.Map)

Aggregations

AuthoritiesImpl (org.eclipse.hono.auth.AuthoritiesImpl)6 Authorities (org.eclipse.hono.auth.Authorities)4 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 Instant (java.time.Instant)2 Map (java.util.Map)2 Activity (org.eclipse.hono.auth.Activity)2 HonoUser (org.eclipse.hono.auth.HonoUser)2 Claims (io.jsonwebtoken.Claims)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 ProtonConnection (io.vertx.proton.ProtonConnection)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1