use of org.openstack4j.openstack.identity.v3.domain.TokenAuth 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;
}
Aggregations