This repository was archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
FF-109, FF-110 Rework Authentication Architecture #27
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/de/filefighter/rest/domain/common/Utils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package de.filefighter.rest.domain.common; | ||
|
||
import de.filefighter.rest.rest.exceptions.RequestDidntMeetFormalRequirementsException; | ||
|
||
public class Utils { | ||
|
||
public static boolean stringIsValid(String s){ | ||
return !(null == s || s.isEmpty() || s.isBlank()); | ||
} | ||
|
||
public static String validateAuthorizationHeader(String header, String testString){ | ||
if(!stringIsValid(testString)) | ||
throw new RequestDidntMeetFormalRequirementsException("Header does not contain a valid String."); | ||
|
||
if (!testString.matches("^" + header + "[^\\s](.*)$")) | ||
throw new RequestDidntMeetFormalRequirementsException("Header does not contain '" + header + "', or format is invalid."); | ||
String[] split = testString.split(header); | ||
return split[1]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/filefighter/rest/domain/user/business/UserAuthorizationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.filefighter.rest.domain.user.business; | ||
|
||
import de.filefighter.rest.domain.common.Utils; | ||
import de.filefighter.rest.domain.token.data.dto.AccessToken; | ||
import de.filefighter.rest.domain.user.data.dto.User; | ||
import de.filefighter.rest.domain.user.data.persistance.UserEntity; | ||
import de.filefighter.rest.domain.user.data.persistance.UserRepository; | ||
import de.filefighter.rest.domain.user.exceptions.UserNotAuthenticatedException; | ||
import de.filefighter.rest.rest.exceptions.RequestDidntMeetFormalRequirementsException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
import static de.filefighter.rest.configuration.RestConfiguration.AUTHORIZATION_BASIC_PREFIX; | ||
import static de.filefighter.rest.configuration.RestConfiguration.AUTHORIZATION_BEARER_PREFIX; | ||
|
||
@Service | ||
public class UserAuthorizationService { | ||
|
||
private final UserRepository userRepository; | ||
private final UserDtoService userDtoService; | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UserAuthorizationService.class); | ||
|
||
public UserAuthorizationService(UserRepository userRepository, UserDtoService userDtoService) { | ||
this.userRepository = userRepository; | ||
this.userDtoService = userDtoService; | ||
} | ||
|
||
public User authenticateUserWithUsernameAndPassword(String base64encodedUserAndPassword) { | ||
String decodedUsernameUndPassword; | ||
try { | ||
byte[] decodedValue = Base64.getDecoder().decode(base64encodedUserAndPassword); | ||
decodedUsernameUndPassword = new String(decodedValue, StandardCharsets.UTF_8.toString()); | ||
} catch (UnsupportedEncodingException | IllegalArgumentException ex) { | ||
LOG.warn("Found UnsupportedEncodingException in {}", base64encodedUserAndPassword); | ||
throw new RuntimeException(ex); | ||
} | ||
|
||
String[] split = decodedUsernameUndPassword.strip().split(":"); | ||
|
||
if (split.length != 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so ":" is not allowed in the password? |
||
throw new RequestDidntMeetFormalRequirementsException("Credentials didnt meet formal requirements."); | ||
|
||
String username = split[0]; | ||
String password = split[1]; | ||
|
||
UserEntity userEntity = userRepository.findByUsernameAndPassword(username, password); | ||
if (null == userEntity) | ||
throw new UserNotAuthenticatedException("No User found with this username and password."); | ||
|
||
return userDtoService.createDto(userEntity); | ||
} | ||
|
||
public User authenticateUserWithRefreshToken(String refreshToken) { | ||
UserEntity userEntity = userRepository.findByRefreshToken(refreshToken); | ||
if (null == userEntity) | ||
throw new UserNotAuthenticatedException("No user found for this Refresh Token."); | ||
|
||
return userDtoService.createDto(userEntity); | ||
} | ||
|
||
public void authenticateUserWithAccessToken(AccessToken accessToken) { | ||
UserEntity userEntity = userRepository.findByUserId(accessToken.getUserId()); | ||
if (null == userEntity) | ||
throw new UserNotAuthenticatedException(accessToken.getUserId()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you shoud not log the password :)