Skip to content

Commit

Permalink
fix: sqlite impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 19, 2024
1 parent 2577ee2 commit 74e55ee
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -3059,12 +3059,21 @@ public List<String> listClientsForApp(AppIdentifier appIdentifier) throws Storag
@Override
public void revoke(AppIdentifier appIdentifier, String targetType, String targetValue)
throws StorageQueryException {
throw new IllegalStateException("todo implement");
try {
OAuthQueries.revoke(this, appIdentifier, targetType, targetValue);
} catch (SQLException e) {
throw new StorageQueryException(e);
}

}

@Override
public boolean isRevoked(AppIdentifier appIdentifier, String[] targetTypes, String[] targetValues, long issuedAt)
throws StorageQueryException {
throw new IllegalStateException("todo implement");
try {
return OAuthQueries.isRevoked(this, appIdentifier, targetTypes, targetValues, issuedAt);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ public String getDashboardSessionsTable() {
public String getOAuthClientTable() {
return "oauth_clients";
}

public String getOAuthRevokeTable() {
return "oauth_revoke";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc
getInstance(main).addState(CREATING_NEW_TABLE, null);
update(start, OAuthQueries.getQueryToCreateOAuthClientTable(start), NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getOAuthRevokeTable())) {
getInstance(main).addState(CREATING_NEW_TABLE, null);
update(start, OAuthQueries.getQueryToCreateOAuthRevokeTable(start), NO_OP_SETTER);

// index
update(start, OAuthQueries.getQueryToCreateOAuthRevokeTimestampIndex(start), NO_OP_SETTER);
}
}


Expand Down
65 changes: 65 additions & 0 deletions src/main/java/io/supertokens/inmemorydb/queries/OAuthQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ public static String getQueryToCreateOAuthClientTable(Start start) {
// @formatter:on
}

public static String getQueryToCreateOAuthRevokeTable(Start start) {
String oAuth2ClientTable = Config.getConfig(start).getOAuthRevokeTable();
// @formatter:off
return "CREATE TABLE IF NOT EXISTS " + oAuth2ClientTable + " ("
+ "app_id VARCHAR(64) DEFAULT 'public',"
+ "target_type VARCHAR(16) NOT NULL,"
+ "target_value VARCHAR(128) NOT NULL,"
+ "timestamp BIGINT NOT NULL, "
+ "PRIMARY KEY (app_id, target_type, target_value),"
+ "FOREIGN KEY(app_id) "
+ " REFERENCES " + Config.getConfig(start).getAppsTable() + "(app_id) ON DELETE CASCADE"
+ ");";
// @formatter:on
}

public static String getQueryToCreateOAuthRevokeTimestampIndex(Start start) {
String oAuth2ClientTable = Config.getConfig(start).getOAuthRevokeTable();
return "CREATE INDEX IF NOT EXISTS oauth_revoke_timestamp_index ON "
+ oAuth2ClientTable + "(timestamp DESC, app_id DESC);";
}

public static boolean isClientIdForAppId(Start start, String clientId, AppIdentifier appIdentifier)
throws SQLException, StorageQueryException {
String QUERY = "SELECT app_id FROM " + Config.getConfig(start).getOAuthClientTable() +
Expand Down Expand Up @@ -89,4 +110,48 @@ public static boolean deleteClientIdForAppId(Start start, String clientId, AppId
return numberOfRow > 0;
}

public static void revoke(Start start, AppIdentifier appIdentifier, String targetType, String targetValue)
throws SQLException, StorageQueryException {
String INSERT = "INSERT INTO " + Config.getConfig(start).getOAuthRevokeTable()
+ "(app_id, target_type, target_value, timestamp) VALUES (?, ?, ?, ?) "
+ "ON CONFLICT (app_id, target_type, target_value) DO UPDATE SET timestamp = ?";

long currentTime = System.currentTimeMillis();
update(start, INSERT, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, targetType);
pst.setString(3, targetValue);
pst.setLong(4, currentTime);
pst.setLong(5, currentTime);
});
}

public static boolean isRevoked(Start start, AppIdentifier appIdentifier, String[] targetTypes, String[] targetValues, long issuedAt)
throws SQLException, StorageQueryException {
String QUERY = "SELECT app_id FROM " + Config.getConfig(start).getOAuthRevokeTable() +
" WHERE app_id = ? AND timestamp > ? AND (";

for (int i = 0; i < targetTypes.length; i++) {
QUERY += "(target_type = ? AND target_value = ?)";

if (i < targetTypes.length - 1) {
QUERY += " OR ";
}
}

QUERY += ")";

return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setLong(2, issuedAt);

int index = 3;
for (int i = 0; i < targetTypes.length; i++) {
pst.setString(index, targetTypes[i]);
index++;
pst.setString(index, targetValues[i]);
index++;
}
}, ResultSet::next);
}
}

0 comments on commit 74e55ee

Please sign in to comment.