james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1719305 - in /james/project/trunk/server/protocols/jmap: ./ src/main/java/org/apache/james/jmap/ src/main/java/org/apache/james/jmap/model/ src/test/java/org/apache/james/jmap/
Date Fri, 11 Dec 2015 10:05:27 GMT
Author: matthieu
Date: Fri Dec 11 10:05:27 2015
New Revision: 1719305

URL: http://svn.apache.org/viewvc?rev=1719305&view=rev
Log:
JAMES-1644 JMAP Authentication first step: get continuation token

Added:
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenRequest.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenResponse.java
Modified:
    james/project/trunk/server/protocols/jmap/pom.xml
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/AuthenticationServlet.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java

Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719305&r1=1719304&r2=1719305&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 10:05:27 2015
@@ -174,6 +174,11 @@
                     <artifactId>james-server-util</artifactId>
                 </dependency>
                 <dependency>
+                    <groupId>com.fasterxml.jackson.core</groupId>
+                    <artifactId>jackson-databind</artifactId>
+                    <version>2.3.3</version>
+                </dependency>
+                <dependency>
                     <groupId>com.jayway.restassured</groupId>
                     <artifactId>rest-assured</artifactId>
                     <version>2.5.0</version>

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/AuthenticationServlet.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/AuthenticationServlet.java?rev=1719305&r1=1719304&r2=1719305&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/AuthenticationServlet.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/AuthenticationServlet.java
Fri Dec 11 10:05:27 2015
@@ -25,11 +25,22 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.james.jmap.model.ContinuationTokenRequest;
+import org.apache.james.jmap.model.ContinuationTokenResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 public class AuthenticationServlet extends HttpServlet {
 
     public static final String JSON_CONTENT_TYPE = "application/json";
     public static final String JSON_CONTENT_TYPE_UTF8 = "application/json; charset=UTF-8";
 
+    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationServlet.class);
+
+    private ObjectMapper mapper = new ObjectMapper();
+
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
         if (!checkJsonContentType(req)) {
@@ -40,6 +51,27 @@ public class AuthenticationServlet exten
             resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
             return;
         }
