Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #199 from junkerm/develop
Browse files Browse the repository at this point in the history
prepare version 0.1.10
  • Loading branch information
junkerm authored Jul 3, 2018
2 parents 904c820 + 0c46106 commit 86b7e60
Show file tree
Hide file tree
Showing 156 changed files with 2,478 additions and 1,466 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ hs_err_pid*
/web/dist
bundles/specmate-std-env/workspace/.metadata/.log
bundles/specmate-ui-core/webcontent/COMMITHASH
/cnf/bin/
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.osgi.service.component.annotations.Component;

import com.specmate.administration.api.ESpecmateStatus;
import com.specmate.administration.api.IStatusService;
import com.specmate.common.RestResult;
import com.specmate.common.SpecmateException;
import com.specmate.common.SpecmateValidationException;
import com.specmate.emfrest.api.IRestService;
Expand Down Expand Up @@ -49,29 +51,29 @@ public boolean canPost(Object target, EObject object) {
}

@Override
public Object get(Object target, MultivaluedMap<String, String> queryParams, String token)
public RestResult<?> get(Object target, MultivaluedMap<String, String> queryParams, String token)
throws SpecmateException {
if (target instanceof Resource) {
return statusMap.get(getCurrentStatus().getName());
return new RestResult<>(Response.Status.OK, statusMap.get(getCurrentStatus().getName()));
}
return null;
return new RestResult<>(Response.Status.BAD_REQUEST);
}

@Override
public Object post(Object target, EObject object, String token)
public RestResult<?> post(Object target, EObject object, String token)
throws SpecmateException, SpecmateValidationException {
if (target instanceof Resource) {
Status status = (Status) object;
switch (status.getValue()) {
case ESpecmateStatus.MAINTENANCE_NAME:
setCurrentStatus(ESpecmateStatus.MAINTENANCE);
return status;
return new RestResult<>(Response.Status.OK, status);
case ESpecmateStatus.NORMAL_NAME:
setCurrentStatus(ESpecmateStatus.NORMAL);
return status;
return new RestResult<>(Response.Status.OK, status);
}
}
return null;
return new RestResult<>(Response.Status.BAD_REQUEST);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface IAuthenticationService {

public void validateToken(String token, String path, boolean refresh) throws SpecmateException;

public String getUserName(String token) throws SpecmateException;

public AccessRights getSourceAccessRights(String token) throws SpecmateException;

public AccessRights getTargetAccessRights(String token) throws SpecmateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.specmate.usermodel.UserSession;

public interface ISessionService {
public UserSession create(AccessRights alm, AccessRights ppm, String projectName) throws SpecmateException;
public UserSession create(AccessRights alm, AccessRights ppm, String userName, String projectName)
throws SpecmateException;

public UserSession create();

Expand All @@ -15,6 +16,8 @@ public interface ISessionService {

public void refresh(String token) throws SpecmateException;

public String getUserName(String token) throws SpecmateException;

public AccessRights getSourceAccessRights(String token) throws SpecmateException;

public AccessRights getTargetAccessRights(String token) throws SpecmateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class InMemorySessionServiceTest {
private static ISessionService sessionService;
private static BundleContext context;
private String baseURL = "localhost/services/rest/";
private String userName = "testuser";

@BeforeClass
public static void init() throws Exception {
Expand All @@ -30,7 +31,7 @@ public static void init() throws Exception {
@Test
public void testIsAuthorized() throws SpecmateException {
String projectName = "testIsAuthorized";
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, projectName);
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, projectName);
assertTrue(sessionService.isAuthorized(session.getId(), baseURL + projectName + "/resource1"));
assertTrue(sessionService.isAuthorized(session.getId(), baseURL + projectName + "/resource1/resource2"));
assertTrue(sessionService.isAuthorized(session.getId(), baseURL + projectName + "/"));
Expand All @@ -41,32 +42,32 @@ public void testIsAuthorized() throws SpecmateException {

@Test
public void testRegexInjection() throws SpecmateException {
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, "testRegexInjection");
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, "testRegexInjection");
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "project/resource1"));
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "project/"));
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "project"));

