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
Feature/user authorization #9
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3e4250a
Added Steps, and fixed feature
open-schnick 3af3e2f
Added default users for dev, fixed wrong param names "roles"
open-schnick 1d0b1a7
FF-89 implemented logic, fixed cucumber tests.
open-schnick eaa33ec
Implemented requested changes by @qvalentin (#9)
open-schnick c5ea717
Fixed test.
open-schnick 681f589
Added UnitTests (1/2) - Some Cleanup
open-schnick abd9efd
Added UnitTests (2/2) - Cleanup, fixes
open-schnick 6fd0557
implemented changes requested by @qvalentin
open-schnick 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Run Cucumber Tests" type="JUnit" factoryName="JUnit"> | ||
<useClassPathOnly /> | ||
<option name="PACKAGE_NAME" value="de.filefighter.rest.cucumber" /> | ||
<option name="MAIN_CLASS_NAME" value="" /> | ||
<option name="METHOD_NAME" value="" /> | ||
<option name="TEST_OBJECT" value="package" /> | ||
<option name="PARAMETERS" value="" /> | ||
<option name="TEST_SEARCH_SCOPE"> | ||
<value defaultName="wholeProject" /> | ||
</option> | ||
<method v="2"> | ||
<option name="Make" enabled="true" /> | ||
</method> | ||
</configuration> | ||
</component> |
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
6 changes: 6 additions & 0 deletions
6
src/main/java/de/filefighter/rest/domain/common/DtoServiceInterface.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,6 @@ | ||
package de.filefighter.rest.domain.common; | ||
|
||
public interface DtoServiceInterface<D,E> { | ||
D createDto(E entity); | ||
E findEntity(D dto); | ||
} |
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,8 @@ | ||
package de.filefighter.rest.domain.common; | ||
|
||
public class Utils { | ||
|
||
public static boolean stringIsValid(String s){ | ||
return !(null == s || s.isEmpty() || s.isBlank()); | ||
} | ||
} |
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
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/token/business/AccessTokenBusinessService.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,80 @@ | ||
package de.filefighter.rest.domain.token.business; | ||
|
||
import de.filefighter.rest.domain.token.data.dto.AccessToken; | ||
import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity; | ||
import de.filefighter.rest.domain.token.data.persistance.AccessTokenRepository; | ||
import de.filefighter.rest.domain.token.exceptions.AccessTokenNotFoundException; | ||
import de.filefighter.rest.domain.user.data.dto.User; | ||
import de.filefighter.rest.domain.user.exceptions.UserNotAuthenticatedException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
import static de.filefighter.rest.configuration.RestConfiguration.AUTHORIZATION_BASIC_PREFIX; | ||
import static de.filefighter.rest.configuration.RestConfiguration.AUTHORIZATION_BEARER_PREFIX; | ||
import static de.filefighter.rest.domain.common.Utils.stringIsValid; | ||
|
||
@Service | ||
public class AccessTokenBusinessService { | ||
|
||
private final AccessTokenRepository accessTokenRepository; | ||
private final AccessTokenDtoService accessTokenDtoService; | ||
|
||
public static final long ACCESS_TOKEN_DURATION_IN_SECONDS = 3600L; | ||
public static final long ACCESS_TOKEN_SAFETY_MARGIN = 5L; | ||
|
||
public AccessTokenBusinessService(AccessTokenRepository accessTokenRepository, AccessTokenDtoService accessTokenDtoService) { | ||
this.accessTokenRepository = accessTokenRepository; | ||
this.accessTokenDtoService = accessTokenDtoService; | ||
} | ||
|
||
public AccessToken getValidAccessTokenForUser(User user) { | ||
AccessTokenEntity accessTokenEntity = accessTokenRepository.findByUserId(user.getId()); | ||
long currentTimeSeconds = Instant.now().getEpochSecond(); | ||
|
||
if (null == accessTokenEntity) { | ||
accessTokenEntity = AccessTokenEntity | ||
.builder() | ||
.validUntil(currentTimeSeconds + ACCESS_TOKEN_DURATION_IN_SECONDS) | ||
.value(this.generateRandomTokenValue()) | ||
.userId(user.getId()) | ||
.build(); | ||
accessTokenEntity = accessTokenRepository.save(accessTokenEntity); | ||
} else { | ||
if (currentTimeSeconds + ACCESS_TOKEN_SAFETY_MARGIN > accessTokenEntity.getValidUntil()) { | ||
accessTokenRepository.delete(accessTokenEntity); | ||
accessTokenEntity = AccessTokenEntity | ||
.builder() | ||
.validUntil(currentTimeSeconds + ACCESS_TOKEN_DURATION_IN_SECONDS) | ||
.value(this.generateRandomTokenValue()) | ||
.userId(user.getId()) | ||
.build(); | ||
accessTokenEntity = accessTokenRepository.save(accessTokenEntity); | ||
} | ||
} | ||
|
||
return accessTokenDtoService.createDto(accessTokenEntity); | ||
} | ||
|
||
public AccessToken findAccessTokenByValueAndUserId(String accessTokenValue, long userId) { | ||
if (!stringIsValid(accessTokenValue)) | ||
throw new IllegalArgumentException("Value of AccessToken was not valid."); | ||
|
||
AccessTokenEntity accessTokenEntity = accessTokenRepository.findByUserIdAndValue(userId, accessTokenValue); | ||
if (null == accessTokenEntity) | ||
throw new UserNotAuthenticatedException(userId); | ||
|
||
return accessTokenDtoService.createDto(accessTokenEntity); | ||
} | ||
|
||
public String generateRandomTokenValue() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
public String checkBearerHeader(String accessTokenValue) { | ||
if (!accessTokenValue.matches("^" + AUTHORIZATION_BEARER_PREFIX + "[^\\s](.*)$")) | ||
throw new UserNotAuthenticatedException("Header does not contain '" + AUTHORIZATION_BEARER_PREFIX + "', or format is invalid."); | ||
return accessTokenValue.split(AUTHORIZATION_BEARER_PREFIX)[1]; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/de/filefighter/rest/domain/token/business/AccessTokenDtoService.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,37 @@ | ||
package de.filefighter.rest.domain.token.business; | ||
|
||
import de.filefighter.rest.domain.common.DtoServiceInterface; | ||
import de.filefighter.rest.domain.token.data.dto.AccessToken; | ||
import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity; | ||
import de.filefighter.rest.domain.token.data.persistance.AccessTokenRepository; | ||
import de.filefighter.rest.domain.token.exceptions.AccessTokenNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AccessTokenDtoService implements DtoServiceInterface<AccessToken, AccessTokenEntity> { | ||
|
||
private final AccessTokenRepository accessTokenRepository; | ||
|
||
public AccessTokenDtoService(AccessTokenRepository accessTokenRepository) { | ||
this.accessTokenRepository = accessTokenRepository; | ||
} | ||
|
||
@Override | ||
public AccessToken createDto(AccessTokenEntity entity) { | ||
return AccessToken | ||
.builder() | ||
.token(entity.getValue()) | ||
.userId(entity.getUserId()) | ||
.validUntil(entity.getValidUntil()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AccessTokenEntity findEntity(AccessToken dto) { | ||
AccessTokenEntity accessTokenEntity = accessTokenRepository.findByUserIdAndValue(dto.getUserId(), dto.getToken()); | ||
if (null == accessTokenEntity) | ||
throw new AccessTokenNotFoundException("AccessTokenEntity does not exist for AccessToken: "+ dto); | ||
|
||
return accessTokenEntity; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,5 +15,5 @@ public class AccessTokenEntity { | |
private String value; | ||
private long userId; | ||
private long validUntil; | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/de/filefighter/rest/domain/token/exceptions/AccessTokenNotFoundException.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,8 @@ | ||
package de.filefighter.rest.domain.token.exceptions; | ||
|
||
public class AccessTokenNotFoundException extends RuntimeException { | ||
|
||
public AccessTokenNotFoundException(String reason) { | ||
super(reason); | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
src/main/java/de/filefighter/rest/domain/token/exceptions/TokenNotFoundException.java
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.