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

fixes query #144

Merged
merged 1 commit into from
Aug 5, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,44 @@ public static void createNewSession(Start start, TenantIdentifier tenantIdentifi
public static SessionInfo getSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
String sessionHandle)
throws SQLException, StorageQueryException {
// we do this as two separate queries and not one query with left join cause psql does not
// support left join with for update if the right table returns null.

String QUERY =
"SELECT sess.session_handle, sess.user_id, sess.refresh_token_hash_2, sess.session_data, sess" +
".expires_at, "
+
"sess.created_at_time, sess.jwt_user_payload, sess.use_static_key, users" +
".primary_or_recipe_user_id FROM " +
"SELECT session_handle, user_id, refresh_token_hash_2, session_data, " +
"expires_at, created_at_time, jwt_user_payload, use_static_key FROM " +
getConfig(start).getSessionInfoTable()
+ " AS sess LEFT JOIN " + getConfig(start).getUsersTable() +
" as users ON sess.app_id = users.app_id AND sess.user_id = users.user_id WHERE sess.app_id =" +
" ? AND " +
"sess.tenant_id = ? AND sess.session_handle = ? FOR UPDATE";
return execute(con, QUERY, pst -> {
+ " WHERE app_id = ? AND tenant_id = ? AND session_handle = ? FOR UPDATE";
SessionInfo sessionInfo = execute(con, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, tenantIdentifier.getTenantId());
pst.setString(3, sessionHandle);
}, result -> {
if (result.next()) {
return SessionInfoRowMapper.getInstance().mapOrThrow(result);
return SessionInfoRowMapper.getInstance().mapOrThrow(result, false);
}
return null;
});

if (sessionInfo == null) {
return null;
}

QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable()
+ " WHERE app_id = ? AND user_id = ?";

return execute(con, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, sessionInfo.recipeUserId);
}, result -> {
if (result.next()) {
String primaryUserId = result.getString("primary_or_recipe_user_id");
if (primaryUserId != null) {
sessionInfo.userId = primaryUserId;
}
}
return sessionInfo;
});
}

public static void updateSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
Expand Down Expand Up @@ -348,7 +365,7 @@ public static SessionInfo getSession(Start start, TenantIdentifier tenantIdentif
pst.setString(3, sessionHandle);
}, result -> {
if (result.next()) {
return SessionInfoRowMapper.getInstance().mapOrThrow(result);
return SessionInfoRowMapper.getInstance().mapOrThrow(result, true);
}
return null;
});
Expand Down Expand Up @@ -400,7 +417,7 @@ public static void removeAccessTokenSigningKeysBefore(Start start, AppIdentifier
});
}

static class SessionInfoRowMapper implements RowMapper<SessionInfo, ResultSet> {
static class SessionInfoRowMapper {
public static final SessionInfoRowMapper INSTANCE = new SessionInfoRowMapper();

private SessionInfoRowMapper() {
Expand All @@ -410,19 +427,23 @@ private static SessionInfoRowMapper getInstance() {
return INSTANCE;
}

@Override
public SessionInfo map(ResultSet result) throws Exception {
public SessionInfo mapOrThrow(ResultSet result, boolean hasPrimaryOrRecipeUserId) throws StorageQueryException {
JsonParser jp = new JsonParser();
// if result.getString("primary_or_recipe_user_id") is null, it will be handled by SessionInfo
// constructor
return new SessionInfo(result.getString("session_handle"),
result.getString("primary_or_recipe_user_id"),
result.getString("user_id"),
result.getString("refresh_token_hash_2"),
jp.parse(result.getString("session_data")).getAsJsonObject(),
result.getLong("expires_at"),
jp.parse(result.getString("jwt_user_payload")).getAsJsonObject(),
result.getLong("created_at_time"), result.getBoolean("use_static_key"));
try {
return new SessionInfo(result.getString("session_handle"),
hasPrimaryOrRecipeUserId ? result.getString("primary_or_recipe_user_id") :
result.getString("user_id"),
result.getString("user_id"),
result.getString("refresh_token_hash_2"),
jp.parse(result.getString("session_data")).getAsJsonObject(),
result.getLong("expires_at"),
jp.parse(result.getString("jwt_user_payload")).getAsJsonObject(),
result.getLong("created_at_time"), result.getBoolean("use_static_key"));
} catch (Exception e) {
throw new StorageQueryException(e);
}
}
}

Expand Down
Loading