use of org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV3 in project openstack4j by ContainX.
the class CustomEndpointURLResolverTest method defaultImplementation_Test.
/**
* This test verifies the default logic to retrieve the endpoint URL is
* still working. Internally it will generate the endpoint included in the
* security token.
*
* @throws IOException
*/
public void defaultImplementation_Test() throws IOException {
// create a default session containing the default Config object
final OSClientSessionV3 clientV3 = getDefaultSession();
// use default logic to generate the identity endpoint url
final String result = clientV3.getEndpoint(ServiceType.IDENTITY);
// and make sure it's the default URL
assertEquals(result, "http://127.0.0.1:5000/v3", "the message");
}
use of org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV3 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.internal.OSClientSession.OSClientSessionV3 in project openstack4j by ContainX.
the class CustomEndpointURLResolverTest method customImplementation_Test.
/**
* This test validates the custom url endpoint resolver is used when it has
* been configured in the Config class.
*
* @throws IOException
*/
public void customImplementation_Test() throws IOException {
// create the default session
final OSClientSessionV3 customConfigSession = getCustomConfigSession();
// retrieve the default endpoint
final String result = customConfigSession.getEndpoint(ServiceType.IDENTITY);
// and make sure it's the default URL
assertEquals(result, "customUrl", "the message");
}
use of org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV3 in project openstack4j by ContainX.
the class OSAuthenticator method reAuthenticate.
/**
* Re-authenticates/renews the token for the current Session
*/
@SuppressWarnings("rawtypes")
public static void reAuthenticate() {
LOG.debug("Re-Authenticating session due to expired Token or invalid response");
OSClientSession session = OSClientSession.getCurrent();
switch(session.getAuthVersion()) {
case V2:
KeystoneAccess access = ((OSClientSessionV2) session).getAccess().unwrap();
SessionInfo info = new SessionInfo(access.getEndpoint(), session.getPerspective(), true, session.getProvider());
Auth auth = (Auth) ((access.isCredentialType()) ? access.getCredentials() : access.getTokenAuth());
authenticateV2((org.openstack4j.openstack.identity.v2.domain.Auth) auth, info, session.getConfig());
break;
case V3:
default:
Token token = ((OSClientSessionV3) session).getToken();
info = new SessionInfo(token.getEndpoint(), session.getPerspective(), true, session.getProvider());
authenticateV3((KeystoneAuth) token.getCredentials(), info, session.getConfig());
break;
}
}
Aggregations