Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add createdat to totp device #870

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public void addInfoToNonAuthRecipesBasedOnUserId(TenantIdentifier tenantIdentifi
}
} else if (className.equals(TOTPStorage.class.getName())) {
try {
TOTPDevice device = new TOTPDevice(userId, "testDevice", "secret", 0, 30, false);
TOTPDevice device = new TOTPDevice(userId, "testDevice", "secret", 0, 30, false, System.currentTimeMillis());
this.startTransaction(con -> {
try {
long now = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ public TOTPDevice map(ResultSet result) throws SQLException {
result.getString("secret_key"),
result.getInt("period"),
result.getInt("skew"),
result.getBoolean("verified"));
result.getBoolean("verified"),
result.getLong("created_at"));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/supertokens/totp/Totp.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static TOTPDevice registerDevice(AppIdentifierWithStorage appIdentifierWi
FeatureNotEnabledException, TenantOrAppNotFoundException, StorageTransactionLogicException {

String secret = generateSecret();
TOTPDevice device = new TOTPDevice(userId, deviceName, secret, period, skew, false);
TOTPDevice device = new TOTPDevice(userId, deviceName, secret, period, skew, false, System.currentTimeMillis());
TOTPSQLStorage totpStorage = appIdentifierWithStorage.getTOTPStorage();

if (deviceName != null) {
Expand All @@ -137,7 +137,8 @@ public static TOTPDevice registerDevice(AppIdentifierWithStorage appIdentifierWi
device.secretKey,
device.period,
device.skew,
device.verified
device.verified,
device.createdAt
));
} catch (DeviceAlreadyExistsException e){
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
appIdentifierWithStorage = getAppIdentifierWithStorage(req);
}

Totp.createDevice(super.main, appIdentifierWithStorage, new TOTPDevice(userId, deviceName, secretKey, period, skew, true));
Totp.createDevice(super.main, appIdentifierWithStorage, new TOTPDevice(userId, deviceName, secretKey, period, skew, true, System.currentTimeMillis()));

result.addProperty("status", "OK");
super.sendJsonResponse(200, result, resp);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/supertokens/test/StorageLayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void totpCodeLengthTest() throws Exception {

Start start = (Start) StorageLayer.getStorage(process.getProcess());

TOTPDevice d1 = new TOTPDevice("user", "d1", "secret", 30, 1, false);
TOTPDevice d1 = new TOTPDevice("user", "d1", "secret", 30, 1, false, System.currentTimeMillis());
storage.createDevice(new AppIdentifier(null, null), d1);

// Try code with length > 8
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/io/supertokens/test/totp/TOTPStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public void createDeviceTests() throws Exception {
}
TOTPSQLStorage storage = result.storage;

TOTPDevice device1 = new TOTPDevice("user", "d1", "secret", 30, 1, false);
TOTPDevice device2 = new TOTPDevice("user", "d2", "secret", 30, 1, true);
TOTPDevice device2Duplicate = new TOTPDevice("user", "d2", "new-secret", 30, 1, false);
TOTPDevice device1 = new TOTPDevice("user", "d1", "secret", 30, 1, false, System.currentTimeMillis());
TOTPDevice device2 = new TOTPDevice("user", "d2", "secret", 30, 1, true, System.currentTimeMillis());
TOTPDevice device2Duplicate = new TOTPDevice("user", "d2", "new-secret", 30, 1, false, System.currentTimeMillis());

storage.createDevice(new AppIdentifier(null, null), device1);

Expand Down Expand Up @@ -155,7 +155,7 @@ public void verifyDeviceTests() throws Exception {
}
TOTPSQLStorage storage = result.storage;

TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false);
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false, System.currentTimeMillis());
storage.createDevice(new AppIdentifier(null, null), device);

TOTPDevice[] storedDevices = storage.getDevices(new AppIdentifier(null, null), "user");
Expand Down Expand Up @@ -195,8 +195,8 @@ public void getDevicesCount_TransactionTests() throws Exception {
});
assert devicesCount == 0;

TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false);
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false);
TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false, System.currentTimeMillis());
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false, System.currentTimeMillis());

