Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/com/openshift/client/IUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.openshift.client;

import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -52,6 +53,24 @@ public interface IUser extends IOpenShiftResource {
*/
public IAuthorization getAuthorization() throws OpenShiftException;

/**
* Returns authorization using a token or id
*
* @param id
* authorization token or id
* @return authorization
* @throws OpenShiftException
*/
public IAuthorization getAuthorization(String id) throws OpenShiftException;

/**
* Returns all current authorizations for a user
*
* @return all authorizations
* @throws OpenShiftException
*/
public Collection<IAuthorization> getAuthorizations() throws OpenShiftException;

/**
* Creates and returns new authorization set for user
*
Expand All @@ -78,6 +97,16 @@ public interface IUser extends IOpenShiftResource {
*/
public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException;

/**
* Removes authorization using a token or id
*
* @param id
* authorization token or id
* @return success or fail
* @throws OpenShiftException
*/
public boolean removeAuthorization(String id) throws OpenShiftException;

/**
* Deprecated, use {@link #addSSHKey(String, ISSHPublicKey)}
*
Expand Down
56 changes: 50 additions & 6 deletions src/main/java/com/openshift/internal/client/APIResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class APIResource extends AbstractOpenShiftResource implements IOpenShift
private List<IDomain> domains;
private List<IStandaloneCartridge> standaloneCartridges;
private List<IEmbeddableCartridge> embeddableCartridges;
private List<IAuthorization> authorizations;
private Map<String, IQuickstart> quickstartsByName;
private final ExecutorService executorService;

Expand Down Expand Up @@ -134,21 +135,26 @@ public IUser getUser() throws OpenShiftException {
}

public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException {
if (authorization != null) {
authorization.destroy();
}
return this.authorization = createAuthorization(note, scopes, null);
return createAuthorization(note, scopes, null);
}

protected AuthorizationResource createAuthorization(String note, String scopes, Integer expiresIn)
throws OpenShiftException {
if (authorizations == null){
authorizations=loadAuthorizations();
}
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,
authorization = new AuthorizationResource(this,
new AddAuthorizationRequest().execute(parameters.toArray()));

//add to cached authorizations
authorizations.add(authorization);

return authorization;
}

protected void removeAuthorization() {
Expand All @@ -165,13 +171,39 @@ protected AuthorizationResource getOrCreateAuthorization(String token) {
}

public IAuthorization getAuthorization() throws OpenShiftException {
if (authorization == null) {
if (authorization == null || authorization.getId() == null) {
// TODO: if the given token is expired we get an exception here
this.authorization = getOrCreateAuthorization(token);
}
return this.authorization;
}

public IAuthorization getAuthorization(String id) throws OpenShiftException {

for (IAuthorization authorization: getAuthorizations()) {
if (authorization.getId().equals(id) || authorization.getToken().equals(id)) {
return authorization;
}
}
return null;

}

public List<IAuthorization> getAuthorizations() throws OpenShiftException {
if (authorizations == null) {
this.authorizations = loadAuthorizations();
}
return CollectionUtils.toUnmodifiableCopy(this.authorizations);
}

private List<IAuthorization> loadAuthorizations() throws OpenShiftException {
List<IAuthorization> authorizations = new ArrayList<IAuthorization>();
for (AuthorizationResourceDTO authorizationDTO : new ListAuthorizationsRequest().execute()) {
authorizations.add(new AuthorizationResource(this, authorizationDTO));
}
return authorizations;
}

@Override
public List<IDomain> getDomains() throws OpenShiftException {
if (domains == null) {
Expand Down Expand Up @@ -323,6 +355,7 @@ private void addCartridge(CartridgeResourceDTO dto, List<IStandaloneCartridge> s
@Override
public void refresh() throws OpenShiftException {
this.domains = null;
this.authorizations = null;
}

/**
Expand Down Expand Up @@ -459,4 +492,15 @@ protected AuthorizationResourceDTO execute(String id) throws OpenShiftException
Collections.<Parameter> emptyList());
}
}

private class ListAuthorizationsRequest extends ServiceRequest {

private ListAuthorizationsRequest() throws OpenShiftException {
super("LIST_AUTHORIZATIONS");
}

protected List<AuthorizationResourceDTO> execute() throws OpenShiftException {
return super.execute();
}
}
}
29 changes: 28 additions & 1 deletion src/main/java/com/openshift/internal/client/UserResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.openshift.internal.client;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class UserResource extends AbstractOpenShiftResource implements IUser {
public UserResource(final APIResource api, final UserResourceDTO dto, final String password) {
super(api.getService(), dto.getLinks(), dto.getMessages());
this.api = api;
this.id = dto.getId();
this.id = dto.getId();
this.rhLogin = dto.getRhLogin();
this.maxGears = dto.getMaxGears();
this.consumedGears = dto.getConsumedGears();
Expand Down Expand Up @@ -141,6 +142,32 @@ public IAuthorization getAuthorization() throws OpenShiftException {
return api.getAuthorization();
}

@Override
public IAuthorization getAuthorization(String id) throws OpenShiftException {
Assert.notNull(id);
return api.getAuthorization(id);
}

@Override
public Collection<IAuthorization> getAuthorizations() throws OpenShiftException {
return api.getAuthorizations();
}

@Override
public boolean removeAuthorization(String id) {
Assert.notNull(id);
IAuthorization auth = getAuthorization(id);
if (auth == null) {
return false;
}
auth.destroy();
api.removeAuthorization();
//sets authorization list to null so next get/create will reload
api.refresh();
return true;
}


@Override
public void refresh() throws OpenShiftException {
this.sshKeys = loadKeys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum EnumDataType {
applications,
application,
authorization,
authorizations,
/** The embedded cartridge type. */
embedded,
gear_groups,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ protected Object createData(EnumDataType dataType, Messages messages, ModelNode
return createApplication(dataNode, messages);
case authorization:
return createAuthorization(dataNode, messages);
case authorizations:
return createAuthorizations(dataNode);
case gear_groups:
return createGearGroups(dataNode);
case cartridges:
Expand Down Expand Up @@ -165,6 +167,28 @@ private AuthorizationResourceDTO createAuthorization(ModelNode dataNode, Message
final Map<String, Link> links = createLinks(dataNode.get(PROPERTY_LINKS));
return new AuthorizationResourceDTO(id, note, scopes, token, expiresIn, links, messages);
}
/**
* Creates a list of authorization DTO objects.
*
* @param dataNode
* the root node
* @return the list< authorization dto>
* @throws OpenShiftException
* the open shift exception
*/
private List<AuthorizationResourceDTO> createAuthorizations(final ModelNode dataNode) throws OpenShiftException {
final List<AuthorizationResourceDTO> authorizationDtos = new ArrayList<AuthorizationResourceDTO>();
for (ModelNode authorizationNode : dataNode.asList()) {
if (authorizationNode.getType() == ModelType.OBJECT) {
AuthorizationResourceDTO dto = createAuthorization(authorizationNode, null);
if (dto != null) {
authorizationDtos.add(dto);
}
}
}

return authorizationDtos;
}

/**
* Creates a new ResourceDTO object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
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 static org.junit.Assert.assertThat;
import org.junit.internal.matchers.StringContains;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import java.util.List;
import org.junit.internal.matchers.StringContains;

import com.openshift.client.IAuthorization;
import com.openshift.client.IOpenShiftConnection;
import com.openshift.client.IOpenShiftSSHKey;
import com.openshift.client.IUser;
import com.openshift.client.OpenShiftException;
import com.openshift.client.OpenShiftEndpointException;
import com.openshift.client.IOpenShiftSSHKey;
import com.openshift.client.OpenShiftException;
import com.openshift.client.utils.TestConnectionFactory;
import com.openshift.internal.client.httpclient.HttpClientException;

Expand Down Expand Up @@ -170,7 +170,6 @@ public void shouldCheckUserInfoPermissions() throws Exception {
}
//clean up
authorization.destroy();

}

@Test
Expand Down