Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit a4f4f5d

Browse files
authored
FF-174 Create UserId generation (#46)
* FF-174 Create UserId generation * Rewrote decoding exception catch. * Rearranged Code * Fixed Security Issue.
1 parent 46349ea commit a4f4f5d

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

src/main/java/de/filefighter/rest/domain/user/business/UserAuthorizationService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.slf4j.LoggerFactory;
1313
import org.springframework.stereotype.Service;
1414

15-
import java.io.UnsupportedEncodingException;
1615
import java.nio.charset.StandardCharsets;
1716
import java.util.Base64;
1817

@@ -39,9 +38,10 @@ public User authenticateUserWithUsernameAndPassword(String base64encodedUserAndP
3938
String decodedUsernameAndPassword = "";
4039
try {
4140
byte[] decodedValue = Base64.getDecoder().decode(base64encodedUserAndPassword);
42-
decodedUsernameAndPassword = new String(decodedValue, StandardCharsets.UTF_8.toString());
43-
} catch (UnsupportedEncodingException ex) {
44-
LOG.warn("Found UnsupportedEncodingException {} in {}",ex.getMessage(), base64encodedUserAndPassword);
41+
decodedUsernameAndPassword = new String(decodedValue, StandardCharsets.UTF_8);
42+
} catch (IllegalArgumentException ex) {
43+
LOG.warn("Found {} in {}", ex.getMessage(), base64encodedUserAndPassword);
44+
throw new RequestDidntMeetFormalRequirementsException("Found unsupported character in header.");
4545
}
4646

4747
String[] split = decodedUsernameAndPassword.split(":");

src/main/java/de/filefighter/rest/domain/user/business/UserBusinessService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.mongodb.core.query.Update;
2323
import org.springframework.stereotype.Service;
2424

25+
import java.security.SecureRandom;
2526
import java.util.Arrays;
2627
import java.util.regex.Pattern;
2728

@@ -38,6 +39,8 @@ public class UserBusinessService {
3839

3940

4041
private static final Logger LOG = LoggerFactory.getLogger(UserBusinessService.class);
42+
public static final int USER_ID_MAX = 99999999;
43+
4144

4245
@Value("${filefighter.disable-password-check}")
4346
public boolean passwordCheckDisabled;
@@ -138,7 +141,7 @@ public void registerNewUser(UserRegisterForm newUser) {
138141
.username(username)
139142
.password(password)
140143
.refreshToken(AccessTokenBusinessService.generateRandomTokenValue())
141-
.userId(getUserCount() + 1)
144+
.userId(generateRandomUserId())
142145
.build());
143146
}
144147

@@ -234,5 +237,19 @@ public void updateUser(long userId, UserRegisterForm userToUpdate, User authenti
234237
query.addCriteria(Criteria.where("userId").is(userId));
235238
mongoTemplate.findAndModify(query, newUpdate, UserEntity.class);
236239
}
240+
241+
public long generateRandomUserId(){
242+
long possibleUserId = 0L;
243+
boolean userIdIsFree = false;
244+
245+
while(!userIdIsFree){
246+
possibleUserId = new SecureRandom().nextInt(UserBusinessService.USER_ID_MAX);
247+
UserEntity userEntity = userRepository.findByUserId(possibleUserId);
248+
if(null == userEntity && possibleUserId > 0)
249+
userIdIsFree = true;
250+
}
251+
252+
return possibleUserId;
253+
}
237254
}
238255

src/test/java/de/filefighter/rest/domain/user/business/UserAuthorizationServiceUnitTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ void authenticateUserWithUsernameAndPasswordThrows() {
2929
String matchesButDoesNotMeetRequirements = AUTHORIZATION_BASIC_PREFIX + "dWdhYnVnYQ==";
3030
String matchesButUserWasNotFound = AUTHORIZATION_BASIC_PREFIX + "dXNlcjpwYXNzd29yZA==";
3131

32-
assertThrows(RuntimeException.class, () ->
32+
assertThrows(RequestDidntMeetFormalRequirementsException.class, () ->
3333
userAuthorizationService.authenticateUserWithUsernameAndPassword(matchesButIsNotSupportedEncoding)
3434
);
35+
3536
assertThrows(RequestDidntMeetFormalRequirementsException.class, () ->
3637
userAuthorizationService.authenticateUserWithUsernameAndPassword(matchesButDoesNotMeetRequirements)
3738
);

src/test/java/de/filefighter/rest/domain/user/business/UserBusinessServiceUnitTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void registerNewUserWorks() {
208208
String username = "username";
209209
String password = "validPassword1234";
210210
String confPassword = "validPassword1234";
211-
long[] groups = null;
211+
long[] groups = new long[]{0};
212212

213213
UserRegisterForm userRegisterForm = UserRegisterForm.builder()
214214
.username(username)
@@ -291,7 +291,7 @@ void updatePasswordThrows() {
291291
assertThrows(UserNotUpdatedException.class, () ->
292292
userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser), "Passwords do not match.");
293293

294-
String validPassword ="ValidPassword1234!=";
294+
String validPassword = "ValidPassword1234!=";
295295
userRegisterForm.setPassword(validPassword);
296296
userRegisterForm.setConfirmationPassword(validPassword);
297297
when(userRepositoryMock.findByUserId(userId)).thenReturn(dummyEntity);
@@ -325,7 +325,7 @@ void updateGroupsThrows() {
325325
assertThrows(UserNotUpdatedException.class, () ->
326326
userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser));
327327

328-
groups = new long[]{123032,1230213};
328+
groups = new long[]{123032, 1230213};
329329
userRegisterForm.setGroupIds(groups);
330330
when(userRepositoryMock.findByUserId(userId)).thenReturn(dummyEntity);
331331
when(groupRepositoryMock.getGroupsByIds(groups)).thenThrow(new IllegalArgumentException("id doesnt belong to a group"));
@@ -346,4 +346,10 @@ void updateGroupsWorks() {
346346
when(groupRepositoryMock.getGroupsByIds(groups)).thenReturn(new Groups[]{Groups.FAMILY});
347347
assertDoesNotThrow(() -> userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser));
348348
}
349+
350+
@Test
351+
void generateRandomUserIdWorks() {
352+
long actualValue = userBusinessService.generateRandomUserId();
353+
assertTrue(0 <= actualValue && actualValue <= UserBusinessService.USER_ID_MAX);
354+
}
349355
}

0 commit comments

Comments
 (0)