storage.createDevice(new AppIdentifier(null, null), device1);
storage.createDevice(new AppIdentifier(null, null), device2);
Expand Down Expand Up @@ -225,8 +225,8 @@ public void removeUser_TransactionTests() throws Exception {
return null;
});

TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false);
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false);
TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false, System.currentTimeMillis());
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false, System.currentTimeMillis());

storage.createDevice(new AppIdentifier(null, null), device1);
storage.createDevice(new AppIdentifier(null, null), device2);
Expand Down Expand Up @@ -266,8 +266,8 @@ public void deleteDevice_TransactionTests() throws Exception {
}
TOTPSQLStorage storage = result.storage;

TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false);
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false);
TOTPDevice device1 = new TOTPDevice("user", "device1", "sk1", 30, 1, false, System.currentTimeMillis());
TOTPDevice device2 = new TOTPDevice("user", "device2", "sk2", 30, 1, false, System.currentTimeMillis());

storage.createDevice(new AppIdentifier(null, null), device1);
storage.createDevice(new AppIdentifier(null, null), device2);
Expand Down Expand Up @@ -314,7 +314,7 @@ public void updateDeviceNameTests() throws Exception {
}
TOTPSQLStorage storage = result.storage;

TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false);
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false, System.currentTimeMillis());
storage.createDevice(new AppIdentifier(null, null), device);

