1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import java .util .Set ;
29
29
import java .util .function .Function ;
30
30
31
+ import org .springframework .dao .DuplicateKeyException ;
31
32
import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
32
33
import org .springframework .jdbc .core .JdbcOperations ;
33
34
import org .springframework .jdbc .core .PreparedStatementSetter ;
@@ -112,6 +113,24 @@ public final class JdbcUserCredentialRepository implements UserCredentialReposit
112
113
113
114
private static final String DELETE_CREDENTIAL_RECORD_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + ID_FILTER ;
114
115
116
+ // @formatter:off
117
+ private static final String UPDATE_CREDENTIAL_RECORD_SQL = "UPDATE " + TABLE_NAME
118
+ + " SET user_entity_user_id = ?, " +
119
+ "public_key = ?, " +
120
+ "signature_count = ?, " +
121
+ "uv_initialized = ?, " +
122
+ "backup_eligible = ? ," +
123
+ "authenticator_transports = ?, " +
124
+ "public_key_credential_type = ?, " +
125
+ "backup_state = ?, " +
126
+ "attestation_object = ?, " +
127
+ "attestation_client_data_json = ?, " +
128
+ "created = ?, " +
129
+ "last_used = ?, " +
130
+ "label = ?"
131
+ + " WHERE " + ID_FILTER ;
132
+ // @formatter:on
133
+
115
134
/**
116
135
* Constructs a {@code JdbcUserCredentialRepository} using the provided parameters.
117
136
* @param jdbcOperations the JDBC operations
@@ -133,6 +152,18 @@ public void delete(Bytes credentialId) {
133
152
@ Override
134
153
public void save (CredentialRecord record ) {
135
154
Assert .notNull (record , "record cannot be null" );
155
+ int rows = updateCredentialRecord (record );
156
+ if (rows == 0 ) {
157
+ try {
158
+ insertCredentialRecord (record );
159
+ }
160
+ catch (DuplicateKeyException ex ) {
161
+ updateCredentialRecord (record );
162
+ }
163
+ }
164
+ }
165
+
166
+ private void insertCredentialRecord (CredentialRecord record ) {
136
167
List <SqlParameterValue > parameters = this .credentialRecordParametersMapper .apply (record );
137
168
try (LobCreator lobCreator = this .lobHandler .getLobCreator ()) {
138
169
PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter (lobCreator ,
@@ -141,6 +172,17 @@ public void save(CredentialRecord record) {
141
172
}
142
173
}
143
174
175
+ private int updateCredentialRecord (CredentialRecord record ) {
176
+ List <SqlParameterValue > parameters = this .credentialRecordParametersMapper .apply (record );
177
+ SqlParameterValue credentialId = parameters .remove (0 );
178
+ parameters .add (credentialId );
179
+ try (LobCreator lobCreator = this .lobHandler .getLobCreator ()) {
180
+ PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter (lobCreator ,
181
+ parameters .toArray ());
182
+ return this .jdbcOperations .update (UPDATE_CREDENTIAL_RECORD_SQL , pss );
183
+ }
184
+ }
185
+
144
186
@ Override
145
187
public CredentialRecord findByCredentialId (Bytes credentialId ) {
146
188
Assert .notNull (credentialId , "credentialId cannot be null" );
0 commit comments