OpenStackAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { public static void addCredentialsToClaudiaData(ClaudiaData claudiaData) { PaasManagerUser paasManagerUser = new PaasManagerUser("unknown", "unknown"); if (SecurityContextHolder.getContext().getAuthentication() != null) { UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder .getContext().getAuthentication(); if (usernamePasswordAuthenticationToken != null) { paasManagerUser.setToken(usernamePasswordAuthenticationToken.getPrincipal().toString()); paasManagerUser.setTenantId(usernamePasswordAuthenticationToken.getCredentials().toString()); } } claudiaData.setUser(paasManagerUser); } OpenStackAuthenticationProvider(); final SystemPropertiesProvider getSystemPropertiesProvider(); void setSystemPropertiesProvider(SystemPropertiesProvider pSystemPropertiesProvider); void setClient(Client client); Client getClient(); void setoSAuthToken(OpenStackAuthenticationToken oSAuthToken); HttpClientConnectionManager getHttpConnectionManager(); void setHttpConnectionManager(HttpClientConnectionManager httpConnectionManager); Cache getTokenCache(); static void addCredentialsToClaudiaData(ClaudiaData claudiaData); static final int CODE_200; static final int CODE_401; }
@Test public void shouldAddCredentialsToClaudiaDataWhenAuthenticatedWithToken() { ClaudiaData claudiaData = new ClaudiaData("org", "vdc", "service"); SecurityContext context = mock(SecurityContext.class); SecurityContextHolder.setContext(context); UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = mock(UsernamePasswordAuthenticationToken.class); when(context.getAuthentication()).thenReturn(usernamePasswordAuthenticationToken); when(usernamePasswordAuthenticationToken.getPrincipal()).thenReturn("token1"); when(usernamePasswordAuthenticationToken.getCredentials()).thenReturn("tenantId1"); OpenStackAuthenticationProvider.addCredentialsToClaudiaData(claudiaData); assertNotNull(claudiaData); assertEquals("org", claudiaData.getOrg()); assertEquals("vdc", claudiaData.getVdc()); assertEquals("service", claudiaData.getService()); assertEquals("token1", claudiaData.getUser().getToken()); assertEquals("tenantId1", claudiaData.getUser().getTenantId()); assertEquals("", claudiaData.getUser().getTenantName()); }