Changeset 88:b406507a953d in AnnotationManagerN4J


Ignore:
Timestamp:
Feb 3, 2015, 6:01:27 PM (9 years ago)
Author:
casties
Branch:
default
Message:

upped version to 0.5.
can use display name and groups from auth token.

Files:
10 edited

Legend:

Unmodified
Added
Removed
  • pom.xml

    r86 r88  
    44  <groupId>de.mpiwg.itgroup.annotations</groupId>
    55  <artifactId>AnnotationManagerN4J</artifactId>
    6   <version>0.4-SNAPSHOT</version>
     6  <version>0.5-SNAPSHOT</version>
    77  <properties>
    88    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  • src/main/java/de/mpiwg/itgroup/annotations/Actor.java

    r70 r88  
    5858        if (isGroup() && store != null) {
    5959            // check if person in group
     60            if (person.groups != null) {
     61                // check person's groups
     62                if (person.groups.contains(this.id)) {
     63                    return true;
     64                }
     65            }
     66            // check in store
    6067            return store.isPersonInGroup(person, (Group) this);           
    6168        }
  • src/main/java/de/mpiwg/itgroup/annotations/Person.java

    r87 r88  
    1 /**
    2  *
    3  */
    41package de.mpiwg.itgroup.annotations;
    52
     
    2623 */
    2724
     25import java.util.Set;
     26
    2827import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
    2928
     
    3433public class Person extends Actor {
    3534
     35    public Set<String> groups;
     36   
    3637    public Person() {
    3738    }
     
    9293        return name;
    9394    }
     95   
     96    /**
     97     * Returns the anonymous Person.
     98     *
     99     * @return
     100     */
     101    public static Person getAnonymous() {
     102        return new Person("anonymous");
     103    }
    94104
    95105    /* (non-Javadoc)
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r75 r88  
    1 /**
    2  * Implements the "annotations" uri of the Annotator API. see
    3  * <https://github.com/okfn/annotator/wiki/Storage>
    4  */
    51package de.mpiwg.itgroup.annotations.restlet;
    62
     
    8884
    8985        // do authentication
    90         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     86        Person authUser = getUserFromAuthToken(entity);
    9187        logger.fine("request authenticated=" + authUser);
    9288
     
    173169
    174170        // do authentication TODO: who's allowed to create?
    175         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     171        Person authUser = getUserFromAuthToken(entity);
    176172        logger.fine("request authenticated=" + authUser);
    177173        if (authUser == null) {
     
    190186            // make sure id is not set for POST
    191187            jo.remove("id");
    192             // get Annotation object from posted JSON
     188            // create Annotation object from posted JSON
    193189            annot = createAnnotation(jo, entity);
    194190        } catch (IOException e1) {
     
    231227
    232228        // do authentication
    233         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     229        Person authUser = getUserFromAuthToken(entity);
    234230        logger.fine("request authenticated=" + authUser);
    235231
     
    294290
    295291        // do authentication
    296         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     292        Person authUser = getUserFromAuthToken(entity);
    297293        logger.fine("request authenticated=" + authUser);
    298294        AnnotationStore store = getAnnotationStore();
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java

    r75 r88  
    6060
    6161        // do authentication
    62         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     62        Person authUser = getUserFromAuthToken(entity);
    6363        logger.fine("request authenticated=" + authUser);
    6464
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java

    r75 r88  
    5959
    6060        // do authentication
    61         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     61        Person authUser = getUserFromAuthToken(entity);
    6262        logger.fine("request authenticated=" + authUser);
    6363
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java

    r75 r88  
    1 /**
    2  * ReST API for accessing groups in the Annotation store.
    3  */
    41package de.mpiwg.itgroup.annotations.restlet;
    52
     
    4037import de.mpiwg.itgroup.annotations.Actor;
    4138import de.mpiwg.itgroup.annotations.Group;
     39import de.mpiwg.itgroup.annotations.Person;
    4240import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    4341
     
    6765        logger.fine("AnnotatorGroups doGetJSON!");
    6866        setCorsHeaders();
    69         Form form = getRequest().getResourceRef().getQueryAsForm();
    70         String user = form.getFirstValue("user");
    71         String uri = form.getFirstValue("uri");
     67        // get user from auth token (preferred)
     68        Person authUser = getUserFromAuthToken(entity);
     69        JSONArray results = null;
     70        if (authUser != null && authUser.groups != null) {
     71            results  = getGroupsFromPerson(authUser);
     72        } else {
     73            // get user or uri from request
     74            Form form = getRequest().getResourceRef().getQueryAsForm();
     75            String user = form.getFirstValue("user");
     76            String uri = form.getFirstValue("uri");
     77            results = getGroupsFromStore(uri, user);
     78        }
     79        // assemble result object
     80        JSONObject result = new JSONObject();
     81        try {
     82            result.put("rows", results);
     83            result.put("total", results.length());
     84        } catch (JSONException e) {
     85            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
     86            return null;
     87        }
     88        logger.fine("sending:");
     89        logger.fine(result.toString());
     90        return new JsonRepresentation(result);
     91    }
     92
     93    public JSONArray getGroupsFromPerson(Person person) {
     94        JSONArray results = new JSONArray();
     95        for (String group : person.groups) {
     96            JSONObject jo = new JSONObject();
     97            try {
     98                jo.put("id", group);
     99                jo.put("name", group);
     100            } catch (JSONException e) {
     101            }
     102            results.put(jo);
     103        }
     104        return results;
     105    }
     106
     107    public JSONArray getGroupsFromStore(String uri, String user) {
     108        JSONArray results = new JSONArray();
    72109        if (uri == null || uri.isEmpty()) {
    73110            // get uri from user-id
    74111            uri = Actor.getUriFromId(user, false);
    75112        }
    76         JSONArray results = new JSONArray();
    77113        AnnotationStore store = getAnnotationStore();
    78114        Node person = store.getPersonNodeByUri(uri);
     
    90126            }
    91127        }
    92         // assemble result object
    93         JSONObject result = new JSONObject();
    94         try {
    95             result.put("rows", results);
    96             result.put("total", results.length());
    97         } catch (JSONException e) {
    98             setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
    99             return null;
    100         }
    101         logger.fine("sending:");
    102         logger.fine(result.toString());
    103         return new JsonRepresentation(result);
    104 
     128        return results;
    105129    }
    106130}
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r86 r88  
    1 /**
    2  * Base class for Annotator resource classes.
    3  */
    41package de.mpiwg.itgroup.annotations.restlet;
    52
     
    2724
    2825import java.io.UnsupportedEncodingException;
    29 import java.security.InvalidKeyException;
    30 import java.security.SignatureException;
    3126import java.text.SimpleDateFormat;
    3227import java.util.ArrayList;
     
    5752import org.restlet.util.Series;
    5853
     54import com.google.gson.JsonArray;
     55import com.google.gson.JsonElement;
     56import com.google.gson.JsonObject;
     57
    5958import de.mpiwg.itgroup.annotations.Actor;
    6059import de.mpiwg.itgroup.annotations.Annotation;
     
    155154     */
    156155    public boolean isAuthenticated(Representation entity) {
    157         return (checkAuthToken(entity) != null);
     156        return (getUserFromAuthToken(entity) != null);
    158157    }
    159158
     
    166165     * @return user-id
    167166     */
    168     public String checkAuthToken(Representation entity) {
     167    public Person getUserFromAuthToken(Representation entity) {
    169168        @SuppressWarnings("unchecked")
    170169        Series<Header> requestHeaders = (Series<Header>) getRequest().getAttributes().get("org.restlet.http.headers");
     
    172171        if (authToken == null) {
    173172            if (!((BaseRestlet) getApplication()).isAuthorizationMode()) {
    174                 return "anonymous";
     173                return Person.getAnonymous();
    175174            }
    176175            return null;
    177176        }
    178         // decode token first to get consumer key
    179         JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
    180         String userId = token.getParamAsPrimitive("userId").getAsString();
    181         String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString();
    182         // get stored consumer secret for key
    183         BaseRestlet restServer = (BaseRestlet) getApplication();
    184         String consumerSecret = restServer.getConsumerSecret(consumerKey);
    185         logger.fine("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
    186         if (consumerSecret == null) {
    187             return null;
    188         }
    189         // logger.fine(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
    190         try {
     177        Person user = null;
     178                try {
     179                        // decode token first to get consumer key
     180            JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
     181            String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString();
     182            // get stored consumer secret for key
     183            BaseRestlet restServer = (BaseRestlet) getApplication();
     184            String consumerSecret = restServer.getConsumerSecret(consumerKey);
     185            logger.fine("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
     186                        if (consumerSecret == null) {
     187                            logger.warning("Error: unknown consumer key: "+consumerKey);
     188                                return null;
     189                        }
     190                        // logger.fine(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
    191191            List<Verifier> verifiers = new ArrayList<Verifier>();
    192192            // we only do HS256 yet
     
    194194            // verify token signature(should really be static...)
    195195            new JsonTokenParser(new SystemClock(), null, (Checker[]) null).verify(token, verifiers);
    196         } catch (SignatureException e) {
    197             // TODO Auto-generated catch block
    198             e.printStackTrace();
    199         } catch (InvalidKeyException e) {
    200             // TODO Auto-generated catch block
    201             e.printStackTrace();
    202         } catch (UnsupportedEncodingException e) {
    203             // TODO Auto-generated catch block
    204             e.printStackTrace();
     196            // create Person
     197            JsonObject payload = token.getPayloadAsJsonObject();
     198            // userId is mandatory
     199            String userId = payload.get("userId").getAsString();
     200            user = new Person(userId);
     201            // displayName is optional
     202            if (payload.has("displayName")) {
     203                user.name = payload.get("displayName").getAsString();
     204            }
     205            // memberOf groups is optional
     206            if (payload.has("memberOf")) {
     207                Set<String> groups = new HashSet<String>();
     208                JsonArray jgroups = payload.get("memberOf").getAsJsonArray();
     209                for (JsonElement jgroup : jgroups) {
     210                    groups.add(jgroup.getAsString());
     211                }
     212                user.groups = groups;
     213            }
     214        } catch (Exception e) {
     215            logger.warning("Error checking auth token: "+e.toString());
     216            return null;
    205217        }
    206218        // must be ok then
    207         logger.fine("auth OK! user=" + userId);
    208         return userId;
     219        logger.fine("auth OK! user=" + user);
     220        return user;
    209221    }
    210222
     
    212224     * creates Annotator-JSON from an Annotation object.
    213225     *
    214      * @param annot
     226     * @param annot annotation object
    215227     * @param forAnonymous
    216      *            TODO
    217      * @return
     228     * @return Annotator-JSON
    218229     */
    219230    public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) {
     
    577588         * check authentication
    578589         */
    579         String authUser = checkAuthToken(entity);
     590        Person authUser = getUserFromAuthToken(entity);
    580591        if (authUser == null) {
    581592            /*
     
    620631        }
    621632        if (username == null) {
    622             username = authUser;
     633            username = authUser.getName();
    623634        }
    624635        // try to get full name
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java

    r79 r88  
    3535public class AnnotatorRestlet extends BaseRestlet {
    3636
    37     public final String version = "AnnotationManagerN4J/Annotator 0.4.0";
     37    public final String version = "AnnotationManagerN4J/Annotator 0.5.0";
    3838
    3939    /*
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java

    r75 r88  
    6868        setCorsHeaders();
    6969        // do authentication
    70         Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     70        Person authUser = getUserFromAuthToken(entity);
    7171        logger.fine("request authenticated=" + authUser);
    7272
Note: See TracChangeset for help on using the changeset viewer.