Skip to content

Commit

Permalink
Allow customization of table name in JdbcOAuth2AuthorizedClientService
Browse files Browse the repository at this point in the history
Closes spring-projectsgh-14044

Signed-off-by: Craig Andrews <[email protected]>
  • Loading branch information
candrews committed Oct 20, 2023
1 parent 975ac10 commit 28a9858
Showing 1 changed file with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ public class JdbcOAuth2AuthorizedClientService implements OAuth2AuthorizedClient
+ "refresh_token_issued_at";
// @formatter:on

private static final String TABLE_NAME = "oauth2_authorized_client";
private static final String DEFAULT_TABLE_NAME = "oauth2_authorized_client";

private static final String PK_FILTER = "client_registration_id = ? AND principal_name = ?";

// @formatter:off
private static final String LOAD_AUTHORIZED_CLIENT_SQL = "SELECT " + COLUMN_NAMES
+ " FROM " + TABLE_NAME
+ " FROM %TABLE_NAME%"
+ " WHERE " + PK_FILTER;
// @formatter:on

// @formatter:off
private static final String SAVE_AUTHORIZED_CLIENT_SQL = "INSERT INTO " + TABLE_NAME
private static final String SAVE_AUTHORIZED_CLIENT_SQL = "INSERT INTO %TABLE_NAME%"
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
// @formatter:on

private static final String REMOVE_AUTHORIZED_CLIENT_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
private static final String REMOVE_AUTHORIZED_CLIENT_SQL = "DELETE FROM %TABLE_NAME% WHERE " + PK_FILTER;

// @formatter:off
private static final String UPDATE_AUTHORIZED_CLIENT_SQL = "UPDATE " + TABLE_NAME
private static final String UPDATE_AUTHORIZED_CLIENT_SQL = "UPDATE %TABLE_NAME%"
+ " SET access_token_type = ?, access_token_value = ?, access_token_issued_at = ?,"
+ " access_token_expires_at = ?, access_token_scopes = ?,"
+ " refresh_token_value = ?, refresh_token_issued_at = ?"
Expand All @@ -114,6 +114,19 @@ public class JdbcOAuth2AuthorizedClientService implements OAuth2AuthorizedClient

protected final LobHandler lobHandler;

/**
* The name of database table used to store OAuth2 authorized clients.
*/
private String tableName = DEFAULT_TABLE_NAME;

private String loadAuthorizedClientSql;

private String saveAuthorizedClientSql;

private String removeAuthorizedClientSql;

private String updateAuthorizedClientSql;

/**
* Constructs a {@code JdbcOAuth2AuthorizedClientService} using the provided
* parameters.
Expand Down Expand Up @@ -145,6 +158,7 @@ public JdbcOAuth2AuthorizedClientService(JdbcOperations jdbcOperations,
authorizedClientRowMapper.setLobHandler(lobHandler);
this.authorizedClientRowMapper = authorizedClientRowMapper;
this.authorizedClientParametersMapper = new OAuth2AuthorizedClientParametersMapper();
prepareQueries();
}

@Override
Expand All @@ -157,7 +171,7 @@ public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRe
new SqlParameterValue(Types.VARCHAR, clientRegistrationId),
new SqlParameterValue(Types.VARCHAR, principalName) };
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters);
List<OAuth2AuthorizedClient> result = this.jdbcOperations.query(LOAD_AUTHORIZED_CLIENT_SQL, pss,
List<OAuth2AuthorizedClient> result = this.jdbcOperations.query(this.loadAuthorizedClientSql, pss,
this.authorizedClientRowMapper);
return !result.isEmpty() ? (T) result.get(0) : null;
}
Expand Down Expand Up @@ -191,7 +205,7 @@ private void updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Aut
try (LobCreator lobCreator = this.lobHandler.getLobCreator()) {
PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter(lobCreator,
parameters.toArray());
this.jdbcOperations.update(UPDATE_AUTHORIZED_CLIENT_SQL, pss);
this.jdbcOperations.update(this.updateAuthorizedClientSql, pss);
}
}

Expand All @@ -201,7 +215,7 @@ private void insertAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Aut
try (LobCreator lobCreator = this.lobHandler.getLobCreator()) {
PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter(lobCreator,
parameters.toArray());
this.jdbcOperations.update(SAVE_AUTHORIZED_CLIENT_SQL, pss);
this.jdbcOperations.update(this.saveAuthorizedClientSql, pss);
}
}

Expand All @@ -213,7 +227,7 @@ public void removeAuthorizedClient(String clientRegistrationId, String principal
new SqlParameterValue(Types.VARCHAR, clientRegistrationId),
new SqlParameterValue(Types.VARCHAR, principalName) };
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters);
this.jdbcOperations.update(REMOVE_AUTHORIZED_CLIENT_SQL, pss);
this.jdbcOperations.update(this.removeAuthorizedClientSql, pss);
}

/**
Expand Down Expand Up @@ -241,6 +255,27 @@ public final void setAuthorizedClientParametersMapper(
this.authorizedClientParametersMapper = authorizedClientParametersMapper;
}

/**
* Set the name of database table used to store OAuth2 authorized clients.
* @param tableName the database table name
*/
public final void setTableName(String tableName) {
Assert.hasText(tableName, "Table name must not be empty");
this.tableName = tableName.trim();
prepareQueries();
}

private String getQuery(String base) {
return StringUtils.replace(base, "%TABLE_NAME%", this.tableName);
}

private void prepareQueries() {
this.loadAuthorizedClientSql = getQuery(LOAD_AUTHORIZED_CLIENT_SQL);
this.saveAuthorizedClientSql = getQuery(SAVE_AUTHORIZED_CLIENT_SQL);
this.removeAuthorizedClientSql = getQuery(REMOVE_AUTHORIZED_CLIENT_SQL);
this.updateAuthorizedClientSql = getQuery(UPDATE_AUTHORIZED_CLIENT_SQL);
}

/**
* The default {@link RowMapper} that maps the current row in
* {@code java.sql.ResultSet} to {@link OAuth2AuthorizedClient}.
Expand Down

0 comments on commit 28a9858

Please sign in to comment.