From e1b75bf5be54c7f71c2ba289483bf587b538d3f2 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Mon, 8 Sep 2014 17:15:27 -0400 Subject: [PATCH 1/7] Added authorization operations Added authorization operations and ability to get a connection with an authorization token. Authorization will be set by default to the authorization used to create the connection. --- .gitignore | 4 +- .../com/openshift/client/IAuthorization.java | 60 +++++++++ .../com/openshift/client/IHttpClient.java | 1 + src/main/java/com/openshift/client/IUser.java | 4 + .../client/OpenShiftConnectionFactory.java | 70 +++++++++-- .../internal/client/APIResource.java | 51 +++++++- .../AbstractOpenShiftConnectionFactory.java | 4 +- .../client/AuthorizationResource.java | 118 ++++++++++++++++++ .../internal/client/UserResource.java | 11 ++ .../httpclient/UrlConnectionHttpClient.java | 22 ++-- .../UrlConnectionHttpClientBuilder.java | 8 +- .../response/AuthorizationResourceDTO.java | 56 +++++++++ .../client/response/EnumDataType.java | 1 + .../response/OpenShiftJsonDTOFactory.java | 24 ++++ .../client/utils/IOpenShiftJsonConstants.java | 6 + .../fakes/PayLoadReturningHttpClientFake.java | 1 + .../client/utils/TestConnectionFactory.java | 15 +++ .../internal/client/AuthorizationTest.java | 77 ++++++++++++ .../client/httpclient/HttpClientTest.java | 6 +- 19 files changed, 511 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/openshift/client/IAuthorization.java create mode 100755 src/main/java/com/openshift/internal/client/AuthorizationResource.java create mode 100644 src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java create mode 100644 src/test/java/com/openshift/internal/client/AuthorizationTest.java 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..b8030a77 --- /dev/null +++ b/src/main/java/com/openshift/client/IAuthorization.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2012 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: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.client; + + +public interface IAuthorization extends IOpenShiftResource { + + + /** + * authorization id + * + * @return + */ + public String getId(); + + /** + * authorization note + * + * @return + */ + public String getNote(); + + + /** + * authorization scopes + * + * @return + */ + public String getScopes(); + + /** + * authorization token + * + * @return + */ + public String getToken(); + + + /** + * Destroys this authorization + * + * @throws OpenShiftException + */ + public void destroy() throws OpenShiftException; + + /** + * Refresh the authorization but 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..17f365e8 100755 --- a/src/main/java/com/openshift/client/IHttpClient.java +++ b/src/main/java/com/openshift/client/IHttpClient.java @@ -43,6 +43,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 3f420614..90a2b35a 100755 --- a/src/main/java/com/openshift/client/IUser.java +++ b/src/main/java/com/openshift/client/IUser.java @@ -42,6 +42,10 @@ public interface IUser extends IOpenShiftResource { public List getSSHKeys() throws OpenShiftException; + public IAuthorization getAuthorization() throws OpenShiftException; + + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException; + /** * Deprecated, use {@link #addSSHKey(String, ISSHPublicKey)} * diff --git a/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java b/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java index 451e604b..d30c77f5 100755 --- a/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java +++ b/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java @@ -107,12 +107,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 +129,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 +139,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 +150,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 +177,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..85c82948 100755 --- a/src/main/java/com/openshift/internal/client/APIResource.java +++ b/src/main/java/com/openshift/internal/client/APIResource.java @@ -23,6 +23,7 @@ import com.openshift.client.IOpenShiftConnection; import com.openshift.client.IQuickstart; import com.openshift.client.IUser; +import com.openshift.client.IAuthorization; import com.openshift.client.OpenShiftException; import com.openshift.client.cartridge.EmbeddableCartridge; import com.openshift.client.cartridge.ICartridge; @@ -37,6 +38,7 @@ import com.openshift.internal.client.response.QuickstartDTO; import com.openshift.internal.client.response.QuickstartJsonDTOFactory; import com.openshift.internal.client.response.UserResourceDTO; +import com.openshift.internal.client.response.AuthorizationResourceDTO; import com.openshift.internal.client.utils.Assert; import com.openshift.internal.client.utils.CollectionUtils; import com.openshift.internal.client.utils.IOpenShiftJsonConstants; @@ -53,7 +55,9 @@ public class APIResource extends AbstractOpenShiftResource implements IOpenShift private final String login; private final String password; + private final String token; private UserResource user; + private AuthorizationResource authorization; //TODO: implement switch that allows to turn ssl checks on/off private boolean doSSLChecks = false; private List domains; @@ -61,12 +65,13 @@ public class APIResource extends AbstractOpenShiftResource implements IOpenShift 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 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); } @@ -115,6 +120,26 @@ public IUser getUser() throws OpenShiftException { } return this.user; } + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException { + if (authorization != null) { + authorization.destroy(); + } + this.authorization = new AuthorizationResource(this, new AddAuthorizationRequest().execute( + new StringParameter(IOpenShiftJsonConstants.PROPERTY_NOTE, note), + new StringParameter(IOpenShiftJsonConstants.PROPERTY_SCOPES, scopes))); + return this.authorization; + } + + public IAuthorization getAuthorization() throws OpenShiftException { + if (authorization == null) { + if(token == null) { + this.authorization = new AuthorizationResource(this, new AddAuthorizationRequest().execute(new StringParameter(IOpenShiftJsonConstants.PROPERTY_SCOPES, IOpenShiftJsonConstants.PROPERTY_SESSION))); + } else { + this.authorization = new AuthorizationResource(this, new ShowAuthorizationRequest().execute(token)); + } + } + return this.authorization; + } @Override public List getDomains() throws OpenShiftException { @@ -346,4 +371,26 @@ protected List execute() throws OpenShiftException { } } + 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..a4db52fc 100755 --- a/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java +++ b/src/main/java/com/openshift/internal/client/AbstractOpenShiftConnectionFactory.java @@ -32,14 +32,14 @@ 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..68c3e273 --- /dev/null +++ b/src/main/java/com/openshift/internal/client/AuthorizationResource.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2011 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: + * Red Hat, Inc. - 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; + + +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; + + + protected AuthorizationResource(final APIResource api, AuthorizationResourceDTO authorizationDTO) { + super(api.getService(), authorizationDTO.getLinks(), authorizationDTO.getMessages()); + this.id = authorizationDTO.getId(); + this.note = authorizationDTO.getNote(); + this.scopes = authorizationDTO.getScopes(); + this.token = authorizationDTO.getToken(); + } + + + @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(); + + } + + + @Override + public String toString() { + return "Authorization [" + + "id=" + id + ", " + + "note=" + note + ", " + + "scopes=" + scopes + ", " + + "token=" + token + + "]"; + } + + public void destroy() { + destroy(false); + } + + public void destroy(boolean force) throws OpenShiftException { + new DeleteAuthorizationRequest().execute(force); + this.id=null; + this.note=null; + this.scopes=null; + this.token=null; + } + + public String getId() { + return id; + } + + public String getNote() { + return note; + } + + public String getScopes() { + return scopes; + } + + public String getToken() { + return token; + } + + + 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 5397ad85..9194ec61 100755 --- a/src/main/java/com/openshift/internal/client/UserResource.java +++ b/src/main/java/com/openshift/internal/client/UserResource.java @@ -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; @@ -117,6 +118,16 @@ 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 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..f02e67c4 100755 --- a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java +++ b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClient.java @@ -58,6 +58,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 +71,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 +83,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 +140,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 +211,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 +254,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 +262,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..6e240545 100755 --- a/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java +++ b/src/main/java/com/openshift/internal/client/httpclient/UrlConnectionHttpClientBuilder.java @@ -24,6 +24,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 +36,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 +69,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/response/AuthorizationResourceDTO.java b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java new file mode 100644 index 00000000..877f4422 --- /dev/null +++ b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2012 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: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client.response; + + +import com.openshift.client.Messages; + +import java.util.Map; + +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; + + + + AuthorizationResourceDTO(final String id, String note, String scopes, String token, final Map links, final Messages messages) { + super(links, messages); + this.id = id; + this.note=note; + this.scopes=scopes; + this.token=token; + } + + + public String getId() { + return id; + } + + public String getNote() { + return note; + } + + public String getScopes() { + return scopes; + } + + public String getToken() { + return token; + } +} 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..4647c637 100755 --- a/src/main/java/com/openshift/internal/client/response/EnumDataType.java +++ b/src/main/java/com/openshift/internal/client/response/EnumDataType.java @@ -28,6 +28,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 03129c64..d22f053b 100755 --- a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java +++ b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java @@ -45,6 +45,9 @@ 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; @@ -98,6 +101,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: @@ -133,6 +138,25 @@ private UserResourceDTO createUser(ModelNode userNode) throws OpenShiftException return new UserResourceDTO(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 Map links = createLinks(dataNode.get(PROPERTY_LINKS)); + return new AuthorizationResourceDTO(id, note, scopes, token, 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..2102b361 100755 --- a/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java +++ b/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java @@ -52,6 +52,9 @@ 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_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 +99,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..237e7d76 100644 --- a/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java +++ b/src/test/java/com/openshift/client/fakes/PayLoadReturningHttpClientFake.java @@ -45,6 +45,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..ad556f80 100644 --- a/src/test/java/com/openshift/client/utils/TestConnectionFactory.java +++ b/src/test/java/com/openshift/client/utils/TestConnectionFactory.java @@ -37,6 +37,7 @@ public IOpenShiftConnection getConnection() throws FileNotFoundException, IOExce , configuration.getPassword() , null , null + , null , configuration.getLibraServer() , new NoopSSLCertificateCallback()); } @@ -47,6 +48,7 @@ public IOpenShiftConnection getConnection(String password, String server, IHttpC configuration.getClientId(), configuration.getRhlogin(), password, + null, server, httpClient); } @@ -55,4 +57,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/AuthorizationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationTest.java new file mode 100644 index 00000000..725d1110 --- /dev/null +++ b/src/test/java/com/openshift/internal/client/AuthorizationTest.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2012 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: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.client; + +import com.openshift.client.IAuthorization; +import com.openshift.client.IOpenShiftConnection; +import com.openshift.client.IUser; +import com.openshift.client.utils.TestConnectionFactory; +import com.openshift.internal.client.httpclient.HttpClientException; +import org.junit.Before; +import org.junit.Test; + +import java.net.SocketTimeoutException; + +import static org.junit.Assert.*; + +public class AuthorizationTest extends TestTimer { + + private IUser user; + private HttpClientMockDirector mockDirector; + + @Before + public void setUp() throws SocketTimeoutException, HttpClientException, Throwable { + this.mockDirector = new HttpClientMockDirector(); + final IOpenShiftConnection connection = + new TestConnectionFactory().getConnection(); + this.user = connection.getUser(); + } + + @Test + public void shouldCreateGenericAuthorization() throws Exception { + + IAuthorization authorization = user.getAuthorization(); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), "session"); + + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + + authorization = connection.getUser().getAuthorization(); + assertEquals(authorization.getScopes(), "session"); + + authorization.destroy(); + + assertNull(authorization.getToken()); + } + + @Test + public void shouldCreateAuthorization() throws Exception { + + IAuthorization authorization = user.createAuthorization("my note", "session read"); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), "session read"); + + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + + authorization = connection.getUser().getAuthorization(); + + assertEquals(authorization.getScopes(), "session read"); + assertEquals(authorization.getNote(), "my note"); + + authorization.destroy(); + + assertNull(authorization.getToken()); + } + + +} 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..89025b2c 100755 --- a/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java +++ b/src/test/java/com/openshift/internal/client/httpclient/HttpClientTest.java @@ -679,17 +679,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); } }; From 7c57ba0cfb715b7303bbe1fc2ec2d03ea78eb8d4 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 10 Sep 2014 19:34:35 -0400 Subject: [PATCH 2/7] Updated comments and documentation Updated comments and documentation --- .../java/com/openshift/client/IAuthorization.java | 10 +++++++--- src/main/java/com/openshift/client/IUser.java | 13 +++++++++++++ .../internal/client/ApplicationResource.java | 4 ++-- .../client/response/AuthorizationResourceDTO.java | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/openshift/client/IAuthorization.java b/src/main/java/com/openshift/client/IAuthorization.java index b8030a77..e3e34dea 100644 --- a/src/main/java/com/openshift/client/IAuthorization.java +++ b/src/main/java/com/openshift/client/IAuthorization.java @@ -1,16 +1,20 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * 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: - * Red Hat, Inc. - initial API and implementation + * 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 + */ public interface IAuthorization extends IOpenShiftResource { diff --git a/src/main/java/com/openshift/client/IUser.java b/src/main/java/com/openshift/client/IUser.java index 90a2b35a..def29937 100755 --- a/src/main/java/com/openshift/client/IUser.java +++ b/src/main/java/com/openshift/client/IUser.java @@ -42,8 +42,21 @@ public interface IUser extends IOpenShiftResource { 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 + * + * @return authorization + * @throws OpenShiftException + */ public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException; /** diff --git a/src/main/java/com/openshift/internal/client/ApplicationResource.java b/src/main/java/com/openshift/internal/client/ApplicationResource.java index cc6081a6..28f225a7 100755 --- a/src/main/java/com/openshift/internal/client/ApplicationResource.java +++ b/src/main/java/com/openshift/internal/client/ApplicationResource.java @@ -1,12 +1,12 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * 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: - * Red Hat, Inc. - initial API and implementation + * Sean Kavanagh - initial API and implementation ******************************************************************************/ package com.openshift.internal.client; diff --git a/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java index 877f4422..8b2e72d6 100644 --- a/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java +++ b/src/main/java/com/openshift/internal/client/response/AuthorizationResourceDTO.java @@ -1,12 +1,12 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * 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: - * Red Hat, Inc. - initial API and implementation + * Sean Kavanagh - initial API and implementation ******************************************************************************/ package com.openshift.internal.client.response; From d50233c4491c2c0245d9c51036066d2649d17362 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 10 Sep 2014 19:35:01 -0400 Subject: [PATCH 3/7] Added authorization token constructor Added authorization token constructor --- .../java/com/openshift/internal/client/APIResource.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/openshift/internal/client/APIResource.java b/src/main/java/com/openshift/internal/client/APIResource.java index 85c82948..f72dee9c 100755 --- a/src/main/java/com/openshift/internal/client/APIResource.java +++ b/src/main/java/com/openshift/internal/client/APIResource.java @@ -66,6 +66,15 @@ public class APIResource extends AbstractOpenShiftResource implements IOpenShift private Map quickstartsByName; private final ExecutorService executorService; + 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); From 4b25c4d8bc36f60e2ea969e6b4d527ec7b9a4bd1 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 10 Sep 2014 19:37:23 -0400 Subject: [PATCH 4/7] Fixed erroneous parameter in delete request Fixed erroneous parameter in delete request --- .../openshift/internal/client/AuthorizationResource.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/openshift/internal/client/AuthorizationResource.java b/src/main/java/com/openshift/internal/client/AuthorizationResource.java index 68c3e273..17a5e185 100755 --- a/src/main/java/com/openshift/internal/client/AuthorizationResource.java +++ b/src/main/java/com/openshift/internal/client/AuthorizationResource.java @@ -62,12 +62,9 @@ public String toString() { + "]"; } - public void destroy() { - destroy(false); - } - public void destroy(boolean force) throws OpenShiftException { - new DeleteAuthorizationRequest().execute(force); + public void destroy() throws OpenShiftException { + new DeleteAuthorizationRequest().execute(); this.id=null; this.note=null; this.scopes=null; From 7ce63e712cec51a5f1008a040002e5fda334e6ea Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 10 Sep 2014 20:22:07 -0400 Subject: [PATCH 5/7] Updated comments and documentation Updated comments and documentation --- .../java/com/openshift/internal/client/AuthorizationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/openshift/internal/client/AuthorizationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationTest.java index 725d1110..bf001c19 100644 --- a/src/test/java/com/openshift/internal/client/AuthorizationTest.java +++ b/src/test/java/com/openshift/internal/client/AuthorizationTest.java @@ -1,12 +1,12 @@ /******************************************************************************* - * Copyright (c) 2012 Red Hat, Inc. + * 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: - * Red Hat, Inc. - initial API and implementation + * Sean Kavanagh - initial API and implementation ******************************************************************************/ package com.openshift.internal.client; From 1e109e8c965e8ff7852d2c61b4d25acc2e49abe2 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 10 Sep 2014 20:38:25 -0400 Subject: [PATCH 6/7] Fixed incorrectly edited file license and contributors Fixed incorrectly edited file license and contributors --- .../com/openshift/internal/client/ApplicationResource.java | 4 ++-- .../com/openshift/internal/client/AuthorizationResource.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/openshift/internal/client/ApplicationResource.java b/src/main/java/com/openshift/internal/client/ApplicationResource.java index 28f225a7..cc6081a6 100755 --- a/src/main/java/com/openshift/internal/client/ApplicationResource.java +++ b/src/main/java/com/openshift/internal/client/ApplicationResource.java @@ -1,12 +1,12 @@ /******************************************************************************* - * Copyright (c) 2014 Red Hat, Inc. + * Copyright (c) 2011 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 + * Red Hat, Inc. - initial API and implementation ******************************************************************************/ package com.openshift.internal.client; diff --git a/src/main/java/com/openshift/internal/client/AuthorizationResource.java b/src/main/java/com/openshift/internal/client/AuthorizationResource.java index 17a5e185..b6f8cb9d 100755 --- a/src/main/java/com/openshift/internal/client/AuthorizationResource.java +++ b/src/main/java/com/openshift/internal/client/AuthorizationResource.java @@ -1,12 +1,12 @@ /******************************************************************************* - * Copyright (c) 2011 Red Hat, Inc. + * 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: - * Red Hat, Inc. - initial API and implementation + * Sean Kavanagh - initial API and implementation ******************************************************************************/ package com.openshift.internal.client; From 45cbda363dffa0d890281fc0808a7643b5364234 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Wed, 17 Sep 2014 20:45:46 -0400 Subject: [PATCH 7/7] Added expires_in parameter Added expires_in parameter for the create authorization operation --- src/main/java/com/openshift/client/IUser.java | 17 ++++++++++++++++ .../internal/client/APIResource.java | 12 +++++++++++ .../internal/client/UserResource.java | 5 +++++ .../client/utils/IOpenShiftJsonConstants.java | 1 + .../internal/client/AuthorizationTest.java | 20 +++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/src/main/java/com/openshift/client/IUser.java b/src/main/java/com/openshift/client/IUser.java index def29937..e7967380 100755 --- a/src/main/java/com/openshift/client/IUser.java +++ b/src/main/java/com/openshift/client/IUser.java @@ -54,11 +54,28 @@ public interface IUser extends IOpenShiftResource { /** * 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)} * diff --git a/src/main/java/com/openshift/internal/client/APIResource.java b/src/main/java/com/openshift/internal/client/APIResource.java index f72dee9c..edd1d193 100755 --- a/src/main/java/com/openshift/internal/client/APIResource.java +++ b/src/main/java/com/openshift/internal/client/APIResource.java @@ -129,6 +129,7 @@ public IUser getUser() throws OpenShiftException { } return this.user; } + public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException { if (authorization != null) { authorization.destroy(); @@ -138,6 +139,17 @@ public IAuthorization createAuthorization(String note, String scopes) throws Ope new StringParameter(IOpenShiftJsonConstants.PROPERTY_SCOPES, scopes))); return this.authorization; } + + public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException { + if (authorization != null) { + authorization.destroy(); + } + this.authorization = new AuthorizationResource(this, new AddAuthorizationRequest().execute( + new StringParameter(IOpenShiftJsonConstants.PROPERTY_NOTE, note), + new StringParameter(IOpenShiftJsonConstants.PROPERTY_SCOPES, scopes), + new StringParameter(IOpenShiftJsonConstants.PROPERTY_EXPIRES_IN, Integer.toString(expiresIn)))); + return this.authorization; + } public IAuthorization getAuthorization() throws OpenShiftException { if (authorization == null) { diff --git a/src/main/java/com/openshift/internal/client/UserResource.java b/src/main/java/com/openshift/internal/client/UserResource.java index 9194ec61..218995ef 100755 --- a/src/main/java/com/openshift/internal/client/UserResource.java +++ b/src/main/java/com/openshift/internal/client/UserResource.java @@ -122,6 +122,11 @@ public boolean hasDomain(String id) throws OpenShiftException { 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 { 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 2102b361..c87c588a 100755 --- a/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java +++ b/src/main/java/com/openshift/internal/client/utils/IOpenShiftJsonConstants.java @@ -54,6 +54,7 @@ public class IOpenShiftJsonConstants { 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"; diff --git a/src/test/java/com/openshift/internal/client/AuthorizationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationTest.java index bf001c19..c1e0aa31 100644 --- a/src/test/java/com/openshift/internal/client/AuthorizationTest.java +++ b/src/test/java/com/openshift/internal/client/AuthorizationTest.java @@ -73,5 +73,25 @@ public void shouldCreateAuthorization() throws Exception { assertNull(authorization.getToken()); } + @Test + public void shouldCreateAuthorizationWithExpiration() throws Exception { + + IAuthorization authorization = user.createAuthorization("my note", "session read", 600); + assertNotNull(authorization.getToken()); + assertEquals(authorization.getScopes(), "session read"); + + IOpenShiftConnection connection = + new TestConnectionFactory().getAuthTokenConnection(authorization.getToken()); + + authorization = connection.getUser().getAuthorization(); + + assertEquals(authorization.getScopes(), "session read"); + assertEquals(authorization.getNote(), "my note"); + + authorization.destroy(); + + assertNull(authorization.getToken()); + } + }