Skip to content

Commit

Permalink
#128 - Add GET/POST/DELETE /configs/apiKey
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed May 23, 2023
1 parent 90c1ec2 commit 5d176bf
Show file tree
Hide file tree
Showing 17 changed files with 1,088 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public interface DbKeys {
/** Config Partition Key Prefix. */
String PREFIX_CONFIG = "configs" + TAG_DELIMINATOR;

/** API Keys Partition Key Prefix. */
String PREFIX_API_KEYS = "apikeys" + TAG_DELIMINATOR;

/** API Key Partition Key Prefix. */
String PREFIX_API_KEY = "apikey" + TAG_DELIMINATOR;

/** Documents Partition Key Prefix. */
String PREFIX_DOCS = "docs" + TAG_DELIMINATOR;

Expand Down
1 change: 1 addition & 0 deletions dynamodb-documents/config/checkstyle/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<allow pkg="java.lang.reflect" />
<allow pkg="java.net" />
<allow pkg="java.nio.charset" />
<allow pkg="java.security" />
<allow pkg="java.text" />
<allow pkg="java.time" />
<allow pkg="java.util" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.dynamodb;

import java.util.List;
import com.formkiq.aws.dynamodb.DynamicObject;

/**
*
* API Key Service.
*
*/
public interface ApiKeysService {

/**
* Create API Key.
*
* @param siteId {@link String}
* @param name {@link String}
* @return {@link String}
*/
String createApiKey(String siteId, String name);

/**
* Delete Api Key.
*
* @param siteId {@link String}
* @param apiKey {@link String}
*/
void deleteApiKey(String siteId, String apiKey);

/**
* Is Api Key Valid.
*
* @param siteId {@link String}
* @param apiKey {@link String}
* @return boolean
*/
boolean isApiKeyValid(String siteId, String apiKey);

/**
* Get List of API Keys.
*
* @param siteId {@link String}
* @return {@link List} {@link String}
*/
List<DynamicObject> list(String siteId);

/**
* Mask Api Key.
*
* @param apiKey {@link String}
* @return {@link String}
*/
String mask(String apiKey);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.dynamodb;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.formkiq.aws.dynamodb.AttributeValueToDynamicObject;
import com.formkiq.aws.dynamodb.DbKeys;
import com.formkiq.aws.dynamodb.DynamicObject;
import com.formkiq.aws.dynamodb.DynamoDbConnectionBuilder;
import com.formkiq.aws.dynamodb.DynamoDbService;
import com.formkiq.aws.dynamodb.DynamoDbServiceImpl;
import com.formkiq.aws.dynamodb.QueryConfig;
import com.formkiq.aws.dynamodb.objects.DateUtil;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.QueryResponse;

/**
*
* DynamoDb implementation of {@link ApiKeysService}.
*
*/
public class ApiKeysServiceDynamoDb implements ApiKeysService, DbKeys {

/** Api Key Length. */
private static final int API_KEY_LENGTH = 51;
/** API Query Limit. */
private static final int LIMIT = 100;
/** Mask Value, must be even number. */
private static final int MASK = 8;

/**
* Generate Random String.
*
* @param len int
* @return {@link String}
*/
private static String generateRandomString(final int len) {
final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
int randomIndex = random.nextInt(chars.length());
sb.append(chars.charAt(randomIndex));
}

return sb.toString();
}

/** {@link DynamoDbService}. */
private DynamoDbService db;

/** {@link SimpleDateFormat} in ISO Standard format. */
private SimpleDateFormat df = DateUtil.getIsoDateFormatter();

/**
* constructor.
*
* @param connection {@link DynamoDbConnectionBuilder}
* @param documentsTable {@link String}
*/
public ApiKeysServiceDynamoDb(final DynamoDbConnectionBuilder connection,
final String documentsTable) {
if (documentsTable == null) {
throw new IllegalArgumentException("Table name is null");
}

this.db = new DynamoDbServiceImpl(connection, documentsTable);
}

@Override
public String createApiKey(final String siteId, final String name) {
String apiKey = generateRandomString(API_KEY_LENGTH);
Map<String, AttributeValue> keys = getKeys(siteId, apiKey);
keys.put("apiKey", AttributeValue.fromS(apiKey));
keys.put("name", AttributeValue.fromS(name));
keys.put("insertedDate", AttributeValue.fromS(this.df.format(new Date())));
this.db.putItem(keys);
return apiKey;
}

@Override
public void deleteApiKey(final String siteId, final String apiKey) {
Map<String, AttributeValue> keys = getKeys(siteId, apiKey);

QueryConfig config = new QueryConfig().projectionExpression("PK,SK");

String apiKeyStart = apiKey.substring(0, MASK);
String apiKeyEnd = apiKey.substring(apiKey.length() - MASK / 2);

QueryResponse response = this.db.queryBeginsWith(config, keys.get(PK),
AttributeValue.fromS(PREFIX_API_KEY + apiKeyStart), null, LIMIT);

response.items().forEach(i -> {
if (i.get(SK).s().endsWith(apiKeyEnd)) {
this.db.deleteItem(i.get(PK), i.get(SK));
}
});

}

private Map<String, AttributeValue> getKeys(final String siteId, final String apiKey) {
return keysGeneric(siteId, PREFIX_API_KEYS, PREFIX_API_KEY + apiKey);
}

@Override
public boolean isApiKeyValid(final String siteId, final String apiKey) {
Map<String, AttributeValue> keys = getKeys(siteId, apiKey);
return this.db.exists(keys.get(PK), keys.get(SK));
}

@Override
public List<DynamicObject> list(final String siteId) {
Map<String, AttributeValue> keys = getKeys(siteId, "");
QueryResponse response = this.db.query(keys.get(PK), null, LIMIT);

List<DynamicObject> list =
response.items().stream().map(new AttributeValueToDynamicObject()).map(o -> {
String apiKey = mask(o.getString("apiKey"));
o.put("apiKey", apiKey);
o.remove(PK);
o.remove(SK);
return o;
}).collect(Collectors.toList());

return list;
}

@Override
public String mask(final String apiKey) {
return apiKey.subSequence(0, MASK) + "****************"
+ apiKey.substring(apiKey.length() - MASK / 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.dynamodb;

import com.formkiq.aws.dynamodb.DynamoDbConnectionBuilder;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.module.lambdaservices.AwsServiceExtension;

/**
*
* {@link AwsServiceExtension} for {@link ApiKeysService}.
*
*/
public class ApiKeysServiceExtension implements AwsServiceExtension<ApiKeysService> {

/** {@link ApiKeysService}. */
private ApiKeysService service;

/**
* constructor.
*/
public ApiKeysServiceExtension() {}

@Override
public ApiKeysService loadService(final AwsServiceCache awsServiceCache) {
if (this.service == null) {
DynamoDbConnectionBuilder connection =
awsServiceCache.getExtension(DynamoDbConnectionBuilder.class);
this.service =
new ApiKeysServiceDynamoDb(connection, awsServiceCache.environment("DOCUMENTS_TABLE"));
}

return this.service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/** Implementation of the {@link ConfigService}. */
public class ConfigServiceImpl implements ConfigService, DbKeys {
public class ConfigServiceDynamoDb implements ConfigService, DbKeys {

/** {@link DynamoDbService}. */
private DynamoDbService db;
Expand All @@ -52,7 +52,7 @@ public class ConfigServiceImpl implements ConfigService, DbKeys {
* @param connection {@link DynamoDbConnectionBuilder}
* @param documentsTable {@link String}
*/
public ConfigServiceImpl(final DynamoDbConnectionBuilder connection,
public ConfigServiceDynamoDb(final DynamoDbConnectionBuilder connection,
final String documentsTable) {
if (documentsTable == null) {
throw new IllegalArgumentException("Table name is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ConfigService loadService(final AwsServiceCache awsServiceCache) {
DynamoDbConnectionBuilder connection =
awsServiceCache.getExtension(DynamoDbConnectionBuilder.class);
this.service =
new ConfigServiceImpl(connection, awsServiceCache.environment("DOCUMENTS_TABLE"));
new ConfigServiceDynamoDb(connection, awsServiceCache.environment("DOCUMENTS_TABLE"));
}

return this.service;
Expand Down
Loading

0 comments on commit 5d176bf

Please sign in to comment.