Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1718 add an implementation for AccessTokenRepository
- Loading branch information
Showing
13 changed files
with
426 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...uice/cassandra-guice/src/main/java/org/apache/james/modules/data/CassandraJmapModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/**************************************************************** | ||
* 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.modules.data; | ||
|
||
import org.apache.james.backends.cassandra.components.CassandraModule; | ||
import org.apache.james.jmap.api.access.AccessTokenRepository; | ||
import org.apache.james.jmap.cassandra.access.CassandraAccessModule; | ||
import org.apache.james.jmap.cassandra.access.CassandraAccessTokenRepository; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.multibindings.Multibinder; | ||
|
||
public class CassandraJmapModule extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(AccessTokenRepository.class).to(CassandraAccessTokenRepository.class); | ||
|
||
Multibinder<CassandraModule> cassandraDataDefinitions = Multibinder.newSetBinder(binder(), CassandraModule.class); | ||
cassandraDataDefinitions.addBinding().to(CassandraAccessModule.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/**************************************************************** | ||
* 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.cassandra.access; | ||
|
||
import static com.datastax.driver.core.DataType.text; | ||
import static com.datastax.driver.core.DataType.uuid; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.james.backends.cassandra.components.CassandraIndex; | ||
import org.apache.james.backends.cassandra.components.CassandraModule; | ||
import org.apache.james.backends.cassandra.components.CassandraTable; | ||
import org.apache.james.backends.cassandra.components.CassandraType; | ||
import org.apache.james.jmap.cassandra.access.table.CassandraAccessTokenTable; | ||
|
||
import com.datastax.driver.core.schemabuilder.SchemaBuilder; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public class CassandraAccessModule implements CassandraModule { | ||
|
||
private final List<CassandraTable> tables; | ||
private final List<CassandraIndex> index; | ||
private final List<CassandraType> types; | ||
|
||
public CassandraAccessModule() { | ||
tables = ImmutableList.of( | ||
new CassandraTable(CassandraAccessTokenTable.TABLE_NAME, | ||
SchemaBuilder.createTable(CassandraAccessTokenTable.TABLE_NAME) | ||
.ifNotExists() | ||
.addPartitionKey(CassandraAccessTokenTable.TOKEN, uuid()) | ||
.addColumn(CassandraAccessTokenTable.USERNAME, text()))); | ||
index = ImmutableList.of(); | ||
types = ImmutableList.of(); | ||
} | ||
|
||
@Override | ||
public List<CassandraTable> moduleTables() { | ||
return tables; | ||
} | ||
|
||
@Override | ||
public List<CassandraIndex> moduleIndex() { | ||
return index; | ||
} | ||
|
||
@Override | ||
public List<CassandraType> moduleTypes() { | ||
return types; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
.../src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/**************************************************************** | ||
* 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.cassandra.access; | ||
|
||
import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.delete; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.ttl; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import org.apache.james.backends.cassandra.utils.CassandraConstants; | ||
import org.apache.james.jmap.api.access.AccessToken; | ||
import org.apache.james.jmap.api.access.AccessTokenRepository; | ||
import org.apache.james.jmap.api.access.exceptions.AccessTokenAlreadyStored; | ||
import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken; | ||
import org.apache.james.jmap.cassandra.access.table.CassandraAccessTokenTable; | ||
|
||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.Session; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class CassandraAccessTokenRepository implements AccessTokenRepository { | ||
|
||
private static final String TTL = "ttl"; | ||
|
||
private final Session session; | ||
private final PreparedStatement removeStatement; | ||
private final PreparedStatement insertStatement; | ||
private final PreparedStatement selectStatement; | ||
private final int durationInSeconds; | ||
|
||
@Inject | ||
public CassandraAccessTokenRepository(Session session, @Named(TOKEN_EXPIRATION_IN_MS) long durationInMilliseconds) { | ||
this.session = session; | ||
this.durationInSeconds = (int) (durationInMilliseconds / 1000); | ||
|
||
this.removeStatement = this.session.prepare(delete() | ||
.from(CassandraAccessTokenTable.TABLE_NAME) | ||
.where(eq(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN)))); | ||
|
||
this.insertStatement = this.session.prepare(insertInto(CassandraAccessTokenTable.TABLE_NAME) | ||
.ifNotExists() | ||
.value(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN)) | ||
.value(CassandraAccessTokenTable.USERNAME, bindMarker(CassandraAccessTokenTable.USERNAME)) | ||
.using(ttl(bindMarker(TTL)))); | ||
|
||
this.selectStatement = this.session.prepare(select() | ||
.from(CassandraAccessTokenTable.TABLE_NAME) | ||
.where(eq(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN)))); | ||
} | ||
|
||
@Override | ||
public void addToken(String username, AccessToken accessToken) throws AccessTokenAlreadyStored { | ||
Preconditions.checkNotNull(username); | ||
Preconditions.checkArgument(! username.isEmpty(), "Username should not be empty"); | ||
Preconditions.checkNotNull(accessToken); | ||
|
||
boolean applied = session.execute(insertStatement.bind() | ||
.setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken()) | ||
.setString(CassandraAccessTokenTable.USERNAME, username) | ||
.setInt(TTL, durationInSeconds)) | ||
.one() | ||
.getBool(CassandraConstants.LIGHTWEIGHT_TRANSACTION_APPLIED); | ||
|
||
if (!applied) { | ||
throw new AccessTokenAlreadyStored(accessToken); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeToken(AccessToken accessToken) { | ||
Preconditions.checkNotNull(accessToken); | ||
|
||
session.execute(removeStatement.bind() | ||
.setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken())); | ||
} | ||
|
||
@Override | ||
public String getUsernameFromToken(AccessToken accessToken) throws InvalidAccessToken { | ||
Preconditions.checkNotNull(accessToken); | ||
|
||
return Optional.ofNullable( | ||
session.execute( | ||
selectStatement.bind() | ||
.setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken())) | ||
.one()) | ||
.map(row -> row.getString(CassandraAccessTokenTable.USERNAME)) | ||
.orElseThrow(() -> new InvalidAccessToken(accessToken)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...src/main/java/org/apache/james/jmap/cassandra/access/table/CassandraAccessTokenTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/**************************************************************** | ||
* 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.cassandra.access.table; | ||
|
||
public interface CassandraAccessTokenTable { | ||
|
||
String TABLE_NAME = "access_token"; | ||
|
||
String TOKEN = "access_token"; | ||
String USERNAME = "username"; | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
.../test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/**************************************************************** | ||
* 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.cassandra.access; | ||
|
||
import org.apache.james.backends.cassandra.CassandraCluster; | ||
import org.apache.james.jmap.api.access.AbstractAccessTokenRepositoryTest; | ||
import org.apache.james.jmap.api.access.AccessTokenRepository; | ||
import org.junit.After; | ||
|
||
public class CassandraAccessTokenRepositoryTest extends AbstractAccessTokenRepositoryTest { | ||
|
||
private CassandraCluster cassandra; | ||
|
||
@Override | ||
protected AccessTokenRepository createAccessTokenRepository() { | ||
cassandra = CassandraCluster.create(new CassandraAccessModule()); | ||
return new CassandraAccessTokenRepository(cassandra.getConf(), TTL_IN_MS); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
cassandra.clearAllTables(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.