diff --git a/.gitignore b/.gitignore index 586adb73..514ce1ba 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ target bin .metadata -**/integrationTest.properties \ No newline at end of file +.idea +*.iml +**/integrationTest.properties diff --git a/src/main/java/com/openshift/client/IAuthorization.java b/src/main/java/com/openshift/client/IAuthorization.java new file mode 100644 index 00000000..95b7e54c --- /dev/null +++ b/src/main/java/com/openshift/client/IAuthorization.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sean Kavanagh - initial API and implementation + ******************************************************************************/ +package com.openshift.client; + +/** + * Operations to manage and view authorization resources + * + * @link http://openshift.github.io/documentation/rest_api/rest-api-1-6.html# + * authorization + * + * @author Sean Kavanagh + */ +public interface IAuthorization extends IOpenShiftResource { + + public static String SCOPE_SESSION = "session"; + public static String SCOPE_SESSION_READ = "session read"; + public static int NO_EXPIRES_IN = -1; + + /** + * Returns the unique id for this authorization. + * + * @return + */ + public String getId(); + + /** + * authorization note + * + * @return + */ + public String getNote(); + + /** + * returns the scope of the authorization token to determine type of access. + * + * @return + */ + public String getScopes(); + + /** + * Returns authorization string that contains user credentials. + * + * @return + */ + public String getToken(); + + /** + * Returns the total time in seconds before this authorization expires. + * + * @return + */ + public int getExpiresIn(); + + /** + * Destroys this authorization + * + * @throws OpenShiftException + */ + public void destroy() throws OpenShiftException; + + /** + * Refreshes the authorization by reloading its content from OpenShift. + * + * @throws OpenShiftException + */ + public void refresh() throws OpenShiftException; +} diff --git a/src/main/java/com/openshift/client/IHttpClient.java b/src/main/java/com/openshift/client/IHttpClient.java index 18325000..086032b5 100755 --- a/src/main/java/com/openshift/client/IHttpClient.java +++ b/src/main/java/com/openshift/client/IHttpClient.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * Copyright (c) 2011-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -25,6 +25,7 @@ * @author André Dietisheim * @author Nicolas Spano * @author Corey Daley + * @author Sean Kavanagh */ public interface IHttpClient { @@ -43,6 +44,7 @@ public interface IHttpClient { public static final String MEDIATYPE_APPLICATION_FORMURLENCODED = "application/x-www-form-urlencoded"; public static final String AUTHORIZATION_BASIC = "Basic"; + public static final String AUTHORIZATION_BEARER = "Bearer"; public static final int STATUS_OK = 200; public static final int STATUS_INTERNAL_SERVER_ERROR = 500; diff --git a/src/main/java/com/openshift/client/IUser.java b/src/main/java/com/openshift/client/IUser.java index 07f3c97b..88ca1f3d 100755 --- a/src/main/java/com/openshift/client/IUser.java +++ b/src/main/java/com/openshift/client/IUser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * Copyright (c) 2011-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -12,41 +12,79 @@ import java.util.List; - /** * @author André Dietisheim + * @author Sean Kavanagh */ public interface IUser extends IOpenShiftResource { - public String getId(); + public String getId(); public String getRhlogin(); public String getPassword(); public String getServer(); - + public IOpenShiftConnection getConnection(); public IDomain createDomain(String id) throws OpenShiftException; public List getDomains() throws OpenShiftException; - + public IDomain getDefaultDomain() throws OpenShiftException; - + public IDomain getDomain(String id) throws OpenShiftException; - + public boolean hasDomain() throws OpenShiftException; public boolean hasDomain(String id) throws OpenShiftException; public List getSSHKeys() throws OpenShiftException; + /** + * Returns current authorization. Creates new authorization for user if none + * exists. Authorization is set by default when token is used to create API + * connection. + * + * @return authorization + * @throws OpenShiftException + */ + public IAuthorization getAuthorization() throws OpenShiftException; + + /** + * Creates and returns new authorization set for user + * + * @param note + * A reminder description of what the authorization is for. + * @param scopes + * Scope of the authorization token to determine type of access. + * @return authorization + * @throws OpenShiftException + */ + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException; + + /** + * Creates and returns new authorization set for user + * + * @param note + * A reminder description of what the authorization is for. + * @param scopes + * Scope of the authorization token to determine type of access. + * @param expiresIn + * The number of seconds before this authorization expires. + * @return authorization + * @throws OpenShiftException + */ + public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException; + /** * Deprecated, use {@link #addSSHKey(String, ISSHPublicKey)} * - * @param name key name to use - * @param key the key to put/add + * @param name + * key name to use + * @param key + * the key to put/add * @return * @throws OpenShiftException */ @@ -54,8 +92,9 @@ public interface IUser extends IOpenShiftResource { public IOpenShiftSSHKey putSSHKey(String name, ISSHPublicKey key) throws OpenShiftException; /** - * Adds the given ssh key with the given name. Key names and public keys have to be unique. Throws - * OpenShiftSSHKeyException if either the key name or the public key are already used. + * Adds the given ssh key with the given name. Key names and public keys + * have to be unique. Throws OpenShiftSSHKeyException if either the key name + * or the public key are already used. * * @param name * the name to identify the key @@ -67,18 +106,19 @@ public interface IUser extends IOpenShiftResource { public IOpenShiftSSHKey addSSHKey(String name, ISSHPublicKey key) throws OpenShiftException; public IOpenShiftSSHKey getSSHKeyByName(String name) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException; - - public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException; + + public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException, + OpenShiftException; public boolean hasSSHKeyName(String name) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException; - + public boolean hasSSHPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException; - + public boolean removeSSHKey(String name); - + @Deprecated public void deleteKey(String name); - + public int getMaxGears(); public int getConsumedGears(); diff --git a/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java b/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java index 451e604b..e0b29380 100755 --- a/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java +++ b/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -30,10 +30,12 @@ * @author Xavier Coulon * @author Andre Dietisheim * @author Corey Daley + * @author Sean Kavanagh * */ public class OpenShiftConnectionFactory extends AbstractOpenShiftConnectionFactory { private IOpenShiftConfiguration configuration = null; + /** * Establish a connection with the clientId along with user's password. * User's login and Server URL are retrieved from the local configuration @@ -107,12 +109,16 @@ public IOpenShiftConnection getConnection(final String clientId, final String us public IOpenShiftConnection getConnection(final String clientId, final String username, final String password, final String serverUrl, ISSLCertificateCallback sslCallback) throws OpenShiftException { - return getConnection(clientId, username, password, null, null, serverUrl, sslCallback); + return getConnection(clientId, username, password, null, null, null, serverUrl, sslCallback); } + public IOpenShiftConnection getConnection(final String clientId, final String token, + final String serverUrl, ISSLCertificateCallback sslCallback) throws OpenShiftException { + return getConnection(clientId, null, null, null, null, token, serverUrl, sslCallback); + } public IOpenShiftConnection getConnection(final String clientId, final String username, final String password, final String authKey, final String authIV, final String serverUrl) throws OpenShiftException { - return getConnection(clientId, username, password, null, null, serverUrl, null); + return getConnection(clientId, username, password, null, null, null, serverUrl, null); } /** @@ -125,6 +131,8 @@ public IOpenShiftConnection getConnection(final String clientId, final String us * user's login. * @param password * user's password. + * @param token + * authorization token. * @param serverUrl * the server url. * @return a valid connection @@ -133,7 +141,7 @@ public IOpenShiftConnection getConnection(final String clientId, final String us * @throws OpenShiftException */ public IOpenShiftConnection getConnection(final String clientId, final String username, final String password, - final String authKey, final String authIV, final String serverUrl, + final String authKey, final String authIV, final String token, final String serverUrl, final ISSLCertificateCallback sslCertificateCallback) throws OpenShiftException { if (configuration == null) { try { @@ -144,24 +152,26 @@ public IOpenShiftConnection getConnection(final String clientId, final String us } Assert.notNull(clientId); - Assert.notNull(username); - Assert.notNull(password); + if (token == null || token.trim().length() == 0) { + Assert.notNull(username); + Assert.notNull(password); + } Assert.notNull(serverUrl); try { IHttpClient httpClient = new UrlConnectionHttpClientBuilder() - .setCredentials(username, password, authKey, authIV) + .setCredentials(username, password, authKey, authIV, token) .setSSLCertificateCallback(sslCertificateCallback) .setConfigTimeout(configuration.getTimeout()) .client(); - return getConnection(clientId, username, password, serverUrl, httpClient); + return getConnection(clientId, username, password, token, serverUrl, httpClient); } catch (IOException e) { throw new OpenShiftException(e, "Failed to establish connection for user ''{0}}''", username); } } - protected IOpenShiftConnection getConnection(final String clientId, final String username, final String password, + protected IOpenShiftConnection getConnection(final String clientId, final String username, final String password, final String token, final String serverUrl, IHttpClient httpClient) throws OpenShiftException, IOException { Assert.notNull(clientId); Assert.notNull(serverUrl); @@ -169,6 +179,50 @@ protected IOpenShiftConnection getConnection(final String clientId, final String IRestService service = new RestService(serverUrl, clientId, new JsonMediaType(), IHttpClient.MEDIATYPE_APPLICATION_JSON, new OpenShiftJsonDTOFactory(), httpClient); - return getConnection(service, username, password); + return getConnection(service, username, password, token); } + + /** + * Establish a connection with the clientId along with a user's authorization token + * + * @param clientId + * http client id + * @param token + * authorization token. + * @param serverUrl + * the server url. + * @return a valid connection + * @throws FileNotFoundException + * @throws IOException + * @throws OpenShiftException + */ + public IOpenShiftConnection getAuthTokenConnection(final String clientId,final String token, final String serverUrl) throws OpenShiftException { + return getConnection(clientId, null, null, null, null, token, serverUrl, null); + } + /** + * Establish a connection with the clientId along with a user's authorization + * token. Server URL is retrieved from the local configuration file (in + * see $USER_HOME/.openshift/express.conf) + * + * @param clientId + * http client id + * @param token + * authorization token. + * @return a valid connection + * @throws FileNotFoundException + * @throws IOException + * @throws OpenShiftException + */ + public IOpenShiftConnection getAuthTokenConnection(final String clientId, final String token) + throws OpenShiftException { + try { + configuration = new OpenShiftConfiguration(); + } catch (IOException e) { + throw new OpenShiftException(e, "Failed to load OpenShift configuration file."); + } + return getConnection(clientId, null, null, null, null, token, configuration.getLibraServer(), null); + } + + + } diff --git a/src/main/java/com/openshift/internal/client/APIResource.java b/src/main/java/com/openshift/internal/client/APIResource.java index 9755a3d1..9cb74f79 100755 --- a/src/main/java/com/openshift/internal/client/APIResource.java +++ b/src/main/java/com/openshift/internal/client/APIResource.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -18,6 +18,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import com.openshift.client.IAuthorization; import com.openshift.client.IDomain; import com.openshift.client.IHttpClient; import com.openshift.client.IOpenShiftConnection; @@ -31,6 +32,7 @@ import com.openshift.client.cartridge.StandaloneCartridge; import com.openshift.internal.client.httpclient.request.Parameter; import com.openshift.internal.client.httpclient.request.StringParameter; +import com.openshift.internal.client.response.AuthorizationResourceDTO; import com.openshift.internal.client.response.CartridgeResourceDTO; import com.openshift.internal.client.response.DomainResourceDTO; import com.openshift.internal.client.response.Link; @@ -44,29 +46,44 @@ /** * @author Andre Dietisheim * @author Xavier Coulon + * @author Sean Kavanagh */ public class APIResource extends AbstractOpenShiftResource implements IOpenShiftConnection { - + private static final String SYSPROPERTY_PROXY_PORT = "proxyPort"; private static final String SYSPROPERTY_PROXY_HOST = "proxyHost"; private static final String SYSPROPERTY_PROXY_SET = "proxySet"; private final String login; private final String password; + // TODO: dont rely on a single token, we could have several authorizations + // existing on the server + private final String token; private UserResource user; - //TODO: implement switch that allows to turn ssl checks on/off + private AuthorizationResource authorization; + // TODO: implement switch that allows to turn ssl checks on/off private boolean doSSLChecks = false; private List domains; private List standaloneCartridges; private List embeddableCartridges; private Map quickstartsByName; private final ExecutorService executorService; - - protected APIResource(final String login, final String password, final IRestService service, + + protected APIResource(final String token, final IRestService service, + final Map links) { + super(service, links, null); + this.login = null; + this.password = null; + this.token = token; + this.executorService = Executors.newFixedThreadPool(10); + } + + protected APIResource(final String login, final String password, final String token, final IRestService service, final Map links) { super(service, links, null); this.login = login; this.password = password; + this.token = token; this.executorService = Executors.newFixedThreadPool(10); } @@ -77,17 +94,17 @@ protected final String getLogin() { protected final String getPassword() { return password; } - + @Override public String getServer() { return getService().getPlatformUrl(); } - + public void setEnableSSLCertChecks(boolean doSSLChecks) { this.doSSLChecks = doSSLChecks; } - @Deprecated + @Deprecated public void setProxySet(boolean proxySet) { if (proxySet) { System.setProperty(SYSPROPERTY_PROXY_SET, "true"); @@ -96,12 +113,12 @@ public void setProxySet(boolean proxySet) { } } - @Deprecated + @Deprecated public void setProxyHost(String proxyHost) { System.setProperty(SYSPROPERTY_PROXY_HOST, proxyHost); } - @Deprecated + @Deprecated public void setProxyPort(String proxyPort) { Assert.notNull(proxyPort); @@ -116,6 +133,45 @@ public IUser getUser() throws OpenShiftException { return this.user; } + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException { + if (authorization != null) { + authorization.destroy(); + } + return this.authorization = createAuthorization(note, scopes, null); + } + + protected AuthorizationResource createAuthorization(String note, String scopes, Integer expiresIn) + throws OpenShiftException { + Parameters parameters = new Parameters() + .add(IOpenShiftJsonConstants.PROPERTY_NOTE, note) + .add(IOpenShiftJsonConstants.PROPERTY_SCOPES, scopes) + .add(IOpenShiftJsonConstants.PROPERTY_EXPIRES_IN, + expiresIn == null ? null : Integer.toString(expiresIn)); + return new AuthorizationResource(this, + new AddAuthorizationRequest().execute(parameters.toArray())); + } + + protected void removeAuthorization() { + this.authorization = null; + } + + protected AuthorizationResource getOrCreateAuthorization(String token) { + if (token == null) { + return createAuthorization(null, IOpenShiftJsonConstants.PROPERTY_SESSION, IAuthorization.NO_EXPIRES_IN); + } else { + return new AuthorizationResource( + this, new ShowAuthorizationRequest().execute(token)); + } + } + + public IAuthorization getAuthorization() throws OpenShiftException { + if (authorization == null) { + // TODO: if the given token is expired we get an exception here + this.authorization = getOrCreateAuthorization(token); + } + return this.authorization; + } + @Override public List getDomains() throws OpenShiftException { if (domains == null) { @@ -172,7 +228,7 @@ public IDomain showDomain(String id) throws OpenShiftException { // TODO: implement caching return domain; } - + @Override public List getStandaloneCartridges() throws OpenShiftException { return CollectionUtils.toUnmodifiableCopy(getOrLoadStandaloneCartridges()); @@ -189,7 +245,7 @@ protected List getOrLoadStandaloneCartridges() throws Open public List getEmbeddableCartridges() throws OpenShiftException { return CollectionUtils.toUnmodifiableCopy(getOrLoadEmbeddableCartridges()); } - + protected List getOrLoadEmbeddableCartridges() throws OpenShiftException { if (embeddableCartridges == null) { loadCartridges(); @@ -201,12 +257,13 @@ protected List getOrLoadEmbeddableCartridges() throws Open public List getCartridges() { List embeddableCartridges = getOrLoadEmbeddableCartridges(); List standaloneCartridges = getOrLoadStandaloneCartridges(); - List cartridges = new ArrayList(embeddableCartridges.size() + standaloneCartridges.size()); + List cartridges = new ArrayList(embeddableCartridges.size() + + standaloneCartridges.size()); cartridges.addAll(embeddableCartridges); cartridges.addAll(standaloneCartridges); return cartridges; } - + private void loadCartridges() throws OpenShiftException { final Map cartridgeDTOsByName = new GetCartridgesRequest().execute(); this.standaloneCartridges = new ArrayList(); @@ -215,7 +272,7 @@ private void loadCartridges() throws OpenShiftException { addCartridgeCartridge(cartridgeDTO, standaloneCartridges, embeddableCartridges); } } - + private void addCartridgeCartridge(CartridgeResourceDTO dto, List standaloneCartridges, List embeddableCartridges) { switch (dto.getType()) { @@ -231,7 +288,7 @@ private void addCartridgeCartridge(CartridgeResourceDTO dto, List getQuickstarts() { if (quickstartsByName == null) { this.quickstartsByName = loadQuickstarts(); @@ -269,14 +326,14 @@ private Map loadQuickstarts() throws OpenShiftException { public ExecutorService getExecutorService() { return executorService; } - + public void disconnect() { standaloneCartridges = null; embeddableCartridges = null; domains = null; executorService.shutdownNow(); } - + private class AddDomainRequest extends ServiceRequest { private AddDomainRequest() throws OpenShiftException { @@ -329,9 +386,10 @@ private ShowDomainRequest() throws OpenShiftException { protected DomainResourceDTO execute(String id) throws OpenShiftException { List urlPathParameter = new Parameters().add("name", id).toList(); - return super.execute(IHttpClient.NO_TIMEOUT, - urlPathParameter, // url path parameter - Collections.emptyList()); // request body parameter + return super.execute(IHttpClient.NO_TIMEOUT, + urlPathParameter, // url path parameter + Collections. emptyList()); // request body + // parameter } } @@ -342,8 +400,32 @@ private ListQuickstartsRequest() throws OpenShiftException { } protected List execute() throws OpenShiftException { - return super.execute(IHttpClient.NO_TIMEOUT, new QuickstartJsonDTOFactory(), Collections. emptyList(), Collections. emptyList()); + return super.execute(IHttpClient.NO_TIMEOUT, new QuickstartJsonDTOFactory(), + Collections. emptyList(), Collections. emptyList()); } } + private class AddAuthorizationRequest extends ServiceRequest { + + private AddAuthorizationRequest() throws OpenShiftException { + super("ADD_AUTHORIZATION"); + } + + protected AuthorizationResourceDTO execute(Parameter... parameters) throws OpenShiftException { + return super.execute(parameters); + } + } + + private class ShowAuthorizationRequest extends ServiceRequest { + + private ShowAuthorizationRequest() throws OpenShiftException { + super("SHOW_AUTHORIZATION"); + } + + protected AuthorizationResourceDTO execute(String id) throws OpenShiftException { + List urlPathParameter = new Parameters().add("id", id).toList(); + return (AuthorizationResourceDTO) super.execute(IHttpClient.NO_TIMEOUT, urlPathParameter, + Collections. emptyList()); + } + } } diff --git a/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java b/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java index 21ea1c34..5522f9ae 100755 --- a/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java +++ b/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -27,19 +27,20 @@ * * @author Xavier Coulon * @author Andre Dietisheim + * @author Sean Kavanagh * */ public abstract class AbstractOpenShiftConnectionFactory { @SuppressWarnings("unchecked") - protected IOpenShiftConnection getConnection(IRestService service, final String login, final String password) throws IOException, OpenShiftException { + protected IOpenShiftConnection getConnection(IRestService service, final String login, final String password, final String token) throws IOException, OpenShiftException { RestResponse response = (RestResponse) service.request( new Link("Get API", "/api", HttpMethod.GET), IHttpClient.NO_TIMEOUT, Collections. emptyList(), Collections. emptyList()); - return new APIResource(login, password, service, (Map) response.getData()); + return new APIResource(login, password, token, service, (Map) response.getData()); } } diff --git a/src/main/java/com/openshift/internal/client/AuthorizationResource.java b/src/main/java/com/openshift/internal/client/AuthorizationResource.java new file mode 100755 index 00000000..ba9b7165 --- /dev/null +++ b/src/main/java/com/openshift/internal/client/AuthorizationResource.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sean Kavanagh - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client; + +import com.openshift.client.IAuthorization; +import com.openshift.client.OpenShiftException; +import com.openshift.internal.client.httpclient.request.StringParameter; +import com.openshift.internal.client.response.AuthorizationResourceDTO; +import com.openshift.internal.client.utils.IOpenShiftJsonConstants; + +/** + * @author Sean Kavanagh + * @author Andre Dietisheim + */ +public class AuthorizationResource extends AbstractOpenShiftResource implements IAuthorization { + + private static final String LINK_GET = "GET"; + private static final String LINK_UPDATE = "UPDATE"; + private static final String LINK_DELETE = "DELETE"; + + private String id; + private String note; + private String scopes; + private String token; + private int expiresIn; + private APIResource api; + + protected AuthorizationResource(final APIResource api, AuthorizationResourceDTO authorizationDTO) { + super(api.getService(), authorizationDTO.getLinks(), authorizationDTO.getMessages()); + this.api = api; + this.id = authorizationDTO.getId(); + this.note = authorizationDTO.getNote(); + this.scopes = authorizationDTO.getScopes(); + this.token = authorizationDTO.getToken(); + this.expiresIn = authorizationDTO.getExpiresIn(); + } + + @Override + public void refresh() throws OpenShiftException { + + final AuthorizationResourceDTO authorizationDTO = new GetAuthorizationRequest().execute(); + this.id = authorizationDTO.getId(); + + this.note = authorizationDTO.getNote(); + + this.scopes = authorizationDTO.getScopes(); + this.token = authorizationDTO.getToken(); + this.expiresIn = authorizationDTO.getExpiresIn(); + } + + @Override + public String toString() { + return "Authorization [" + + "id=" + id + ", " + + "note=" + note + ", " + + "scopes=" + scopes + ", " + + "token=" + token + + "expiresIn=" + expiresIn + + "]"; + } + + @Override + public void destroy() throws OpenShiftException { + new DeleteAuthorizationRequest().execute(); + this.id = null; + this.note = null; + this.scopes = null; + this.token = null; + this.expiresIn = IAuthorization.NO_EXPIRES_IN; + api.removeAuthorization(); + } + + @Override + public String getId() { + return id; + } + + @Override + public String getNote() { + return note; + } + + @Override + public String getScopes() { + return scopes; + } + + @Override + public String getToken() { + return token; + } + + @Override + public int getExpiresIn() { + return expiresIn; + } + + private class GetAuthorizationRequest extends ServiceRequest { + + private GetAuthorizationRequest() throws OpenShiftException { + super(LINK_GET); + } + + protected AuthorizationResourceDTO execute() throws OpenShiftException { + return (AuthorizationResourceDTO) super.execute(); + } + } + + private class DeleteAuthorizationRequest extends ServiceRequest { + + private DeleteAuthorizationRequest() throws OpenShiftException { + super(LINK_DELETE); + } + + protected void execute(boolean force) throws OpenShiftException { + super.execute(new StringParameter(IOpenShiftJsonConstants.PROPERTY_FORCE, String.valueOf(force))); + } + } + +} diff --git a/src/main/java/com/openshift/internal/client/UserResource.java b/src/main/java/com/openshift/internal/client/UserResource.java index 68057411..845536a6 100755 --- a/src/main/java/com/openshift/internal/client/UserResource.java +++ b/src/main/java/com/openshift/internal/client/UserResource.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * Copyright (c) 2011-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -23,6 +23,7 @@ import com.openshift.client.OpenShiftSSHKeyException; import com.openshift.client.OpenShiftUnknonwSSHKeyTypeException; import com.openshift.client.SSHKeyType; +import com.openshift.client.IAuthorization; import com.openshift.internal.client.httpclient.request.StringParameter; import com.openshift.internal.client.response.KeyResourceDTO; import com.openshift.internal.client.response.UserResourceDTO; @@ -32,6 +33,7 @@ /** * @author André Dietisheim + * @author Sean Kavanagh */ public class UserResource extends AbstractOpenShiftResource implements IUser { @@ -124,6 +126,21 @@ public boolean hasDomain(String id) throws OpenShiftException { return api.getDomain(id) != null; } + @Override + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException { + return api.createAuthorization(note, scopes); + } + + @Override + public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException { + return api.createAuthorization(note, scopes, expiresIn); + } + + @Override + public IAuthorization getAuthorization() throws OpenShiftException { + return api.getAuthorization(); + } + @Override public void refresh() throws OpenShiftException { this.sshKeys = loadKeys(); diff --git a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java index cd7a00e3..0d983e51 100755 --- a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java +++ b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Red Hat, Inc. + * Copyright (c) 2013-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -48,6 +48,7 @@ * @author Andre Dietisheim * @author Nicolas Spano * @author Corey Daley + * @author Sean Kavanagh */ public class UrlConnectionHttpClient implements IHttpClient { @@ -58,6 +59,7 @@ public class UrlConnectionHttpClient implements IHttpClient { protected String password; protected String authKey; protected String authIV; + protected String token; protected String acceptedMediaType; protected String acceptedVersion; protected ISSLCertificateCallback sslAuthorizationCallback; @@ -70,11 +72,11 @@ public UrlConnectionHttpClient( public UrlConnectionHttpClient( String username, String password, String userAgent, String acceptedMediaType, String version, String authKey, String authIV) { - this(username, password, userAgent, acceptedMediaType, version, authKey, authIV, null,null); + this(username, password, userAgent, acceptedMediaType, version, authKey, authIV, null, null,null); } public UrlConnectionHttpClient(String username, String password, String userAgent, String acceptedMediaType, - String version, String authKey, String authIV, ISSLCertificateCallback callback, Integer configTimeout) { + String version, String authKey, String authIV, String token, ISSLCertificateCallback callback, Integer configTimeout) { this.username = username; this.password = password; this.userAgent = userAgent; @@ -82,6 +84,7 @@ public UrlConnectionHttpClient(String username, String password, String userAgen this.acceptedVersion = version; this.authKey = authKey; this.authIV = authIV; + this.token = token; this.sslAuthorizationCallback = callback; this.configTimeout = configTimeout; } @@ -138,7 +141,7 @@ protected String request(HttpMethod httpMethod, URL url, IMediaType requestMedia HttpURLConnection connection = null; try { connection = createConnection( - url, username, password, authKey, authIV, userAgent, acceptedVersion, acceptedMediaType, sslAuthorizationCallback, timeout); + url, username, password, authKey, authIV, token, userAgent, acceptedVersion, acceptedMediaType, sslAuthorizationCallback, timeout); // PATCH not yet supported by JVM if (httpMethod == HttpMethod.PATCH) { httpMethod = HttpMethod.POST; @@ -209,14 +212,14 @@ private boolean isHttps(URL url) { } protected HttpURLConnection createConnection(URL url, String username, String password, String authKey, - String authIV, String userAgent, String acceptedVersion, String acceptedMediaType, ISSLCertificateCallback callback, int timeout) + String authIV, String token, String userAgent, String acceptedVersion, String acceptedMediaType, ISSLCertificateCallback callback, int timeout) throws IOException { LOGGER.trace( - "creating connection to {} using username \"{}\" and password \"{}\"", - new Object[] { url, username, password }); + "creating connection to {} using username \"{}\" and password \"{}\" or token \"{}\"", + new Object[] { url, username, password, token }); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); setSSLCallback(url, connection); - setAuthorisation(username, password, authKey, authIV, connection); + setAuthorization(username, password, authKey, authIV, token, connection); connection.setUseCaches(false); connection.setDoInput(true); connection.setAllowUserInteraction(false); @@ -252,7 +255,7 @@ private void setAcceptHeader(String acceptedVersion, String acceptedMediaType, H connection.setRequestProperty(PROPERTY_ACCEPT, builder.toString()); } - private void setAuthorisation(String username, String password, String authKey, String authIV, + private void setAuthorization(String username, String password, String authKey, String authIV, String token, HttpURLConnection connection) { if (username == null || username.trim().length() == 0 || password == null || password.trim().length() == 0) { @@ -260,6 +263,10 @@ private void setAuthorisation(String username, String password, String authKey, connection.setRequestProperty(PROPERTY_AUTHKEY, authKey); connection.setRequestProperty(PROPERTY_AUTHIV, authIV); } + else if(token != null){ + connection.setRequestProperty(PROPERTY_AUTHORIZATION, + new StringBuilder().append(AUTHORIZATION_BEARER).append(SPACE).append(token).toString()); + } } else { String credentials = Base64Coder.encode( new StringBuilder().append(username).append(COLON).append(password).toString().getBytes()); diff --git a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java index 13f9a068..36366543 100755 --- a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java +++ b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -16,6 +16,7 @@ /** * @author André Dietisheim * @author Corey Daley + * @author Sean Kavanagh */ public class UrlConnectionHttpClientBuilder { @@ -24,6 +25,7 @@ public class UrlConnectionHttpClientBuilder { private String password; private String authKey; private String authIV; + private String token; private String acceptedMediaType; private String version; private Integer configTimeout; @@ -35,14 +37,15 @@ public UrlConnectionHttpClientBuilder setUserAgent(String userAgent) { } public UrlConnectionHttpClientBuilder setCredentials(String username, String password) { - return setCredentials(username, password, null, null); + return setCredentials(username, password, null, null, null); } - public UrlConnectionHttpClientBuilder setCredentials(String username, String password, String authKey, String authIV) { + public UrlConnectionHttpClientBuilder setCredentials(String username, String password, String authKey, String authIV, String token) { this.username = username; this.password = password; this.authKey = authKey; this.authIV = authIV; + this.token = token; return this; } public UrlConnectionHttpClientBuilder setConfigTimeout (Integer configTimeout) { @@ -67,6 +70,6 @@ public UrlConnectionHttpClientBuilder setVersion(String version) { public IHttpClient client() { return new UrlConnectionHttpClient( - username, password, userAgent, acceptedMediaType, version, authKey, authIV, callback, configTimeout); + username, password, userAgent, acceptedMediaType, version, authKey, authIV, token, callback, configTimeout); } } diff --git a/src/main/java/com/openshift/internal/client/httpclient/request/IMediaType.java b/src/main/java/com/openshift/internal/client/httpclient/request/IMediaType.java index a50e647d..0c48fbd0 100644 --- a/src/main/java/com/openshift/internal/client/httpclient/request/IMediaType.java +++ b/src/main/java/com/openshift/internal/client/httpclient/request/IMediaType.java @@ -10,7 +10,6 @@ ******************************************************************************/ package com.openshift.internal.client.httpclient.request; -import java.io.IOException; import java.io.OutputStream; import com.openshift.client.IHttpClient; diff --git a/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java new file mode 100644 index 00000000..e2f77915 --- /dev/null +++ b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sean Kavanagh - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client.response; + +import com.openshift.client.Messages; + +import java.util.Map; + +/** + * @author Sean Kavanagh + * @author Andre Dietisheim + */ +public class AuthorizationResourceDTO extends BaseResourceDTO { + + /* authorization id */ + private final String id; + /* authorization note */ + private final String note; + /* authorization scopes */ + private final String scopes; + /* authorization token */ + private final String token; + private int expiresIn; + + AuthorizationResourceDTO(final String id, String note, String scopes, String token, int expiresIn, + final Map links, + final Messages messages) { + super(links, messages); + this.id = id; + this.note = note; + this.scopes = scopes; + this.token = token; + this.expiresIn = expiresIn; + } + + public String getId() { + return id; + } + + public String getNote() { + return note; + } + + public String getScopes() { + return scopes; + } + + public String getToken() { + return token; + } + + public int getExpiresIn() { + return expiresIn; + } +} diff --git a/src/main/java/com/openshift/internal/client/response/EnumDataType.java b/src/main/java/com/openshift/internal/client/response/EnumDataType.java index 7dfcce07..d40c9d9e 100755 --- a/src/main/java/com/openshift/internal/client/response/EnumDataType.java +++ b/src/main/java/com/openshift/internal/client/response/EnumDataType.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -14,6 +14,10 @@ /** * The Enum EnumDataType. + * + * @author Xavier Coulon + * @author Andre Dieitsheim + * @author Sean Kavanagh */ public enum EnumDataType { @@ -28,6 +32,7 @@ public enum EnumDataType { domain, applications, application, + authorization, /** The embedded cartridge type. */ embedded, gear_groups, diff --git a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java index 9e923d87..3d4e3295 100755 --- a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java +++ b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -20,6 +20,7 @@ import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_DESCRIPTION; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_DISPLAY_NAME; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_DOMAIN_ID; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_EXPIRES_IN; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_FRAMEWORK; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_GEARS; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_GEAR_PROFILE; @@ -33,18 +34,24 @@ import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_MAX_GEARS; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_METHOD; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_NAME; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_NOTE; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_OPTIONAL_PARAMS; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_PROPERTIES; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_REL; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_REQUIRED_PARAMS; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_SCALABLE; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_SCOPES; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_SSH_URL; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_SUFFIX; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_TOKEN; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_TYPE; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_URL; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_UUID; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_VALID_OPTIONS; import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_VALUE; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_NOTE; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_SCOPES; +import static com.openshift.internal.client.utils.IOpenShiftJsonConstants.PROPERTY_TOKEN; import java.net.MalformedURLException; import java.net.URL; @@ -74,6 +81,7 @@ * * @author Xavier Coulon * @author Andre Dietisheim + * @author Sean Kavanagh */ public class OpenShiftJsonDTOFactory extends AbstractJsonDTOFactory { @@ -98,6 +106,8 @@ protected Object createData(EnumDataType dataType, Messages messages, ModelNode return createApplications(dataNode); case application: return createApplication(dataNode, messages); + case authorization: + return createAuthorization(dataNode, messages); case gear_groups: return createGearGroups(dataNode); case cartridges: @@ -134,6 +144,26 @@ private UserResourceDTO createUser(ModelNode userNode) throws OpenShiftException return new UserResourceDTO(id, rhlogin, maxGears, consumedGears, links); } + /** + * Creates a new ResourceDTO object. + * + * @param dataNode + * the root node + * @return the list< key resource dt o> + * @throws OpenShiftException + * the open shift exception + */ + private AuthorizationResourceDTO createAuthorization(ModelNode dataNode, Messages messages) throws OpenShiftException { + + final String id = getAsString(dataNode, PROPERTY_ID); + final String note = getAsString(dataNode, PROPERTY_NOTE); + final String scopes = getAsString(dataNode, PROPERTY_SCOPES); + final String token = getAsString(dataNode, PROPERTY_TOKEN); + final int expiresIn = getAsInteger(dataNode, PROPERTY_EXPIRES_IN); + final Map links = createLinks(dataNode.get(PROPERTY_LINKS)); + return new AuthorizationResourceDTO(id, note, scopes, token, expiresIn, links, messages); + } + /** * Creates a new ResourceDTO object. * diff --git a/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java b/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java index 0eb81193..759da9b4 100755 --- a/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java +++ b/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * Copyright (c) 2011-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -12,6 +12,7 @@ /** * @author André Dietisheim + * @author Sean Kavanagh */ public class IOpenShiftJsonConstants { @@ -52,6 +53,10 @@ public class IOpenShiftJsonConstants { public static final String PROPERTY_HREF = "href"; public static final String PROPERTY_ID = "id"; public static final String PROPERTY_INFO = "info"; + public static final String PROPERTY_NOTE= "note"; + public static final String PROPERTY_SCOPES= "scopes"; + public static final String PROPERTY_EXPIRES_IN= "expires_in"; + public static final String PROPERTY_TOKEN= "token"; public static final String PROPERTY_INITIAL_GIT_URL = "initial_git_url"; public static final String PROPERTY_INTERNAL_PORT = "internal_port"; public static final String PROPERTY_KEY_TYPE = "key_type"; @@ -96,6 +101,9 @@ public class IOpenShiftJsonConstants { public static final String PROPERTY_VALID_OPTIONS = "valid_options"; public static final String PROPERTY_VALUE = "value"; public static final String PROPERTY_WEBSITE = "website"; + public static final String PROPERTY_SESSION= "session"; + public static final String PROPERTY_READ= "read"; + public static final String PROPERTY_USERINFO= "userinfo"; public static final String VALUE_STATUS_OK = "ok"; public static final String VALUE_STATUS_CREATED = "created"; diff --git a/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java b/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java index 3886990e..e7d59691 100644 --- a/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java +++ b/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -28,6 +28,7 @@ /** * @author Andre Dietisheim * @author Nicolas Spano + * @author Sean Kavanagh */ public class PayLoadReturningHttpClientFake extends UrlConnectionHttpClient { @@ -45,6 +46,7 @@ protected PayLoadReturningHttpClientFake(OpenShiftTestConfiguration configuratio null, null, null, + null, null); } diff --git a/src/test/java/com/openshift/client/utils/TestConnectionFactory.java b/src/test/java/com/openshift/client/utils/TestConnectionFactory.java index 7d1e045a..01bda2d5 100644 --- a/src/test/java/com/openshift/client/utils/TestConnectionFactory.java +++ b/src/test/java/com/openshift/client/utils/TestConnectionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -23,6 +23,7 @@ * User Builder, used to establish a connection and retrieve a user. * * @author Andre Dietisheim + * @author Sean Kavanagh * */ public class TestConnectionFactory extends OpenShiftConnectionFactory { @@ -37,6 +38,7 @@ public IOpenShiftConnection getConnection() throws FileNotFoundException, IOExce , configuration.getPassword() , null , null + , null , configuration.getLibraServer() , new NoopSSLCertificateCallback()); } @@ -47,6 +49,7 @@ public IOpenShiftConnection getConnection(String password, String server, IHttpC configuration.getClientId(), configuration.getRhlogin(), password, + null, server, httpClient); } @@ -55,4 +58,17 @@ public IOpenShiftConnection getConnection(IHttpClient httpClient) throws FileNot OpenShiftTestConfiguration configuration = new OpenShiftTestConfiguration(); return getConnection(configuration.getPassword(), configuration.getLibraServer(), httpClient); } + + + public IOpenShiftConnection getAuthTokenConnection(String token) throws FileNotFoundException, IOException, OpenShiftException { + + OpenShiftTestConfiguration configuration = new OpenShiftTestConfiguration(); + return getConnection( + configuration.getClientId(), + token, + configuration.getLibraServer(), + new NoopSSLCertificateCallback()); + + } + } diff --git a/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java new file mode 100644 index 00000000..ea7ea287 --- /dev/null +++ b/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java @@ -0,0 +1,125 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sean Kavanagh - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +import com.openshift.client.IAuthorization; +import com.openshift.client.IOpenShiftConnection; +import com.openshift.client.IUser; +import com.openshift.client.OpenShiftException; +import com.openshift.client.utils.TestConnectionFactory; +import com.openshift.internal.client.httpclient.HttpClientException; + +/** + * @author Andre Dietisheim + */ +public class AuthorizationIntegrationTest extends TestTimer { + + // TODO: add tests for expired tokens + private IUser user; + + @Before + public void setUp() throws HttpClientException, OpenShiftException, IOException { + final IOpenShiftConnection connection = + new TestConnectionFactory().getConnection(); + this.user = connection.getUser(); + } + + @Test + public void shouldCreateGenericAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.getAuthorization(); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION); + + authorization.destroy(); + } + + @Test + public void shouldCreateAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + assertEquals(authorization.getNote(), "my note"); + + authorization.destroy(); + } + + @Test + public void shouldCreateAuthorizationWithExpiration() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ, 600); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + assertEquals(authorization.getNote(), "my note"); + assertEquals(authorization.getExpiresIn(), 600); + + authorization.destroy(); + } + + @Test + public void shouldReplaceExistingAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ, 600); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + + // operations + user.createAuthorization("new note", IAuthorization.SCOPE_SESSION); + IAuthorization newAuthorization = user.getAuthorization(); + + // verifications + assertFalse(authorization.equals(newAuthorization)); + assertEquals(newAuthorization.getScopes(), IAuthorization.SCOPE_SESSION); + assertFalse(authorization.getToken().equals(newAuthorization.getToken())); + assertEquals(newAuthorization.getNote(), "new note"); + assertTrue(newAuthorization.getExpiresIn() != 600); + + // cleanup + authorization.destroy(); + newAuthorization.destroy(); + } +} diff --git a/src/test/java/com/openshift/internal/client/AuthorizationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationTest.java new file mode 100644 index 00000000..552aabe0 --- /dev/null +++ b/src/test/java/com/openshift/internal/client/AuthorizationTest.java @@ -0,0 +1,133 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sean Kavanagh - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +import com.openshift.client.IAuthorization; +import com.openshift.client.IOpenShiftConnection; +import com.openshift.client.IUser; +import com.openshift.client.OpenShiftException; +import com.openshift.client.utils.TestConnectionFactory; +import com.openshift.internal.client.httpclient.HttpClientException; + +/** + * @author Sean Kavanagh + */ +public class AuthorizationTest extends TestTimer { + + private IUser user; + private HttpClientMockDirector mockDirector; + + @Before + public void setUp() throws HttpClientException, OpenShiftException, IOException { + this.mockDirector = new HttpClientMockDirector(); + final IOpenShiftConnection connection = + new TestConnectionFactory().getConnection(); + this.user = connection.getUser(); + } + + @Test + public void shouldCreateGenericAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.getAuthorization(); + assertNotNull(authorization.getToken()); + String token = authorization.getToken(); + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION); + assertEquals(token, authorization.getToken()); + + authorization.destroy(); + } + + @Test + public void shouldCreateAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ); + assertNotNull(authorization.getToken()); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + assertEquals(authorization.getNote(), "my note"); + + authorization.destroy(); + } + + @Test + public void shouldCreateAuthorizationWithExpiration() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ, 600); + assertNotNull(authorization.getToken()); + + // operations + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + authorization = connection.getUser().getAuthorization(); + + // verifications + assertEquals(authorization.getScopes(), IAuthorization.SCOPE_SESSION_READ); + assertEquals(authorization.getNote(), "my note"); + + authorization.destroy(); + } + + @Test + public void shouldDestroyAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ, 600); + assertNotNull(authorization.getToken()); + + // operations + authorization.destroy(); + + // verification + assertNull(authorization.getId()); + assertNull(authorization.getScopes()); + assertNull(authorization.getToken()); + assertNull(authorization.getNote()); + assertEquals(IAuthorization.NO_EXPIRES_IN, authorization.getExpiresIn()); + } + + @Test + public void shouldCreateNewAuthorization() throws Exception { + // pre-conditions + IAuthorization authorization = user.createAuthorization("my note", IAuthorization.SCOPE_SESSION_READ, 600); + assertNotNull(authorization.getToken()); + + // operations + authorization.destroy(); + + // verification + // new authorization created upon #getAuthorization() since old one is destroyed + IAuthorization newAuthorization = user.getAuthorization(); + assertFalse(authorization.equals(newAuthorization)); + } +} diff --git a/src/test/java/com/openshift/internal/client/HttpClientMockDirector.java b/src/test/java/com/openshift/internal/client/HttpClientMockDirector.java index 0455902c..dcfe3912 100644 --- a/src/test/java/com/openshift/internal/client/HttpClientMockDirector.java +++ b/src/test/java/com/openshift/internal/client/HttpClientMockDirector.java @@ -47,7 +47,7 @@ public class HttpClientMockDirector { private IHttpClient client; - public HttpClientMockDirector() throws SocketTimeoutException, HttpClientException { + public HttpClientMockDirector() throws HttpClientException, SocketTimeoutException { this.client = Mockito.mock(IHttpClient.class); mockGetAPI(Samples.GET_API) .mockGetCartridges(Samples.GET_CARTRIDGES) diff --git a/src/test/java/com/openshift/internal/client/OpenShiftIntegrationTestSuite.java b/src/test/java/com/openshift/internal/client/OpenShiftIntegrationTestSuite.java index c2b877eb..d263b08d 100755 --- a/src/test/java/com/openshift/internal/client/OpenShiftIntegrationTestSuite.java +++ b/src/test/java/com/openshift/internal/client/OpenShiftIntegrationTestSuite.java @@ -24,7 +24,8 @@ ApplicationResourceIntegrationTest.class, StandaloneCartridgesIntegrationTest.class, EmbeddedCartridgeResourceIntegrationTest.class, - EnvironmentVariableResourceIntegrationTest.class + EnvironmentVariableResourceIntegrationTest.class, + AuthorizationIntegrationTest.class }) /** * @author André Dietisheim diff --git a/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java b/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java index b240d368..7f860b87 100755 --- a/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java +++ b/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java @@ -46,7 +46,8 @@ JsonMediaTypeTest.class, EnvironmentVariableResourceTest.class, ApplicationSSHSessionTest.class, - QuickstartDTOCartridgeQueryTest.class + QuickstartDTOCartridgeQueryTest.class, + AuthorizationTest.class }) /** diff --git a/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java b/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java index 219d6ac2..42a47a1c 100755 --- a/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java +++ b/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 2012-2014 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, @@ -19,14 +19,12 @@ import java.io.FileNotFoundException; import java.io.IOException; -import java.io.Writer; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.net.URL; import java.security.KeyStoreException; import java.security.cert.X509Certificate; -import java.util.Properties; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -40,9 +38,6 @@ import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLSession; -import com.openshift.client.configuration.*; -import com.openshift.client.fakes.*; -import com.openshift.internal.client.TestTimer; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -52,16 +47,23 @@ import com.openshift.client.IHttpClient; import com.openshift.client.IHttpClient.ISSLCertificateCallback; import com.openshift.client.OpenShiftException; +import com.openshift.client.configuration.IOpenShiftConfiguration; +import com.openshift.client.fakes.HttpServerFake; +import com.openshift.client.fakes.HttpsServerFake; +import com.openshift.client.fakes.OpenShiftConfigurationFake; +import com.openshift.client.fakes.PayLoadReturningHttpClientFake; +import com.openshift.client.fakes.WaitingHttpServerFake; import com.openshift.client.utils.Base64Coder; import com.openshift.client.utils.ExceptionCauseMatcher; +import com.openshift.internal.client.TestTimer; import com.openshift.internal.client.httpclient.request.FormUrlEncodedMediaType; import com.openshift.internal.client.httpclient.request.StringParameter; -import sun.net.www.http.HttpClient; /** * @author Andre Dietisheim * @author Nicolas Spano * @author Corey Daley + * @author Sean Kavanagh */ public class HttpClientTest extends TestTimer { @@ -653,18 +655,6 @@ private WaitingHttpServerFake startWaitingHttpServerFake(int delay) throws Excep return serverFake; } - private class UserAgentClientFake extends UrlConnectionHttpClientFake { - - public UserAgentClientFake(String userAgent) { - super(userAgent, null); - } - - public String getUserAgent(HttpURLConnection connection) { - return connection.getRequestProperty(PROPERTY_USER_AGENT); - } - - } - private class AcceptVersionClientFake extends UrlConnectionHttpClientFake { public AcceptVersionClientFake(String acceptVersion) { @@ -679,17 +669,17 @@ public String getAcceptHeader(HttpURLConnection connection) { private abstract class UrlConnectionHttpClientFake extends UrlConnectionHttpClient { private UrlConnectionHttpClientFake(String userAgent, String acceptVersion) { super("username", "password", userAgent, IHttpClient.MEDIATYPE_APPLICATION_JSON, acceptVersion, - "authkey", "authiv", null,IHttpClient.NO_TIMEOUT); + "authkey", "authiv", null, null, IHttpClient.NO_TIMEOUT); } private UrlConnectionHttpClientFake(String userAgent, String acceptVersion, ISSLCertificateCallback callback) { super("username", "password", userAgent, IHttpClient.MEDIATYPE_APPLICATION_JSON, acceptVersion, - "authkey", "authiv", callback,IHttpClient.NO_TIMEOUT); + "authkey", "authiv", null, callback, IHttpClient.NO_TIMEOUT); } public HttpURLConnection createConnection() throws IOException, KeyStoreException { return super.createConnection(new URL("http://localhost"), username, password, authKey, authIV, - userAgent, acceptedVersion, acceptedMediaType, sslAuthorizationCallback, NO_TIMEOUT); + token, userAgent, acceptedVersion, acceptedMediaType, sslAuthorizationCallback, NO_TIMEOUT); } };