TOTPDevice[] storedDevices = storage.getDevices(new AppIdentifier(null, null), "user");
Expand All @@ -335,7 +335,7 @@ public void updateDeviceNameTests() throws Exception {

// Try to create a new device and rename it to the same name as an existing
// device:
TOTPDevice newDevice = new TOTPDevice("user", "new-device", "secretKey", 30, 1, false);
TOTPDevice newDevice = new TOTPDevice("user", "new-device", "secretKey", 30, 1, false, System.currentTimeMillis());
storage.createDevice(new AppIdentifier(null, null), newDevice);

assertThrows(DeviceAlreadyExistsException.class,
Expand All @@ -354,8 +354,8 @@ public void getDevicesTest() throws Exception {
}
TOTPSQLStorage storage = result.storage;

TOTPDevice device1 = new TOTPDevice("user", "d1", "secretKey", 30, 1, false);
TOTPDevice device2 = new TOTPDevice("user", "d2", "secretKey", 30, 1, false);
TOTPDevice device1 = new TOTPDevice("user", "d1", "secretKey", 30, 1, false, System.currentTimeMillis());
TOTPDevice device2 = new TOTPDevice("user", "d2", "secretKey", 30, 1, false, System.currentTimeMillis());

storage.createDevice(new AppIdentifier(null, null), device1);
storage.createDevice(new AppIdentifier(null, null), device2);
Expand All @@ -382,7 +382,7 @@ public void insertUsedCodeTest() throws Exception {

// Insert a long lasting valid code and check that it's returned when queried:
{
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false);
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false, System.currentTimeMillis());
TOTPUsedCode code = new TOTPUsedCode("user", "1234", true, nextDay, now);

storage.createDevice(new AppIdentifier(null, null), device);
Expand Down Expand Up @@ -413,7 +413,7 @@ public void insertUsedCodeTest() throws Exception {

// Try to insert code after user has atleast one device (i.e. TOTP enabled)
{
TOTPDevice newDevice = new TOTPDevice("user", "new-device", "secretKey", 30, 1, false);
TOTPDevice newDevice = new TOTPDevice("user", "new-device", "secretKey", 30, 1, false, System.currentTimeMillis());
storage.createDevice(new AppIdentifier(null, null), newDevice);
insertUsedCodesUtil(
storage,
Expand Down Expand Up @@ -447,7 +447,7 @@ public void getAllUsedCodesTest() throws Exception {
long nextDay = now + 1000 * 60 * 60 * 24; // 1 day from now
long prevDay = now - 1000 * 60 * 60 * 24; // 1 day ago

TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false);
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false, System.currentTimeMillis());
TOTPUsedCode validCode1 = new TOTPUsedCode("user", "valid1", true, nextDay, now + 1);
TOTPUsedCode invalidCode = new TOTPUsedCode("user", "invalid", false, nextDay, now + 2);
TOTPUsedCode expiredCode = new TOTPUsedCode("user", "expired", true, prevDay, now + 3);
Expand Down Expand Up @@ -493,7 +493,7 @@ public void removeExpiredCodesTest() throws Exception {
long nextDay = System.currentTimeMillis() + 1000 * 60 * 60 * 24; // 1 day from now
long hundredMs = System.currentTimeMillis() + 100; // 100ms from now

TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false);
TOTPDevice device = new TOTPDevice("user", "device", "secretKey", 30, 1, false, System.currentTimeMillis());
TOTPUsedCode validCodeToLive = new TOTPUsedCode("user", "valid", true, nextDay, now);
TOTPUsedCode invalidCodeToLive = new TOTPUsedCode("user", "invalid", false, nextDay, now + 1);
TOTPUsedCode validCodeToExpire = new TOTPUsedCode("user", "valid", true, hundredMs, now + 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ public void testApi() throws Exception {
attempt2Secret,
30,
0,
false
);
false,
System.currentTimeMillis()
);
JsonObject verifyDeviceBody = new JsonObject();
verifyDeviceBody.addProperty("userId", device.userId);
verifyDeviceBody.addProperty("deviceName", device.deviceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void testApi() throws Exception {
}

// Verify totp on the imported device
TOTPDevice device = new TOTPDevice("user-id", "d1", secret, 30, 0, false);
TOTPDevice device = new TOTPDevice("user-id", "d1", secret, 30, 0, false, System.currentTimeMillis());

JsonObject verifyDeviceReq = new JsonObject();
verifyDeviceReq.addProperty("userId", device.userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void testSameCodeUsedOnDifferentTenantsIsAllowed() throws Exception {
for (TenantIdentifier tenant1 : tenants) {
JsonObject deviceResponse = createDevice(tenant1, "user" + userCount);
String secretKey = deviceResponse.get("secret").getAsString();
TOTPDevice device = new TOTPDevice("user" + userCount, "d1", secretKey, 2, 1, true);
TOTPDevice device = new TOTPDevice("user" + userCount, "d1", secretKey, 2, 1, true, System.currentTimeMillis());
String validTotp = TOTPRecipeTest.generateTotpCode(process.getProcess(), device);
verifyDevice(tenant1, "user" + userCount, validTotp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testExternalUserIdTranslation() throws Exception {
"totp");
assert res1.get("status").getAsString().equals("OK");
String d1Secret = res1.get("secret").getAsString();
TOTPDevice device1 = new TOTPDevice(externalUserId, "deviceName", d1Secret, 30, 1, false);
TOTPDevice device1 = new TOTPDevice(externalUserId, "deviceName", d1Secret, 30, 1, false, System.currentTimeMillis());

body.addProperty("deviceName", "d2");

Expand All @@ -93,7 +93,7 @@ public void testExternalUserIdTranslation() throws Exception {
"totp");
assert res2.get("status").getAsString().equals("OK");
String d2Secret = res2.get("secret").getAsString();
TOTPDevice device2 = new TOTPDevice(externalUserId, "d2", d2Secret, 30, 1, false);
TOTPDevice device2 = new TOTPDevice(externalUserId, "deviceName", d2Secret, 30, 1, false, System.currentTimeMillis());

// Verify d1 but not d2:
JsonObject verifyD1Input = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testApi() throws Exception {
assertEquals(createDeviceRes.get("status").getAsString(), "OK");
String secretKey = createDeviceRes.get("secret").getAsString();

TOTPDevice device = new TOTPDevice("user-id", "deviceName", secretKey, 2, 0, false);
TOTPDevice device = new TOTPDevice("user-id", "deviceName", secretKey, 2, 0, false, System.currentTimeMillis());

JsonObject verifyDeviceReq = new JsonObject();
verifyDeviceReq.addProperty("userId", device.userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testApi() throws Exception {
assertEquals(createDeviceRes.get("status").getAsString(), "OK");
String secretKey = createDeviceRes.get("secret").getAsString();

TOTPDevice device = new TOTPDevice("user-id", "deviceName", secretKey, 30, 0, false);
TOTPDevice device = new TOTPDevice("user-id", "deviceName", secretKey, 30, 0, false, System.currentTimeMillis());

// Start the actual tests for update device API:

Expand Down