use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.
the class OpalCredentialService method saveOrUpdateOpalCertificateCredential.
public void saveOrUpdateOpalCertificateCredential(String opalUrl) {
OpalCredential credential = Optional.ofNullable(repository.findOne(opalUrl)).map(c -> {
c.setAuthType(AuthType.CERTIFICATE);
c.setUsername(null);
c.setPassword(null);
c.setToken(null);
return c;
}).orElse(new OpalCredential(opalUrl, AuthType.CERTIFICATE));
repository.save(credential);
}
use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.
the class OpalCredentialService method createOrUpdateOpalCredential.
public void createOrUpdateOpalCredential(String opalUrl, String token) {
OpalCredential credential = Optional.ofNullable(repository.findOne(opalUrl)).map(c -> {
if (c.getAuthType() == AuthType.CERTIFICATE)
keyStoreService.deleteKeyPair(OpalService.OPAL_KEYSTORE, opalUrl);
c.setAuthType(AuthType.TOKEN);
c.setUsername(null);
c.setPassword(null);
c.setToken(micaConfigService.encrypt(token));
return c;
}).orElse(new OpalCredential(opalUrl, AuthType.TOKEN, micaConfigService.encrypt(token)));
repository.save(credential);
}
use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.
the class OpalService method getDatasource.
/**
* Get the datasource from the provided Opal server url.
*
* @param opalUrl if null, default Opal server url will be used.
* @param project
* @return
*/
public synchronized RestDatasource getDatasource(@Nullable String opalUrl, String project) {
final String projectUrl = getOpalProjectUrl(opalUrl, project);
opalUrl = Strings.isNullOrEmpty(opalUrl) ? getDefaultOpal() : opalUrl;
OpalCredential opalCredential = getOpalCredential(opalUrl);
if (cachedDatasources.containsKey(projectUrl)) {
Pair<OpalCredential, RestDatasource> p = cachedDatasources.get(projectUrl);
if (p.getKey().equals(opalCredential)) {
log.debug("Using cached rest datasource to " + projectUrl);
return p.getValue();
}
log.debug("Opal credential changed, evicting rest datasource for " + projectUrl);
// opal credential changed
cachedDatasources.remove(projectUrl);
}
RestDatasource datasource = createRestDatasource(opalCredential, projectUrl, opalUrl, project);
Initialisables.initialise(datasource);
cachedDatasources.put(projectUrl, Pair.create(opalCredential, datasource));
log.debug("Initialized rest datasource for " + projectUrl);
return datasource;
}
use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.
the class OpalService method getOpalJavaClient.
private OpalJavaClient getOpalJavaClient(String opalUrl) throws URISyntaxException {
String alias = opalUrl;
OpalCredential opalCredential = getOpalCredential(opalUrl);
if (opalCredential.getAuthType() == AuthType.CERTIFICATE) {
KeyStoreManager kms = keyStoreService.getKeyStore(OPAL_KEYSTORE);
if (!kms.aliasExists(alias))
throw new IllegalStateException("Trying to use opal certificate credential but could not be found in keystore.");
return new OpalJavaClient(cleanupOpalUrl(opalUrl), kms.getKeyStore(), alias, micaConfigService.getConfig().getSecretKey());
} else if (opalCredential.getAuthType() == AuthType.TOKEN)
return new OpalJavaClient(cleanupOpalUrl(opalCredential.getOpalUrl()), opalCredential.getToken());
return new OpalJavaClient(cleanupOpalUrl(opalCredential.getOpalUrl()), opalCredential.getUsername(), opalCredential.getPassword());
}
use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.
the class OpalCredentialServiceTests method testDeleteCertificateCredential.
@Test
public void testDeleteCertificateCredential() throws KeyStoreException {
OpalCredential credential = new OpalCredential("https://opal", AuthType.CERTIFICATE);
when(opalCredentialRepository.findOne("https://opal")).thenReturn(credential);
KeyStoreManager keyStore = mock(KeyStoreManager.class);
doNothing().when(keyStore).deleteKey("https://opal");
when(keyStoreService.getKeyStore("opal")).thenReturn(keyStore);
opalCredentialService.deleteOpalCredential("https://opal");
verify(opalCredentialRepository).delete(any(OpalCredential.class));
}
Aggregations