
org.xlcloud.iam.XAuthTokenClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iam-utils Show documentation
Show all versions of iam-utils Show documentation
This module is provides set of base IAM utilities.
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xlcloud.iam;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.xlcloud.logging.LoggingUtils;
import org.xlcloud.rest.exception.InternalErrorException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
/**
* Calls OpenAM's service to retrieve X-Auth-Token
*
* @author Jakub Wachowski, AMG.net
*/
public class XAuthTokenClient {
private final Logger LOG = Logger.getLogger(XAuthTokenClient.class);
@Inject
@IamRestClient
private Client client;
private static final String XAUTH_SERVICE_URL = IamUtils.getOpenAmUrl() + "/xlc-os/x-auth-tokens/";
private static final String ACCEPT_AUTHN_HEADER = "X-Accept-Authentication";
private static final String QUERY_PARAMS_HEADER = "X-Query-Parameters";
private static final String AUTHN_SCHEMA = "oauth";
private static final String AUTHZ_SCHEMA = "oauth";
private static final String AUTHZ_SCHEMA_SEPARATOR = ":";
@PostConstruct
public void init() {
LOG.debug("Using Jersey client " + client.hashCode());
}
/**
* Retrieves X-Auth-Token for accessToken and group synchronized with
* keystone
*
* @param accessToken
* @param groupId
* @return
*/
public String getXAuthToken(String accessToken, Long groupId) {
ClientResponse clientResponse = prepareWebResource(accessToken, groupId).get(ClientResponse.class);
LOG.debug("Getting XAuthToken for access token: " + LoggingUtils.maskPartially(accessToken) + " => "+clientResponse.getStatus());
return clientResponseToXAuthToken(clientResponse);
}
/**
* Updates X-Auth-Token in OpenAM. Returns Validation Exception in case of
* X-Auth-Token is valid in OpenAM
*
* @param accessToken
* @param groupId
* @return
*/
public String updateXAuthToken(String accessToken, Long groupId) {
ClientResponse clientResponse = prepareWebResource(accessToken, groupId).put(ClientResponse.class);
LOG.debug("Updating XAuthToken for access token: " + LoggingUtils.maskPartially(accessToken) + " => "+clientResponse.getStatus());
return clientResponseToXAuthToken(clientResponse);
}
/**
* Prepare {@link WebResource} with appropriate headers
*
* @param accessToken
* @param groupId
* @return
*/
private Builder prepareWebResource(String accessToken, Long groupId) {
Builder webResource = client.resource(XAUTH_SERVICE_URL + groupId).header(ACCEPT_AUTHN_HEADER, AUTHN_SCHEMA)
.header(QUERY_PARAMS_HEADER, AUTHZ_SCHEMA + AUTHZ_SCHEMA_SEPARATOR + accessToken);
return webResource;
}
/**
* Collects X-Auth-Token from response
*
* @param clientResponse
* @return
*/
private String clientResponseToXAuthToken(ClientResponse clientResponse) {
if (clientResponse.getStatus() == 200) {
return clientResponse.getEntity(String.class);
}
String message = "Error occured invoking openam to retrieve X-Auth-Token";
String details = clientResponse.getEntity(String.class);
LOG.error(message + ": " + details);
throw new InternalErrorException(message, InternalErrorException.ErrorType.OPENAM, details);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy