use of org.openstack4j.openstack.identity.v3.domain.KeystoneToken in project openstack4j by ContainX.
the class OSAuthenticator method authenticateV3.
private static OSClientV3 authenticateV3(KeystoneAuth auth, SessionInfo info, Config config) {
if (auth.getType().equals(Type.TOKENLESS)) {
Map headers = new HashMap();
Authentication.Scope.Project project = auth.getScope().getProject();
if (project != null) {
if (!isEmpty(project.getId()))
headers.put(ClientConstants.HEADER_X_PROJECT_ID, project.getId());
if (!isEmpty(project.getName()))
headers.put(ClientConstants.HEADER_X_PROJECT_NAME, project.getName());
Authentication.Scope.Domain domain = project.getDomain();
if (domain != null) {
if (!isEmpty(domain.getId()))
headers.put(ClientConstants.HEADER_X_PROJECT_DOMAIN_ID, domain.getId());
if (!isEmpty(domain.getName()))
headers.put(ClientConstants.HEADER_X_PROJECT_DOMAIN_NAME, domain.getName());
}
} else {
Authentication.Scope.Domain domain = auth.getScope().getDomain();
if (domain != null) {
if (!isEmpty(domain.getId()))
headers.put(ClientConstants.HEADER_X_DOMAIN_ID, domain.getId());
if (!isEmpty(domain.getName()))
headers.put(ClientConstants.HEADER_X_DOMAIN_NAME, domain.getName());
}
}
KeystoneToken keystoneToken = new KeystoneToken();
keystoneToken.setEndpoint(info.endpoint);
return OSClientSessionV3.createSession(keystoneToken, null, null, config).headers(headers);
}
HttpRequest<KeystoneToken> request = HttpRequest.builder(KeystoneToken.class).header(ClientConstants.HEADER_OS4J_AUTH, TOKEN_INDICATOR).endpoint(info.endpoint).method(HttpMethod.POST).path("/auth/tokens").config(config).entity(auth).build();
HttpResponse response = HttpExecutor.create().execute(request);
if (response.getStatus() >= 400) {
try {
throw mapException(response.getStatusMessage(), response.getStatus());
} finally {
HttpEntityHandler.closeQuietly(response);
}
}
KeystoneToken token = response.getEntity(KeystoneToken.class);
token.setId(response.header(ClientConstants.HEADER_X_SUBJECT_TOKEN));
if (auth.getType() == Type.CREDENTIALS) {
token = token.applyContext(info.endpoint, auth);
} else {
if (token.getProject() != null) {
token = token.applyContext(info.endpoint, new TokenAuth(token.getId(), auth.getScope().getProject().getName(), auth.getScope().getProject().getId()));
} else {
token = token.applyContext(info.endpoint, new TokenAuth(token.getId(), auth.getScope().getDomain().getName(), auth.getScope().getDomain().getId()));
}
}
String reqId = response.header(ClientConstants.X_OPENSTACK_REQUEST_ID);
if (!info.reLinkToExistingSession) {
OSClientSessionV3 v3 = OSClientSessionV3.createSession(token, info.perspective, info.provider, config);
v3.reqId = reqId;
return v3;
}
OSClientSessionV3 current = (OSClientSessionV3) OSClientSessionV3.getCurrent();
current.token = token;
current.reqId = reqId;
return current;
}
use of org.openstack4j.openstack.identity.v3.domain.KeystoneToken in project openstack4j by ContainX.
the class AbstractTest method osv3.
protected OSClientV3 osv3() {
if (osv3 == null) {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
try {
String json = new String(Streams.readAll(getClass().getResourceAsStream(JSON_TOKEN)));
LOG.info(getClass().getName());
json = json.replaceAll("devstack.openstack.stack", getHost());
KeystoneToken token = mapper.readValue(json, KeystoneToken.class);
token.setId(TOKEN_ID);
token.applyContext(authURL("/v3"), new org.openstack4j.openstack.identity.v3.domain.Credentials("admin", "test"));
osv3 = OSFactory.clientFromToken(token);
} catch (Exception e) {
e.printStackTrace();
}
}
return osv3;
}
Aggregations