@@ -155,18 +155,24 @@ public SecurityDynamicConfiguration<?> createOrUpdateAccount(ObjectNode contentA
155155 throw new UserServiceException (NO_ACCOUNT_NAME_MESSAGE );
156156 }
157157
158- SecurityJsonNode attributeNode = securityJsonNode .get ("attributes" );
158+ // Read service flag from top level (boolean)
159+ SecurityJsonNode serviceNode = securityJsonNode .get ("service" );
160+ boolean isServiceAccount = !serviceNode .isNull () && Boolean .parseBoolean (serviceNode .asString ());
159161
160- if (!attributeNode .get ("service" ).isNull () && attributeNode .get ("service" ).asString ().equalsIgnoreCase ("true" )) { // If this is a
161- // service account
162+ if (isServiceAccount ) {
162163 verifyServiceAccount (securityJsonNode , accountName );
163164 String password = generatePassword ();
164165 contentAsNode .put ("hash" , passwordHasher .hash (password .toCharArray ()));
165- contentAsNode .put ("service" , " true" );
166+ contentAsNode .put ("service" , true );
166167 } else {
167- contentAsNode .put ("service" , " false" );
168+ contentAsNode .put ("service" , false );
168169 }
169170
171+ // Read enabled flag from top level (boolean), default to true
172+ SecurityJsonNode enabledNode = securityJsonNode .get ("enabled" );
173+ boolean isEnabled = enabledNode .isNull () || Boolean .parseBoolean (enabledNode .asString ());
174+ contentAsNode .put ("enabled" , isEnabled );
175+
170176 securityJsonNode = new SecurityJsonNode (contentAsNode );
171177 final var foundRestrictedContents = restrictedFromUsername (accountName );
172178 if (foundRestrictedContents .isPresent ()) {
@@ -185,10 +191,6 @@ public SecurityDynamicConfiguration<?> createOrUpdateAccount(ObjectNode contentA
185191 contentAsNode .remove ("password" );
186192 }
187193
188- if (!attributeNode .get ("enabled" ).isNull ()) {
189- contentAsNode .put ("enabled" , securityJsonNode .get ("enabled" ).asString ());
190- }
191-
192194 final boolean userExisted = internalUsersConfiguration .exists (accountName );
193195
194196 // sanity checks, hash is mandatory for newly created users
@@ -273,21 +275,23 @@ public AuthToken generateAuthToken(String accountName) throws IOException {
273275 final ObjectNode contentAsNode = (ObjectNode ) accountDetails ;
274276 SecurityJsonNode securityJsonNode = new SecurityJsonNode (contentAsNode );
275277
276- Optional .ofNullable (securityJsonNode .get ("attributes" ).get ("service" ))
277- .map (SecurityJsonNode ::asString )
278- .filter ("true" ::equalsIgnoreCase )
279- .orElseThrow (() -> new UserServiceException (AUTH_TOKEN_GENERATION_MESSAGE ));
278+ var serviceNode = securityJsonNode .get ("service" );
279+ boolean isService = !serviceNode .isNull () && Boolean .parseBoolean (serviceNode .asString ());
280+ if (!isService ) {
281+ throw new UserServiceException (AUTH_TOKEN_GENERATION_MESSAGE );
282+ }
280283
281- Optional .ofNullable (securityJsonNode .get ("attributes" ).get ("enabled" ))
282- .map (SecurityJsonNode ::asString )
283- .filter ("true" ::equalsIgnoreCase )
284- .orElseThrow (() -> new UserServiceException (AUTH_TOKEN_GENERATION_MESSAGE ));
284+ var enabledNode = securityJsonNode .get ("enabled" );
285+ boolean isEnabled = enabledNode .isNull () || Boolean .parseBoolean (enabledNode .asString ());
286+ if (!isEnabled ) {
287+ throw new UserServiceException (AUTH_TOKEN_GENERATION_MESSAGE );
288+ }
285289
286290 // Generate a new password for the account and store the hash of it
287291 String plainTextPassword = generatePassword ();
288292 contentAsNode .put ("hash" , passwordHasher .hash (plainTextPassword .toCharArray ()));
289- contentAsNode .put ("enabled" , " true" );
290- contentAsNode .put ("service" , " true" );
293+ contentAsNode .put ("enabled" , true );
294+ contentAsNode .put ("service" , true );
291295
292296 // Update the internal user associated with the auth token
293297 internalUsersConfiguration .remove (accountName );
@@ -296,7 +300,8 @@ public AuthToken generateAuthToken(String accountName) throws IOException {
296300 accountName ,
297301 DefaultObjectMapper .readTree (contentAsNode , internalUsersConfiguration .getImplementingClass ())
298302 );
299- saveAndUpdateConfigs (getUserConfigName ().toString (), client , CType .INTERNALUSERS , internalUsersConfiguration );
303+ saveAndUpdateConfigs (securityIndex , client , CType .INTERNALUSERS , internalUsersConfiguration );
304+ configurationRepository .reloadConfiguration (java .util .Set .of (CType .INTERNALUSERS ), null );
300305
301306 authToken = Base64 .getUrlEncoder ().encodeToString ((accountName + ":" + plainTextPassword ).getBytes (StandardCharsets .UTF_8 ));
302307 return new BasicAuthToken ("Basic " + authToken );
0 commit comments