+
+        ContinuationTokenRequest continuationTokenRequest;
+        try {
+            continuationTokenRequest = mapper.readValue(req.getReader(), ContinuationTokenRequest.class);
+        } catch (Exception e) {
+            LOG.warn("Invalid authentication request received.", e);
+            resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+            return;
+        }
+
+        resp.setContentType(JSON_CONTENT_TYPE_UTF8);
+
+        ContinuationTokenResponse continuationTokenResponse = ContinuationTokenResponse
+                .builder()
+                // TODO Answer a real token
+                .continuationToken("token")
+                .methods(ContinuationTokenResponse.AuthenticationMethod.PASSWORD)
+                .build();
+
+        mapper.writeValue(resp.getOutputStream(), continuationTokenResponse);
+
     }
 
     private boolean checkJsonContentType(HttpServletRequest req) {

Added: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenRequest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenRequest.java?rev=1719305&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenRequest.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenRequest.java
Fri Dec 11 10:05:27 2015
@@ -0,0 +1,93 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap.model;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+
+@JsonDeserialize(builder=ContinuationTokenRequest.Builder.class)
+public class ContinuationTokenRequest {
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    @JsonPOJOBuilder(withPrefix="")
+    public static class Builder {
+
+        private String username;
+        private String clientName;
+        private String clientVersion;
+        private String deviceName;
+
+        private Builder() {}
+
+        public Builder username(String username) {
+            this.username = username;
+            return this;
+        }
+
+        public Builder clientName(String clientName) {
+            this.clientName = clientName;
+            return this;
+        }
+
+        public Builder clientVersion(String clientVersion) {
+            this.clientVersion = clientVersion;
+            return this;
+        }
+
+        public Builder deviceName(String deviceName) {
+            this.deviceName = deviceName;
+            return this;
+        }
+
+        public ContinuationTokenRequest build() {
+            return new ContinuationTokenRequest(username, clientName, clientVersion, deviceName);
+        }
+    }
+
+    private final String username;
+    private final String clientName;
+    private final String clientVersion;
+    private final String deviceName;
+
+    private ContinuationTokenRequest(String username, String clientName, String clientVersion,
String deviceName) {
+        this.username = username;
+        this.clientName = clientName;
+        this.clientVersion = clientVersion;
+        this.deviceName = deviceName;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public String getClientName() {
+        return clientName;
+    }
+
+    public String getClientVersion() {
+        return clientVersion;
+    }
+
+    public String getDeviceName() {
+        return deviceName;
+    }
+}

Added: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenResponse.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenResponse.java?rev=1719305&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenResponse.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ContinuationTokenResponse.java
Fri Dec 11 10:05:27 2015
@@ -0,0 +1,102 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap.model;
+
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.collect.ImmutableList;
+
+public class ContinuationTokenResponse {
+
+    public enum AuthenticationMethod {
+        PASSWORD("password"),
+        EXTERNAL("external"),
+        PROMPT("prompt");
+
+        private String value;
+
+        AuthenticationMethod(String value) {
+            this.value = value;
+        }
+
+        @JsonValue
+        public String toString() {
+            return value;
+        }
+    };
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder {
+        private String continuationToken;
+        private ImmutableList<AuthenticationMethod> methods;
+        private String prompt;
+
+        private Builder() {}
+
+        public Builder continuationToken(String continuationToken) {
+            this.continuationToken = continuationToken;
+            return this;
+        }
+
+        public Builder methods(List<AuthenticationMethod> methods) {
+            this.methods = ImmutableList.copyOf(methods);
+            return this;
+        }
+
+        public Builder methods(AuthenticationMethod... methods) {
+            this.methods = ImmutableList.copyOf(methods);
+            return this;
+        }
+
+        public Builder prompt(String prompt) {
+            this.prompt = prompt;
+            return this;
+        }
+
+        public ContinuationTokenResponse build() {
+            return new ContinuationTokenResponse(continuationToken, methods, prompt);
+        }
+    }
+
+    private final String continuationToken;
+    private final ImmutableList<AuthenticationMethod> methods;
+    private final String prompt;
+
+    private ContinuationTokenResponse(String continuationToken, ImmutableList<AuthenticationMethod>
methods, String prompt) {
+        this.continuationToken = continuationToken;
+        this.methods = methods;
+        this.prompt = prompt;
+    }
+
+    public String getContinuationToken() {
+        return continuationToken;
+    }
+
+    public List<AuthenticationMethod> getMethods() {
+        return methods;
+    }
+
+    public String getPrompt() {
+        return prompt;
+    }
+}
\ No newline at end of file

Modified: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java?rev=1719305&r1=1719304&r2=1719305&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPAuthenticationTest.java
Fri Dec 11 10:05:27 2015
@@ -21,6 +21,8 @@ package org.apache.james.jmap;
 import static com.jayway.restassured.RestAssured.given;
 import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
 import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.isA;
 
 import org.apache.james.http.jetty.Configuration;
 import org.apache.james.http.jetty.JettyHttpServer;
@@ -119,4 +121,43 @@ public class JMAPAuthenticationTest {
             .statusCode(400);
     }
 
+    @Test
+    public void mustReturnMalformedRequestWhenBodyIsEmpty() {
+        given()
+            .contentType(ContentType.JSON)
+            .accept(ContentType.JSON)
+        .when()
+            .post("/authentication")
+        .then()
+            .statusCode(400);
+    }
+
+    @Test
+    public void mustReturnJsonResponse() {
+        given()
+            .contentType(ContentType.JSON)
+            .accept(ContentType.JSON)
+            .body("{\"username\": \"user@domain.tld\", \"clientName\": \"Mozilla Thunderbird\",
\"clientVersion\": \"42.0\", \"deviceName\": \"Joe Blogg’s iPhone\"}")
+        .when()
+            .post("/authentication")
+        .then()
+            .statusCode(200)
+            .contentType(ContentType.JSON);
+    }
+
+    @Test
+    public void getContinuationTokenWhenValidResquest() {
+        given()
+            .contentType(ContentType.JSON)
+            .accept(ContentType.JSON)
+            .body("{\"username\": \"user@domain.tld\", \"clientName\": \"Mozilla Thunderbird\",
\"clientVersion\": \"42.0\", \"deviceName\": \"Joe Blogg’s iPhone\"}")
+        .when()
+            .post("/authentication")
+        .then()
+            .statusCode(200)
+            .body("continuationToken", isA(String.class))
+            .body("methods", hasItem("password"));
+    }
+
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message