Commit a9f7c2af authored by Sebastian's avatar Sebastian

oAuth-Kram, Bugfixes, Liebe & Weltfrieden

parent 106374ed
...@@ -49,6 +49,16 @@ public abstract class GenericDao<E extends Serializable> implements DaoInterface ...@@ -49,6 +49,16 @@ public abstract class GenericDao<E extends Serializable> implements DaoInterface
return session().createQuery(cq).getResultList(); return session().createQuery(cq).getResultList();
} }
@Transactional(readOnly=true)
public List<E> getByPlainField(String field,Object value) {
CriteriaBuilder builder = session().getCriteriaBuilder();
CriteriaQuery<E> cq = builder.createQuery(entityClass);
Root<E> root = cq.from(entityClass);
cq.select(root).where(builder.equal(root.get(field), value));
return session().createQuery(cq).getResultList();
}
@Transactional(readOnly=true) @Transactional(readOnly=true)
public E getSingleResultByField(String field,String value) { public E getSingleResultByField(String field,String value) {
......
...@@ -7,4 +7,6 @@ import de.transformationsstadt.geoportal.entities.OAuthToken; ...@@ -7,4 +7,6 @@ import de.transformationsstadt.geoportal.entities.OAuthToken;
public interface OAuthTokenDAO extends DaoInterface<OAuthToken>{ public interface OAuthTokenDAO extends DaoInterface<OAuthToken>{
public OAuthToken get(String token); public OAuthToken get(String token);
public List<OAuthToken> getTokenForUser(Long id);
} }
...@@ -13,5 +13,9 @@ public class OAuthTokenDAOImpl extends GenericDao<OAuthToken> implements OAuthTo ...@@ -13,5 +13,9 @@ public class OAuthTokenDAOImpl extends GenericDao<OAuthToken> implements OAuthTo
{ {
return getSingleResultByField("OAuthToken",token); return getSingleResultByField("OAuthToken",token);
} }
public List<OAuthToken> getTokenForUser(Long userId){
return this.getByPlainField("user",(Object) userId);
}
} }
...@@ -472,6 +472,9 @@ public class GeoElements { ...@@ -472,6 +472,9 @@ public class GeoElements {
Subject subject = SecurityUtils.getSubject(); Subject subject = SecurityUtils.getSubject();
System.out.println((String)subject.getPrincipal()); System.out.println((String)subject.getPrincipal());
if(!subject.isAuthenticated()) {
return Response.status(Status.FORBIDDEN).entity("{error:\"you need to be logged in to proceed\"}").build();
}
OsmReference existingNode = osmRefService.get(id); OsmReference existingNode = osmRefService.get(id);
if(existingNode == null) { if(existingNode == null) {
return Response.status(Status.NOT_FOUND).entity("{error:\"I know of no item with this id\"}").build(); return Response.status(Status.NOT_FOUND).entity("{error:\"I know of no item with this id\"}").build();
......
package de.transformationsstadt.geoportal.api; package de.transformationsstadt.geoportal.api;
import java.awt.List;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.security.Provider.Service; import java.security.Provider.Service;
...@@ -108,9 +109,11 @@ public class OAuth { ...@@ -108,9 +109,11 @@ public class OAuth {
} }
OAuthToken t = tokenService.get(token.getValue()); OAuthToken t = tokenService.get(token.getValue());
if(t == null) { if(t == null) {
return Response.status(Status.NOT_FOUND).entity(null).build(); return Response.status(Status.NOT_FOUND).entity("{\"error\":\"token not found\"}").build();
}
if(t.getUser() == null) {
return Response.status(Status.NOT_FOUND).entity("{\"error\":\"user not found\"}").build();
} }
HashMap<String,String> loginData = createBearer(request,t.getUser()); HashMap<String,String> loginData = createBearer(request,t.getUser());
return Response.status(Status.OK).entity(loginData).build(); return Response.status(Status.OK).entity(loginData).build();
} }
...@@ -226,9 +229,10 @@ public class OAuth { ...@@ -226,9 +229,10 @@ public class OAuth {
URI frontend = null; URI frontend = null;
if(t.getOauthTokenVerifier() != null && !t.getOauthTokenVerifier().isEmpty()) { if(t.getOauthTokenVerifier() != null && !t.getOauthTokenVerifier().isEmpty()) {
try { try {
frontend = new URI("http", null, "localhost", 4200, "", null, "/login;rt="+t.getOauthToken()); getUserCredentials(t);
frontend = new URI("http", null, "localhost", 4200, "", null, "/login;rt="+t.getOauthToken());
//return Response.temporaryRedirect(frontend).build(); //return Response.temporaryRedirect(frontend).build();
return Response.temporaryRedirect(frontend).build(); return Response.temporaryRedirect(frontend).build();
}catch(Exception e) { }catch(Exception e) {
return Response.status(Status.BAD_REQUEST).entity(null).build(); return Response.status(Status.BAD_REQUEST).entity(null).build();
} }
...@@ -253,7 +257,10 @@ public class OAuth { ...@@ -253,7 +257,10 @@ public class OAuth {
OAuth1AccessToken generated = t.getOAuth1AccessToken(); OAuth1AccessToken generated = t.getOAuth1AccessToken();
tokenService.update(t); tokenService.update(t);
frontend = new URI("http", null, "localhost", 4200, "", null, "/login;rt="+t.getOauthToken()); frontend = new URI("http", null, "localhost", 4200, "", null, "/login;rt="+t.getOauthToken());
getUserCredentials(t);
return Response.temporaryRedirect(frontend).build(); return Response.temporaryRedirect(frontend).build();
//return Response.status(Status.OK).entity(getUserCredentials(t)).build(); //return Response.status(Status.OK).entity(getUserCredentials(t)).build();
}catch(Exception e) { }catch(Exception e) {
if(Settings.getInstance().debugMode()) { if(Settings.getInstance().debugMode()) {
...@@ -263,7 +270,27 @@ public class OAuth { ...@@ -263,7 +270,27 @@ public class OAuth {
} }
//return Response.status(Status.OK).entity(t).build(); //return Response.status(Status.OK).entity(t).build();
} }
/*
@GET
@Path("/revoke/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response revoke() {
// das scheint es in oAuth1.0a nicht zu geben
// eventuell ginge das, wenn man sich die berechtigung abholt,
// die benutzereinstellungen zu ändern.
ArrayList<OAuthToken> tokens = (ArrayList)tokenService.getTokenForUser(new Long(1255));
ArrayList<String> tokenStrings = new ArrayList<String>();
OAuthToken t = tokens.get(0);
OAuth10aService service = (new ConfiguredOAuthApi10a()).getService();
for(OAuthToken token : tokens) {
tokenStrings.add(token.getAccessToken());
}
return Response.status(Status.OK).entity(tokenStrings).build();
}
*/
@POST @POST
@Path("/run/") @Path("/run/")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
......
...@@ -127,6 +127,14 @@ public class OsmNodeParameter { ...@@ -127,6 +127,14 @@ public class OsmNodeParameter {
public void setPeers(List<OsmNodeParameter> peers) { public void setPeers(List<OsmNodeParameter> peers) {
this.peers = peers; this.peers = peers;
} }
public Boolean hasPeer(Long id) {
for(OsmNodeParameter p : this.peers) {
if(p.getId().equals(id)) {
return true;
}
}
return false;
}
String osmType; String osmType;
Long osmId; Long osmId;
List<KeyValuePairParameter> osmtags = new ArrayList<KeyValuePairParameter>(); List<KeyValuePairParameter> osmtags = new ArrayList<KeyValuePairParameter>();
......
...@@ -21,6 +21,7 @@ public class OsmReferenceParameter { ...@@ -21,6 +21,7 @@ public class OsmReferenceParameter {
private ArrayList<Long> dataGroups = new ArrayList<Long>(); private ArrayList<Long> dataGroups = new ArrayList<Long>();
private ArrayList<Long> peers = new ArrayList<Long>(); private ArrayList<Long> peers = new ArrayList<Long>();
private ArrayList<Long> bliDimensionIds = new ArrayList<Long>(); private ArrayList<Long> bliDimensionIds = new ArrayList<Long>();
ArrayList<Tag> tags = new ArrayList<Tag>(); ArrayList<Tag> tags = new ArrayList<Tag>();
Double lon; Double lon;
Double lat; Double lat;
......
...@@ -11,7 +11,7 @@ public interface OAuthTokenService extends ServiceInterface<OAuthToken>{ ...@@ -11,7 +11,7 @@ public interface OAuthTokenService extends ServiceInterface<OAuthToken>{
public OAuthToken create(OAuthToken t); public OAuthToken create(OAuthToken t);
public List<OAuthToken> getAll(); public List<OAuthToken> getAll();
public List<OAuthToken> getTokenForUser(Long id);
public OAuthToken merge(OAuthToken t); public OAuthToken merge(OAuthToken t);
public OAuthToken update(OAuthToken t); public OAuthToken update(OAuthToken t);
......
...@@ -21,6 +21,7 @@ public class OAuthTokenServiceImpl extends GenericService<OAuthToken> implements ...@@ -21,6 +21,7 @@ public class OAuthTokenServiceImpl extends GenericService<OAuthToken> implements
public OAuthToken get(Long id) { public OAuthToken get(Long id) {
return dao.get(id); return dao.get(id);
} }
public OAuthToken get(String t) { public OAuthToken get(String t) {
return dao.getSingleResultByField("oauthToken", t); return dao.getSingleResultByField("oauthToken", t);
} }
...@@ -43,4 +44,9 @@ public class OAuthTokenServiceImpl extends GenericService<OAuthToken> implements ...@@ -43,4 +44,9 @@ public class OAuthTokenServiceImpl extends GenericService<OAuthToken> implements
t = dao.update(t); t = dao.update(t);
return t; return t;
} }
@Override
public List<OAuthToken> getTokenForUser(Long id) {
return dao.getTokenForUser(id);
}
} }
...@@ -14,11 +14,13 @@ import de.transformationsstadt.geoportal.apiparameters.BliDimensionParameter; ...@@ -14,11 +14,13 @@ import de.transformationsstadt.geoportal.apiparameters.BliDimensionParameter;
import de.transformationsstadt.geoportal.apiparameters.DataGroupParameter; import de.transformationsstadt.geoportal.apiparameters.DataGroupParameter;
import de.transformationsstadt.geoportal.apiparameters.KeyValuePairParameter; import de.transformationsstadt.geoportal.apiparameters.KeyValuePairParameter;
import de.transformationsstadt.geoportal.apiparameters.OsmNodeParameter; import de.transformationsstadt.geoportal.apiparameters.OsmNodeParameter;
import de.transformationsstadt.geoportal.apiparameters.OsmReferenceParameter;
import de.transformationsstadt.geoportal.entities.BliDimension; import de.transformationsstadt.geoportal.entities.BliDimension;
import de.transformationsstadt.geoportal.entities.DataGroup; import de.transformationsstadt.geoportal.entities.DataGroup;
import de.transformationsstadt.geoportal.entities.KeyValuePair; import de.transformationsstadt.geoportal.entities.KeyValuePair;
import de.transformationsstadt.geoportal.entities.OsmReference; import de.transformationsstadt.geoportal.entities.OsmReference;
import de.transformationsstadt.geoportal.entities.Rectangle; import de.transformationsstadt.geoportal.entities.Rectangle;
import de.westnordost.osmapi.map.data.OsmNode;
@Service @Service
@Transactional @Transactional
...@@ -123,22 +125,39 @@ public class OsmReferenceServiceImpl extends GenericService<OsmReference> implem ...@@ -123,22 +125,39 @@ public class OsmReferenceServiceImpl extends GenericService<OsmReference> implem
* *
* Achtung: bisher nicht-persistierte Tags ({@link KeyValuePair}) werden dabei persistent. * Achtung: bisher nicht-persistierte Tags ({@link KeyValuePair}) werden dabei persistent.
* *
* @param p {@link OsmNodeParameter} * @param updated {@link OsmNodeParameter}
*/ */
public OsmReference update(OsmReference ref,OsmNodeParameter p) { public OsmReference update(OsmReference existing,OsmNodeParameter updated) {
if(ref == null) { if(existing == null) {
return null; return null;
} }
// rufe jeden peer ab
dao.merge(fromOsmNodeParameter(ref,p,false)); ArrayList<OsmReference> removedPeers = new ArrayList<OsmReference>();
for(OsmReference existingPeer: existing.getPeers()) {
for(OsmReference peer :ref.getPeers()) { if(!updated.hasPeer(existingPeer.getId())) {
if(!peer.hasPeer(ref)) { removedPeers.add(existingPeer);
peer.addPeer(ref);
}
}
for(OsmReference peer : removedPeers){
peer.getPeers().remove(existing);
System.out.println("Removing peer "+existing.getId() + " from " + peer.getId() );
update(peer);
}
dao.merge(fromOsmNodeParameter(existing,updated,false));
/**
* Wenn peers dazu gekommen sind, auch die rückwärtsverbindungen anlegen.
*/
for(OsmReference peer :existing.getPeers()) {
if(!peer.hasPeer(existing)) {
peer.addPeer(existing);
update(peer); update(peer);
} }
} }
return ref;
return existing;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment