Ignore:
Timestamp:
Jul 13, 2012, 6:41:02 PM (12 years ago)
Author:
casties
Branch:
default
Message:

permissions mostly work. need more server-side checking.

Location:
src/main/java/de/mpiwg/itgroup/annotations/restlet
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r4 r14  
    5151        // TODO: what to return without id - list of all annotations?
    5252
    53         // TODO: what to do with authentication?
    54         boolean authenticated = isAuthenticated(entity);
    55         logger.debug("request authenticated=" + authenticated);
    56 
    57         Annotation annots = getAnnotationStore().getAnnotationById(id);
    58         if (annots != null) {
    59             // there should be only one
    60             JSONObject result = createAnnotatorJson(annots);
     53        // do authentication
     54        String authUser = this.checkAuthToken(entity);
     55        logger.debug("request authenticated=" + authUser);
     56
     57        Annotation annot = getAnnotationStore().getAnnotationById(id);
     58        if (annot != null) {
     59            if (! annot.isActionAllowed("read", authUser)) {
     60                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
     61                return null;
     62            }
     63            JSONObject result = createAnnotatorJson(annot, (authUser == null));
    6164            logger.debug("sending:");
    6265            logger.debug(result);
    6366            return new JsonRepresentation(result);
    6467        } else {
    65             JSONArray results = new JSONArray();
    66             // annotator read request returns a list of annotation objects
    67             logger.debug("sending:");
    68             logger.debug(results);
    69             return new JsonRepresentation(results);
     68            // not found
     69            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
     70            return null;
    7071        }
    7172    }
     
    7374    /**
    7475     * POST with JSON content-type.
    75      *
    76      * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
    77      * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
    78      * auf eine Annotation falls extern
    7976     *
    8077     * @return
     
    8582        // set headers
    8683        setCorsHeaders();
     84       
     85        // do authentication TODO: who's allowed to create?
     86        String authUser = this.checkAuthToken(entity);
     87        logger.debug("request authenticated=" + authUser);
     88        if (authUser == null) {
     89            setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
     90            return null;
     91        }
     92
    8793        Annotation annot = null;
    8894        try {
     
    115121         * return 303: see other. For now we return the annotation.
    116122         */
    117         JSONObject jo = createAnnotatorJson(storedAnnot);
     123        JSONObject jo = createAnnotatorJson(storedAnnot, (authUser == null));
    118124        JsonRepresentation retRep = new JsonRepresentation(jo);
    119125        return retRep;
     
    135141        logger.debug("annotation-id=" + id);
    136142
    137         // TODO: what to do with authentication? we should check the owner
    138         boolean authenticated = isAuthenticated(entity);
    139         logger.debug("request authenticated=" + authenticated);
    140         if (!authenticated) {
    141             setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    142             return null;
    143         }
     143        // do authentication
     144        String authUser = this.checkAuthToken(entity);
     145        logger.debug("request authenticated=" + authUser);
    144146
    145147        Annotation annot = null;
     
    156158            if (storedAnnot == null) {
    157159                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
     160                return null;
     161            }
     162            if (! storedAnnot.isActionAllowed("update", authUser)) {
     163                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    158164                return null;
    159165            }
     
    170176             */
    171177            // return new annotation
    172             jo = createAnnotatorJson(storedAnnot);
     178            jo = createAnnotatorJson(storedAnnot, (authUser == null));
    173179            JsonRepresentation retRep = new JsonRepresentation(jo);
    174180            return retRep;
     
    198204        logger.debug("annotation-id=" + id);
    199205
    200         // TODO: what to do with authentication? we should check the owner
    201         boolean authenticated = isAuthenticated(entity);
    202         logger.debug("request authenticated=" + authenticated);
    203         if (!authenticated) {
    204             setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    205             return null;
    206         }
    207 
     206        // do authentication
     207        String authUser = this.checkAuthToken(entity);
     208        logger.debug("request authenticated=" + authUser);
     209        Annotation annot = getAnnotationStore().getAnnotationById(id);
     210        if (annot != null) {
     211            if (! annot.isActionAllowed("delete", authUser)) {
     212                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
     213                return null;
     214            }
     215        }
     216       
    208217        // delete annotation
    209218        getAnnotationStore().deleteById(id);
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r10 r14  
    141141        Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
    142142        String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
     143        if (authToken == null) return null;
    143144        // decode token first to get consumer key
    144145        JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
     
    178179     *
    179180     * @param annot
     181     * @param forAnonymous TODO
    180182     * @return
    181183     */
    182     public JSONObject createAnnotatorJson(Annotation annot) {
     184    public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) {
    183185        // return user as a JSON object (otherwise just as string)
    184186        boolean makeUserObject = true;
     
    233235            if (adminPerm != null) {
    234236                adminPerms.put(adminPerm.getIdString());
     237            } else if (forAnonymous) {
     238                // set something because its not allowed for anonymous
     239                adminPerms.put("not-you");
    235240            }
    236241            // delete
     
    240245            if (deletePerm != null) {
    241246                deletePerms.put(deletePerm.getIdString());
     247            } else if (forAnonymous) {
     248                // set something because its not allowed for anonymous
     249                deletePerms.put("not-you");
    242250            }
    243251            // update
     
    247255            if (updatePerm != null) {
    248256                updatePerms.put(updatePerm.getIdString());
     257            } else if (forAnonymous) {
     258                // set something because its not allowed for anonymous
     259                updatePerms.put("not-you");
    249260            }
    250261            // read
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java

    r4 r14  
    1616
    1717import de.mpiwg.itgroup.annotations.Annotation;
    18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    1918
    2019/**
     
    3231
    3332    /**
    34      * result for JSON content-type. optional search parameters: uri user limit
    35      * offset
     33     * result for JSON content-type. optional search parameters: uri, user, limit,
     34     * offset.
    3635     *
    3736     * @param entity
     
    4241        logger.debug("AnnotatorSearch doGetJSON!");
    4342        setCorsHeaders();
    44         // TODO: what to do with authentication?
    45         boolean authenticated = isAuthenticated(entity);
    46         logger.debug("request authenticated=" + authenticated);
     43        // do authentication
     44        String authUser = this.checkAuthToken(entity);
     45        logger.debug("request authenticated=" + authUser);
    4746
    4847        Form form = getRequest().getResourceRef().getQueryAsForm();
    4948        String uri = form.getFirstValue("uri");
    5049        String user = form.getFirstValue("user");
    51 
    5250        String limit = form.getFirstValue("limit");
    5351        String offset = form.getFirstValue("offset");
    5452
    55         AnnotationStore searcher = getAnnotationStore();
    56 
    57         JSONArray ja;
    58 
    59         List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
    60 
    61         ja = new JSONArray();
     53        JSONArray results = new JSONArray();
     54        // do search
     55        logger.debug(String.format("searching for uri=%s user=%s", uri, user));
     56        List<Annotation> annots = getAnnotationStore().searchByUriUser(uri, user, limit, offset);
    6257        for (Annotation annot : annots) {
    63             JSONObject jo = createAnnotatorJson(annot);
     58            // check permission
     59            if (!annot.isActionAllowed("read", authUser)) continue;
     60            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
    6461            if (jo != null) {
    65                 ja.put(createAnnotatorJson(annot));
     62                results.put(jo);
    6663            } else {
    6764                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
     
    6966            }
    7067        }
    71 
     68        // assemble result object
    7269        JSONObject result = new JSONObject();
    7370        try {
    74             result.put("rows", ja);
    75             result.put("total", ja.length());
     71            result.put("rows", results);
     72            result.put("total", results.length());
    7673        } catch (JSONException e) {
    77             e.printStackTrace();
    7874            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
    7975            return null;
Note: See TracChangeset for help on using the changeset viewer.