SSLAuth extends ConsumerAuth { @Override public Principal getPrincipal(HttpRequest httpRequest) { X509Certificate[] certs = (X509Certificate[]) httpRequest.getAttribute(CERTIFICATES_ATTR); if (certs == null || certs.length < 1) { if (log.isDebugEnabled()) { log.debug("no certificate was present to authenticate the client"); } return null; } X509Certificate identityCert = certs[0]; return createPrincipal(parseUuid(identityCert)); } @Inject  SSLAuth(ConsumerCurator consumerCurator, OwnerCurator ownerCurator,
        DeletedConsumerCurator deletedConsumerCurator,
        Provider<I18n> i18nProvider); @Override Principal getPrincipal(HttpRequest httpRequest);  }
@Test public void noCert() throws Exception { assertNull(this.auth.getPrincipal(httpRequest)); }
@Test public void correctUserName() throws Exception { ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.SYSTEM); ctype.setId("test-ctype"); Owner owner = new Owner("test owner"); owner.setId(TestUtil.randomString()); Consumer consumer = new Consumer("machine_name", "test user", owner, ctype); ConsumerPrincipal expected = new ConsumerPrincipal(consumer, owner); String dn = "CN=453-44423-235"; mockCert(dn); when(this.consumerCurator.getConsumer("453-44423-235")).thenReturn(consumer); when(this.ownerCurator.findOwnerById(owner.getOwnerId())).thenReturn(owner); assertEquals(expected, this.auth.getPrincipal(httpRequest)); }
@Test public void noUuidOnCert() throws Exception { mockCert("OU=something"); when(this.consumerCurator.findByUuid(anyString())).thenReturn( new Consumer("machine_name", "test user", null, null)); assertNull(this.auth.getPrincipal(httpRequest)); }
@Test public void noValidConsumerEntity() throws Exception { mockCert("CN=235-8"); when(this.consumerCurator.findByUuid("235-8")).thenReturn(null); assertNull(this.auth.getPrincipal(httpRequest)); }