Skip to content

Commit ad108f5

Browse files
committed
Polish JdbcOAuth2AuthorizationConsentService
Issue gh-314
1 parent 9787794 commit ad108f5

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationConsentService.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
/**
4242
* A JDBC implementation of an {@link OAuth2AuthorizationConsentService} that uses a
43-
* <p>
4443
* {@link JdbcOperations} for {@link OAuth2AuthorizationConsent} persistence.
4544
*
4645
* <p>
@@ -50,11 +49,11 @@
5049
* therefore MUST be defined in the database schema.
5150
*
5251
* @author Ovidiu Popa
52+
* @since 0.1.2
5353
* @see OAuth2AuthorizationConsentService
5454
* @see OAuth2AuthorizationConsent
5555
* @see JdbcOperations
5656
* @see RowMapper
57-
* @since 0.1.2
5857
*/
5958
public class JdbcOAuth2AuthorizationConsentService implements OAuth2AuthorizationConsentService {
6059

@@ -109,10 +108,8 @@ public JdbcOAuth2AuthorizationConsentService(JdbcOperations jdbcOperations,
109108
@Override
110109
public void save(OAuth2AuthorizationConsent authorizationConsent) {
111110
Assert.notNull(authorizationConsent, "authorizationConsent cannot be null");
112-
113-
OAuth2AuthorizationConsent existingAuthorizationConsent =
114-
findById(authorizationConsent.getRegisteredClientId(), authorizationConsent.getPrincipalName());
115-
111+
OAuth2AuthorizationConsent existingAuthorizationConsent = findById(
112+
authorizationConsent.getRegisteredClientId(), authorizationConsent.getPrincipalName());
116113
if (existingAuthorizationConsent == null) {
117114
insertAuthorizationConsent(authorizationConsent);
118115
} else {
@@ -205,7 +202,6 @@ protected final Function<OAuth2AuthorizationConsent, List<SqlParameterValue>> ge
205202
* {@code ResultSet} to {@link OAuth2AuthorizationConsent}.
206203
*/
207204
public static class OAuth2AuthorizationConsentRowMapper implements RowMapper<OAuth2AuthorizationConsent> {
208-
209205
private final RegisteredClientRepository registeredClientRepository;
210206

211207
public OAuth2AuthorizationConsentRowMapper(RegisteredClientRepository registeredClientRepository) {
@@ -216,9 +212,7 @@ public OAuth2AuthorizationConsentRowMapper(RegisteredClientRepository registered
216212
@Override
217213
public OAuth2AuthorizationConsent mapRow(ResultSet rs, int rowNum) throws SQLException {
218214
String registeredClientId = rs.getString("registered_client_id");
219-
220-
RegisteredClient registeredClient = this.registeredClientRepository
221-
.findById(registeredClientId);
215+
RegisteredClient registeredClient = this.registeredClientRepository.findById(registeredClientId);
222216
if (registeredClient == null) {
223217
throw new DataRetrievalFailureException(
224218
"The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository.");

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationConsentServiceTests.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,38 @@
5656
* @author Ovidiu Popa
5757
*/
5858
public class JdbcOAuth2AuthorizationConsentServiceTests {
59-
6059
private static final String OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql";
6160
private static final String CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-consent-schema.sql";
6261
private static final String PRINCIPAL_NAME = "principal-name";
6362
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
6463

6564
private static final OAuth2AuthorizationConsent AUTHORIZATION_CONSENT =
6665
OAuth2AuthorizationConsent.withId(REGISTERED_CLIENT.getId(), PRINCIPAL_NAME)
67-
.authority(new SimpleGrantedAuthority("some.authority"))
66+
.authority(new SimpleGrantedAuthority("SCOPE_scope1"))
67+
.authority(new SimpleGrantedAuthority("SCOPE_scope2"))
68+
.authority(new SimpleGrantedAuthority("SCOPE_scope3"))
69+
.authority(new SimpleGrantedAuthority("authority-a"))
70+
.authority(new SimpleGrantedAuthority("authority-b"))
6871
.build();
6972

7073
private EmbeddedDatabase db;
7174
private JdbcOperations jdbcOperations;
7275
private RegisteredClientRepository registeredClientRepository;
7376
private JdbcOAuth2AuthorizationConsentService authorizationConsentService;
7477

78+
@Before
79+
public void setUp() {
80+
this.db = createDb();
81+
this.jdbcOperations = new JdbcTemplate(this.db);
82+
this.registeredClientRepository = mock(RegisteredClientRepository.class);
83+
this.authorizationConsentService = new JdbcOAuth2AuthorizationConsentService(this.jdbcOperations, this.registeredClientRepository);
84+
}
85+
86+
@After
87+
public void tearDown() {
88+
this.db.shutdown();
89+
}
90+
7591
@Test
7692
public void constructorWhenJdbcOperationsIsNullThenThrowIllegalArgumentException() {
7793
// @formatter:off
@@ -127,7 +143,7 @@ public void saveWhenAuthorizationConsentNewThenSaved() {
127143
RegisteredClient newRegisteredClient = TestRegisteredClients.registeredClient()
128144
.id("new-client").build();
129145

130-
when(registeredClientRepository.findById(eq(newRegisteredClient.getId())))
146+
when(this.registeredClientRepository.findById(eq(newRegisteredClient.getId())))
131147
.thenReturn(newRegisteredClient);
132148

133149
this.authorizationConsentService.save(expectedAuthorizationConsent);
@@ -143,7 +159,7 @@ public void saveWhenAuthorizationConsentExistsThenUpdated() {
143159
OAuth2AuthorizationConsent.from(AUTHORIZATION_CONSENT)
144160
.authority(new SimpleGrantedAuthority("new.authority"))
145161
.build();
146-
when(registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
162+
when(this.registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
147163
.thenReturn(REGISTERED_CLIENT);
148164

149165
this.authorizationConsentService.save(expectedAuthorizationConsent);
@@ -157,7 +173,7 @@ public void saveWhenAuthorizationConsentExistsThenUpdated() {
157173

158174
@Test
159175
public void saveLoadAuthorizationConsentWhenCustomStrategiesSetThenCalled() throws Exception {
160-
when(registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
176+
when(this.registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
161177
.thenReturn(REGISTERED_CLIENT);
162178

163179
JdbcOAuth2AuthorizationConsentService.OAuth2AuthorizationConsentRowMapper authorizationConsentRowMapper = spy(
@@ -205,6 +221,17 @@ public void findByIdWhenPrincipalNameNullThenThrowIllegalArgumentException() {
205221
.withMessage("principalName cannot be empty");
206222
}
207223

224+
@Test
225+
public void findByIdWhenAuthorizationConsentExistsThenFound() {
226+
when(this.registeredClientRepository.findById(eq(REGISTERED_CLIENT.getId())))
227+
.thenReturn(REGISTERED_CLIENT);
228+
229+
this.authorizationConsentService.save(AUTHORIZATION_CONSENT);
230+
OAuth2AuthorizationConsent authorizationConsent = this.authorizationConsentService.findById(
231+
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
232+
assertThat(authorizationConsent).isNotNull();
233+
}
234+
208235
@Test
209236
public void findByIdWhenAuthorizationConsentDoesNotExistThenNull() {
210237
this.authorizationConsentService.save(AUTHORIZATION_CONSENT);
@@ -221,27 +248,16 @@ public void tableDefinitionWhenCustomThenAbleToOverride() {
221248
OAuth2AuthorizationConsentService authorizationConsentService =
222249
new CustomJdbcOAuth2AuthorizationConsentService(new JdbcTemplate(db), this.registeredClientRepository);
223250
authorizationConsentService.save(AUTHORIZATION_CONSENT);
224-
OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService.findById(AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
251+
OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService.findById(
252+
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
225253
assertThat(foundAuthorizationConsent1).isEqualTo(AUTHORIZATION_CONSENT);
226254
authorizationConsentService.remove(AUTHORIZATION_CONSENT);
227-
OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService.findById(REGISTERED_CLIENT.getClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
255+
OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService.findById(
256+
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
228257
assertThat(foundAuthorizationConsent2).isNull();
229258
db.shutdown();
230259
}
231260

232-
@Before
233-
public void setUp() {
234-
this.db = createDb();
235-
this.registeredClientRepository = mock(RegisteredClientRepository.class);
236-
this.jdbcOperations = new JdbcTemplate(this.db);
237-
this.authorizationConsentService = new JdbcOAuth2AuthorizationConsentService(this.jdbcOperations, this.registeredClientRepository);
238-
}
239-
240-
@After
241-
public void tearDown() {
242-
this.db.shutdown();
243-
}
244-
245261
private static EmbeddedDatabase createDb() {
246262
return createDb(OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE);
247263
}
@@ -282,7 +298,7 @@ private static final class CustomJdbcOAuth2AuthorizationConsentService extends J
282298

283299
private static final String REMOVE_AUTHORIZATION_CONSENT_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER;
284300

285-
CustomJdbcOAuth2AuthorizationConsentService(JdbcOperations jdbcOperations, RegisteredClientRepository registeredClientRepository) {
301+
private CustomJdbcOAuth2AuthorizationConsentService(JdbcOperations jdbcOperations, RegisteredClientRepository registeredClientRepository) {
286302
super(jdbcOperations, registeredClientRepository);
287303
setAuthorizationConsentRowMapper(new CustomOAuth2AuthorizationConsentRowMapper(registeredClientRepository));
288304
}
@@ -317,16 +333,14 @@ public OAuth2AuthorizationConsent findById(String registeredClientId, String pri
317333

318334
private static final class CustomOAuth2AuthorizationConsentRowMapper extends JdbcOAuth2AuthorizationConsentService.OAuth2AuthorizationConsentRowMapper {
319335

320-
CustomOAuth2AuthorizationConsentRowMapper(RegisteredClientRepository registeredClientRepository) {
336+
private CustomOAuth2AuthorizationConsentRowMapper(RegisteredClientRepository registeredClientRepository) {
321337
super(registeredClientRepository);
322338
}
323339

324340
@Override
325341
public OAuth2AuthorizationConsent mapRow(ResultSet rs, int rowNum) throws SQLException {
326342
String registeredClientId = rs.getString("registeredClientId");
327-
328-
RegisteredClient registeredClient = getRegisteredClientRepository()
329-
.findById(registeredClientId);
343+
RegisteredClient registeredClient = getRegisteredClientRepository().findById(registeredClientId);
330344
if (registeredClient == null) {
331345
throw new DataRetrievalFailureException(
332346
"The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository.");

0 commit comments

Comments
 (0)