session = sessionService.create(AccessRights.ALL, AccessRights.ALL, "");
session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, "");
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "pro/resource1"));
sessionService.delete(session.getId());

session = sessionService.create(AccessRights.ALL, AccessRights.ALL, "?");
session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, "?");
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "p/resource1"));
sessionService.delete(session.getId());

session = sessionService.create(AccessRights.ALL, AccessRights.ALL, ".*");
session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, ".*");
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "pr/resource1"));
sessionService.delete(session.getId());

session = sessionService.create(AccessRights.ALL, AccessRights.ALL, ".+");
session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, ".+");
assertFalse(sessionService.isAuthorized(session.getId(), baseURL + "pro/resource1"));
}

@Test
public void testDeleteSession() throws SpecmateException {
boolean thrown = false;
String projectName = "testDeleteSession";
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, projectName);
UserSession session = sessionService.create(AccessRights.ALL, AccessRights.ALL, userName, projectName);
assertTrue(sessionService.isAuthorized(session.getId(), baseURL + projectName + "/resource1"));
sessionService.delete(session.getId());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
package com.specmate.auth.config;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.specmate.common.OSGiUtil;
import com.specmate.common.SpecmateException;
import com.specmate.common.config.SessionServiceImplementationConfig;
import com.specmate.config.api.IConfigService;

@Component(immediate = true)
public class AuthenticationServiceConfig extends SessionServiceImplementationConfig {
public class AuthenticationServiceConfig {
/** The PID of the authentication service */
public static final String PID = "com.specmate.auth.AuthenticationServiceImpl";

@Override
/** Config key for deciding whether the sessions should be persisted or not */
public static final String SESSION_PERSISTENT = "session.persistent";

private ConfigurationAdmin configurationAdmin;
private IConfigService configService;

@Activate
public void activate() throws SpecmateException {
configureSessionImplementation();
}
Dictionary<String, Object> properties = new Hashtable<>();
boolean isPersistentSession = Boolean.parseBoolean(configService.getConfigurationProperty(SESSION_PERSISTENT));

if (isPersistentSession) {
properties.put("SessionService.target", "(impl=persistent)");
} else {
properties.put("SessionService.target", "(impl=volatile)");
}

@Override
public String getPID() {
return PID;
OSGiUtil.configureService(configurationAdmin, PID, properties);
}

@Reference
@Override
public void setConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}

@Reference
@Override
public void setConfigurationService(IConfigService configService) {
this.configService = configService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public UserSession authenticate(String username, String password, String project
}

return sessionService.create(AccessRights.ALL, retrieveTargetAccessRights(project, username, password),
projectname);
username, projectname);
}

/**
* Use this method only in tests to create a session that authorizes
* requests to all resources.
* Use this method only in tests to create a session that authorizes requests to
* all resources.
*/
@Override
public UserSession authenticate(String username, String password) throws SpecmateException {
Expand Down Expand Up @@ -65,6 +65,11 @@ public void validateToken(String token, String path, boolean refresh) throws Spe
}
}

@Override
public String getUserName(String token) throws SpecmateException {
return sessionService.getUserName(token);
}

