chibenwa commented on a change in pull request #781:
URL: https://github.com/apache/james-project/pull/781#discussion_r765507735
##########
File path:
server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/draft/DraftMethodsModule.java
##########
@@ -104,12 +116,27 @@ protected void configure() {
@Provides
@Named(InjectionKeys.DRAFT)
Authenticator provideAuthenticator(MetricFactory metricFactory,
- AccessTokenAuthenticationStrategy
accessTokenAuthenticationStrategy,
- JWTAuthenticationStrategy
jwtAuthenticationStrategy,
-
QueryParameterAccessTokenAuthenticationStrategy
queryParameterAuthenticationStrategy) {
- return Authenticator.of(metricFactory,
- jwtAuthenticationStrategy,
- accessTokenAuthenticationStrategy,
- queryParameterAuthenticationStrategy);
+
@Named("draftJmapAuthenticationStrategies") Set<AuthenticationStrategy>
strategies) {
+ return Authenticator.of(metricFactory, strategies);
+ }
+
+ @Provides
+ @Singleton
+ @Named("draftJmapAuthenticationStrategies")
+ public Set<AuthenticationStrategy>
provideAuthenticationStrategies(GuiceGenericLoader loader,
+
JMAPDraftConfiguration configuration) {
+ PackageName implicitFQDNPrefix =
PackageName.of("org.apache.james.jmap.http");
+
+ List<String> defaultStrategies = ImmutableList.of(
+ AccessTokenAuthenticationStrategy.class.getSimpleName(),
+ JWTAuthenticationStrategy.class.getSimpleName(),
+
QueryParameterAccessTokenAuthenticationStrategy.class.getSimpleName());
+
+ return configuration.getAuthenticationStrategies()
+ .orElse(defaultStrategies)
+ .stream()
+ .map(ClassName::new)
+
.map(Throwing.function(loader.<AuthenticationStrategy>withNamingSheme(new
NamingScheme.OptionalPackagePrefix(implicitFQDNPrefix))::instantiate))
Review comment:
```suggestion
new NamingScheme.OptionalPackagePrefix(implicitFQDNPrefix)
```
Constant?
##########
File path:
server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/draft/DraftMethodsModule.java
##########
@@ -104,12 +116,27 @@ protected void configure() {
@Provides
@Named(InjectionKeys.DRAFT)
Authenticator provideAuthenticator(MetricFactory metricFactory,
- AccessTokenAuthenticationStrategy
accessTokenAuthenticationStrategy,
- JWTAuthenticationStrategy
jwtAuthenticationStrategy,
-
QueryParameterAccessTokenAuthenticationStrategy
queryParameterAuthenticationStrategy) {
- return Authenticator.of(metricFactory,
- jwtAuthenticationStrategy,
- accessTokenAuthenticationStrategy,
- queryParameterAuthenticationStrategy);
+
@Named("draftJmapAuthenticationStrategies") Set<AuthenticationStrategy>
strategies) {
+ return Authenticator.of(metricFactory, strategies);
+ }
+
+ @Provides
+ @Singleton
+ @Named("draftJmapAuthenticationStrategies")
+ public Set<AuthenticationStrategy>
provideAuthenticationStrategies(GuiceGenericLoader loader,
+
JMAPDraftConfiguration configuration) {
+ PackageName implicitFQDNPrefix =
PackageName.of("org.apache.james.jmap.http");
Review comment:
Use a constant?
##########
File path:
server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryJmapTestRule.java
##########
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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.rfc8621.memory;
+
+import java.io.IOException;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.modules.TestJMAPServerModule;
+import org.apache.james.server.core.configuration.Configuration;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
Review comment:
JUNIT 4 spotted.
Please rewrite RFC-8621 tests at least in JUNIT 5
##########
File path:
server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/ModularizeJmapDraftAuthenticationStrategyTest.java
##########
@@ -0,0 +1,260 @@
+/****************************************************************
+ * 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;
+
+import static io.restassured.RestAssured.given;
+import static
org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JMAPTestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.JMAPTestingConstants.NAME;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+
+public abstract class ModularizeJmapDraftAuthenticationStrategyTest {
+ public static String DOMAIN = "domain.tld";
+ public static Username BOB = Username.of("bob" + "@" + DOMAIN);
+ public static String BOB_PASSWORD = "123456";
+
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+ private AccessToken bobAccessToken;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
Review comment:
```suggestion
protected GuiceJamesServer createJmapServer(Optional<Class<? extends
AuthenticationStrategy>> authOverride) throws Exception {
```
And we should be good IMO
##########
File path:
server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/ModularizeJmapDraftAuthenticationStrategyTest.java
##########
@@ -0,0 +1,260 @@
+/****************************************************************
+ * 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;
+
+import static io.restassured.RestAssured.given;
+import static
org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JMAPTestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.JMAPTestingConstants.NAME;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+
+public abstract class ModularizeJmapDraftAuthenticationStrategyTest {
+ public static String DOMAIN = "domain.tld";
+ public static Username BOB = Username.of("bob" + "@" + DOMAIN);
+ public static String BOB_PASSWORD = "123456";
+
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+ private AccessToken bobAccessToken;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
+
+ protected abstract MailboxManager initMailboxManager();
+
+ @Before
+ public void setup() {
+ MailboxManager mailboxManager = initMailboxManager();
+ allowAuthenticationStrategy = new
AllowAuthenticationStrategy(mailboxManager);
+ denyAuthenticationStrategy = new DenyAuthenticationStrategy();
Review comment:
Please rely on guice to instanciate the AUth strategies, otherwize you
do not exercise the guice loading logic...
##########
File path:
server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryModularizeJmapRFC8621AuthenticationStrategyContract.java
##########
@@ -0,0 +1,65 @@
+/****************************************************************
+ * 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.rfc8621.memory;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.jmap.core.JmapRfc8621Configuration;
+import
org.apache.james.jmap.rfc8621.contract.custom.authentication.strategy.AllowAuthenticationStrategy;
+import
org.apache.james.jmap.rfc8621.contract.custom.authentication.strategy.DenyAuthenticationStrategy;
+import
org.apache.james.jmap.rfc8621.contract.custom.authentication.strategy.ModularizeJmapRFC8621AuthenticationStrategyContract;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
+import org.junit.Rule;
Review comment:
Get rid of this evil JUNIT 4 stuff and embrace JUNIT 5!
(Before @Arsnael gets mad at you)
##########
File path:
server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/custom/authentication/strategy/ModularizeJmapRFC8621AuthenticationStrategyContract.java
##########
@@ -0,0 +1,256 @@
+/****************************************************************
+ * 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.rfc8621.contract.custom.authentication.strategy;
+
+import static io.restassured.RestAssured.given;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.apache.http.HttpStatus.SC_OK;
+import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JMAPUrls.JMAP;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.ACCEPT_RFC8621_VERSION_HEADER;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.AUTHORIZATION_HEADER;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.BOB;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.BOB_BASIC_AUTH_HEADER;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.BOB_PASSWORD;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.DOMAIN;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.ECHO_REQUEST_OBJECT;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.ECHO_RESPONSE_OBJECT;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.INVALID_JWT_TOKEN;
+import static
org.apache.james.jmap.rfc8621.contract.Fixture.UNKNOWN_USER_TOKEN;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.USER_TOKEN;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.getHeadersWith;
+import static org.apache.james.jmap.rfc8621.contract.Fixture.toBase64;
+import static org.hamcrest.Matchers.equalTo;
+import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.ACCEPT;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+import io.restassured.http.Header;
+
+public abstract class ModularizeJmapRFC8621AuthenticationStrategyContract {
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
Review comment:
Idem
```
protected abstract GuiceJamesServer createJmapServer(Optional<Class<?
extends AuthenticationStrategy>> authOverride) throws IOException;
```
##########
File path:
server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/ModularizeJmapDraftAuthenticationStrategyTest.java
##########
@@ -0,0 +1,260 @@
+/****************************************************************
+ * 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;
+
+import static io.restassured.RestAssured.given;
+import static
org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JMAPTestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.JMAPTestingConstants.NAME;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+
+public abstract class ModularizeJmapDraftAuthenticationStrategyTest {
+ public static String DOMAIN = "domain.tld";
+ public static Username BOB = Username.of("bob" + "@" + DOMAIN);
+ public static String BOB_PASSWORD = "123456";
+
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+ private AccessToken bobAccessToken;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
+
+ protected abstract MailboxManager initMailboxManager();
Review comment:
Kill this method that have nothing to do in an integration test.
##########
File path:
server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/ModularizeJmapDraftAuthenticationStrategyTest.java
##########
@@ -0,0 +1,260 @@
+/****************************************************************
+ * 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;
+
+import static io.restassured.RestAssured.given;
+import static
org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JMAPTestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.JMAPTestingConstants.NAME;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+
+public abstract class ModularizeJmapDraftAuthenticationStrategyTest {
+ public static String DOMAIN = "domain.tld";
+ public static Username BOB = Username.of("bob" + "@" + DOMAIN);
+ public static String BOB_PASSWORD = "123456";
+
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+ private AccessToken bobAccessToken;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
+
+ protected abstract MailboxManager initMailboxManager();
+
+ @Before
+ public void setup() {
+ MailboxManager mailboxManager = initMailboxManager();
+ allowAuthenticationStrategy = new
AllowAuthenticationStrategy(mailboxManager);
+ denyAuthenticationStrategy = new DenyAuthenticationStrategy();
+ }
+
+ public void
setupJamesServerWithCustomAuthenticationStrategy(Optional<AuthenticationStrategy>
authenticationStrategy) throws Throwable {
+ jmapServer = authenticationStrategy.map(strategy -> {
+ if (strategy instanceof AllowAuthenticationStrategy) {
+ try {
+ return
createJmapServerWithAllowAuthenticationStrategy((AllowAuthenticationStrategy)
strategy);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else if (strategy instanceof DenyAuthenticationStrategy) {
+ try {
+ return
createJmapServerWithDenyAuthenticationStrategy((DenyAuthenticationStrategy)
strategy);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }).orElse(createJmapServerWithDefaultAuthenticationStrategies());
+
+ jmapServer.start();
+
+ RestAssured.requestSpecification = jmapRequestSpecBuilder
+
.setPort(jmapServer.getProbe(JmapGuiceProbe.class).getJmapPort().getValue())
+ .build();
+
+ jmapServer.getProbe(DataProbeImpl.class)
+ .fluent()
+ .addDomain(DOMAIN)
+ .addUser(BOB.asString(), BOB_PASSWORD);
+ bobAccessToken = authenticateJamesUser(baseUri(jmapServer), BOB,
BOB_PASSWORD);
+ }
+
+ @After
+ public void teardown() {
+ jmapServer.stop();
+ }
+
+ @Test
+ public void
getAuthenticationRouteWithAllowAuthenticationStrategyShouldSucceed() throws
Throwable {
+
setupJamesServerWithCustomAuthenticationStrategy(Optional.of(allowAuthenticationStrategy));
+
+ given()
+ .when()
+ .get("/authentication")
+ .then()
+ .statusCode(200)
+ .body("api", equalTo("/jmap"))
+ .body("eventSource", both(isA(String.class)).and(notNullValue()))
+ .body("upload", equalTo("/upload"))
+ .body("download", equalTo("/download"));
+ }
+
+ @Test
+ public void
getFilterWithAllowAuthenticationStrategyShouldNotRequiredAnyAuthentication()
throws Throwable {
+
setupJamesServerWithCustomAuthenticationStrategy(Optional.of(allowAuthenticationStrategy));
+
+ String body = "[[" +
+ " \"getFilter\", " +
+ " {}, " +
+ "\"#0\"" +
+ "]]";
+
+ given()
+ .body(body)
+ .when()
+ .post("/jmap")
+ .then()
+ .statusCode(200)
+ .body(NAME, CoreMatchers.equalTo("filter"))
+ .body(ARGUMENTS + ".singleton", hasSize(0));
+ }
+
+ @Test
+ public void
getAuthenticationRouteWithDenyAuthenticationStrategyShouldReturnUnauthorizedCode()
throws Throwable {
+
setupJamesServerWithCustomAuthenticationStrategy(Optional.of(denyAuthenticationStrategy));
+
+ given()
+ .when()
+ .get("/authentication")
+ .then()
+ .statusCode(401);
+ }
+
+ @Test
+ public void
getAuthenticationRouteWhenDefaultAuthenticationStrategiesWithNonAuthenticationShouldReturnUnauthorizedCode()
throws Throwable {
+ setupJamesServerWithCustomAuthenticationStrategy(Optional.empty());
+
+ given()
+ .when()
+ .get("/authentication")
+ .then()
+ .statusCode(401);
+ }
+
+ @Test
+ public void
getAuthenticationRouteWhenDefaultAuthenticationStrategiesWithValidAccessTokenShouldSucceed()
throws Throwable {
+ setupJamesServerWithCustomAuthenticationStrategy(Optional.empty());
+
+ given()
+ .header("Authorization", bobAccessToken.asString())
+ .when()
+ .get("/authentication")
+ .then()
+ .statusCode(200)
+ .body("api", equalTo("/jmap"))
+ .body("eventSource", both(isA(String.class)).and(notNullValue()))
+ .body("upload", equalTo("/upload"))
+ .body("download", equalTo("/download"));
+ }
+
+ @Test
+ public void
getFilterWhenDenyAuthenticationStrategyWithNonAuthenticationShouldReturnUnauthorizedCode()
throws Throwable {
+
setupJamesServerWithCustomAuthenticationStrategy(Optional.of(denyAuthenticationStrategy));
+
+ String body = "[[" +
+ " \"getFilter\", " +
+ " {}, " +
+ "\"#0\"" +
+ "]]";
+
+ given()
+ .body(body)
+ .when()
+ .post("/jmap")
+ .then()
+ .statusCode(401);
+ }
+
+ @Test
+ public void
getFilterWhenDenyAuthenticationStrategyWithValidAccessTokenShouldReturnUnauthorizedCode()
throws Throwable {
+
setupJamesServerWithCustomAuthenticationStrategy(Optional.of(denyAuthenticationStrategy));
+
+ String body = "[[" +
+ " \"getFilter\", " +
+ " {}, " +
+ "\"#0\"" +
+ "]]";
+
+ given()
+ .header("Authorization", bobAccessToken.asString())
+ .body(body)
+ .when()
+ .post("/jmap")
+ .then()
+ .statusCode(401);
+ }
+
+ @Test
+ public void
getFilterWhenDefaultAuthenticationStrategiesWithValidAccessTokenShouldSucceed()
throws Throwable {
+ setupJamesServerWithCustomAuthenticationStrategy(Optional.empty());
+
+ String body = "[[" +
+ " \"getFilter\", " +
+ " {}, " +
+ "\"#0\"" +
+ "]]";
Review comment:
```suggestion
String body = "[[\"getFilter\", {}, \"#0\"]]";
```
Would be both acceptable and more readable to me.
##########
File path:
server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/draft/DraftMethodsModule.java
##########
@@ -104,12 +116,27 @@ protected void configure() {
@Provides
@Named(InjectionKeys.DRAFT)
Authenticator provideAuthenticator(MetricFactory metricFactory,
- AccessTokenAuthenticationStrategy
accessTokenAuthenticationStrategy,
- JWTAuthenticationStrategy
jwtAuthenticationStrategy,
-
QueryParameterAccessTokenAuthenticationStrategy
queryParameterAuthenticationStrategy) {
- return Authenticator.of(metricFactory,
- jwtAuthenticationStrategy,
- accessTokenAuthenticationStrategy,
- queryParameterAuthenticationStrategy);
+
@Named("draftJmapAuthenticationStrategies") Set<AuthenticationStrategy>
strategies) {
+ return Authenticator.of(metricFactory, strategies);
+ }
+
+ @Provides
+ @Singleton
+ @Named("draftJmapAuthenticationStrategies")
+ public Set<AuthenticationStrategy>
provideAuthenticationStrategies(GuiceGenericLoader loader,
+
JMAPDraftConfiguration configuration) {
+ PackageName implicitFQDNPrefix =
PackageName.of("org.apache.james.jmap.http");
+
+ List<String> defaultStrategies = ImmutableList.of(
+ AccessTokenAuthenticationStrategy.class.getSimpleName(),
+ JWTAuthenticationStrategy.class.getSimpleName(),
+
QueryParameterAccessTokenAuthenticationStrategy.class.getSimpleName());
Review comment:
Constant?
##########
File path:
server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/ModularizeJmapDraftAuthenticationStrategyTest.java
##########
@@ -0,0 +1,260 @@
+/****************************************************************
+ * 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;
+
+import static io.restassured.RestAssured.given;
+import static
org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JMAPTestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.JMAPTestingConstants.NAME;
+import static
org.apache.james.jmap.JMAPTestingConstants.jmapRequestSpecBuilder;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.draft.JmapGuiceProbe;
+import org.apache.james.jmap.http.AuthenticationStrategy;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.utils.DataProbeImpl;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.restassured.RestAssured;
+
+public abstract class ModularizeJmapDraftAuthenticationStrategyTest {
+ public static String DOMAIN = "domain.tld";
+ public static Username BOB = Username.of("bob" + "@" + DOMAIN);
+ public static String BOB_PASSWORD = "123456";
+
+ private AllowAuthenticationStrategy allowAuthenticationStrategy;
+ private DenyAuthenticationStrategy denyAuthenticationStrategy;
+ private GuiceJamesServer jmapServer;
+ private AccessToken bobAccessToken;
+
+ protected abstract GuiceJamesServer
createJmapServerWithAllowAuthenticationStrategy(AllowAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDenyAuthenticationStrategy(DenyAuthenticationStrategy
authenticationStrategy) throws IOException;
+
+ protected abstract GuiceJamesServer
createJmapServerWithDefaultAuthenticationStrategies() throws IOException;
+
+ protected abstract MailboxManager initMailboxManager();
+
+ @Before
+ public void setup() {
+ MailboxManager mailboxManager = initMailboxManager();
+ allowAuthenticationStrategy = new
AllowAuthenticationStrategy(mailboxManager);
+ denyAuthenticationStrategy = new DenyAuthenticationStrategy();
+ }
+
+ public void
setupJamesServerWithCustomAuthenticationStrategy(Optional<AuthenticationStrategy>
authenticationStrategy) throws Throwable {
+ jmapServer = authenticationStrategy.map(strategy -> {
+ if (strategy instanceof AllowAuthenticationStrategy) {
+ try {
+ return
createJmapServerWithAllowAuthenticationStrategy((AllowAuthenticationStrategy)
strategy);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else if (strategy instanceof DenyAuthenticationStrategy) {
+ try {
+ return
createJmapServerWithDenyAuthenticationStrategy((DenyAuthenticationStrategy)
strategy);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }).orElse(createJmapServerWithDefaultAuthenticationStrategies());
Review comment:
We should be able to just pass the list of authentication strategies we
want for Draft and RFC-8621.
From there, we could craft the JMAPDraft configuration POJO and the
JMAPRFC8621 configuration POJO with those strategies and inject these
strategies using a guice module override.
We would be closer to reality!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]