@Override
public AccessRights getSourceAccessRights(String token) throws SpecmateException {
return sessionService.getSourceAccessRights(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ protected String sanitize(String projectName) {
return sb.toString();
}

protected UserSession createSession(AccessRights source, AccessRights target, String projectName) {
protected UserSession createSession(AccessRights source, AccessRights target, String userName, String projectName) {
UserSession session = UsermodelFactory.eINSTANCE.createUserSession();
session.setSourceSystem(source);
session.setTargetSystem(target);
session.setAllowedPathPattern(String.format(pathPattern, projectName));
session.setUserName(userName);
session.setLastActive(new Date().getTime());
String token = randomString.nextString();
session.setId(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class InMemorySessionService extends BaseSessionService {
private Map<String, UserSession> sessions = new HashMap<>();

@Override
public UserSession create(AccessRights source, AccessRights target, String projectName) {
UserSession session = createSession(source, target, sanitize(projectName));
public UserSession create(AccessRights source, AccessRights target, String userName, String projectName) {
UserSession session = createSession(source, target, userName, sanitize(projectName));
String token = session.getId();
sessions.put(token, session);
return session;
Expand All @@ -32,6 +32,7 @@ public UserSession create() {
session.setSourceSystem(AccessRights.NONE);
session.setTargetSystem(AccessRights.NONE);
session.setAllowedPathPattern(".*");
session.setUserName("unknown");
session.setLastActive(new Date().getTime());
String token = randomString.nextString();
session.setId(token);
Expand Down Expand Up @@ -75,6 +76,11 @@ public void delete(String token) throws SpecmateException {
sessions.remove(token);
}

@Override
public String getUserName(String token) throws SpecmateException {
return sessions.get(token).getUserName();
}

private void checkSessionExists(String token) throws SpecmateException {
if (!sessions.containsKey(token)) {
throw new SpecmateException("Session " + token + " does not exist.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public void deactivate() throws SpecmateException {
}

@Override
public UserSession create(AccessRights source, AccessRights target, String projectName) throws SpecmateException {
UserSession session = createSession(source, target, sanitize(projectName));
public UserSession create(AccessRights source, AccessRights target, String userName, String projectName)
throws SpecmateException {
UserSession session = createSession(source, target, userName, sanitize(projectName));
sessionTransaction.getResource().getContents().add(session);
sessionTransaction.commit();
return session;
Expand Down Expand Up @@ -116,6 +117,11 @@ public void delete(String token) throws SpecmateException {
sessionTransaction.commit();
}

@Override
public String getUserName(String token) throws SpecmateException {
return getSession(token).getUserName();
}

@Reference
public void setPersistencyService(IPersistencyService persistencyService) {
this.persistencyService = persistencyService;
Expand All @@ -139,5 +145,4 @@ private UserSession getSession(String token) throws SpecmateException {
private CDOID getSessionID(String token) throws SpecmateException {
return getSession(token).cdoID();
}

}
3 changes: 1 addition & 2 deletions bundles/specmate-common/bnd.bnd
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Bundle-Version: 0.0.0.${tstamp}
Export-Package: \
com.specmate.common,\
com.specmate.urihandler,\
com.specmate.common.config
com.specmate.urihandler
-buildpath: \
org.eclipse.osgi.services,\
org.eclipse.emf.common,\
Expand Down
30 changes: 27 additions & 3 deletions bundles/specmate-common/src/com/specmate/common/RestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import javax.ws.rs.core.Response;

public class RestResult<T> {
public class RestResult<T> {

private Response response;
private String url;
private T payload;
private String userName;
private Response.Status status;

public RestResult(Response response, String url, T payload) {
super();
Expand All @@ -15,7 +17,27 @@ public RestResult(Response response, String url, T payload) {
this.payload = payload;
}

public RestResult(Response.Status status, T payload, String userName) {
this.status = status;
this.payload = payload;
this.userName = userName;
}

public RestResult(Response.Status status, T payload) {
this(status, payload, null);
}

public RestResult(Response.Status status) {
this(status, null, null);
}

public Response getResponse() {
if (this.response == null && this.payload == null) {
return Response.status(this.status).build();
}
if (this.response == null) {
return Response.status(this.status).entity(this.payload).build();
}
return response;
}

Expand All @@ -38,7 +60,9 @@ public T getPayload() {
public void setPayload(T payload) {
this.payload = payload;
}



public String getUserName() {
return this.userName;
}

}

This file was deleted.

Loading

0 comments on commit 86b7e60

Please sign in to comment.