-
-
Notifications
You must be signed in to change notification settings - Fork 27
Refresh token #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
georgebejan
wants to merge
1
commit into
code4romania:develop
Choose a base branch
from
georgebejan:refresh_token
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refresh token #50
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions
21
api/src/main/java/com/code4ro/nextdoor/authentication/entity/RefreshToken.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,21 @@ | ||
| package com.code4ro.nextdoor.authentication.entity; | ||
|
|
||
| import com.code4ro.nextdoor.core.entity.BaseEntity; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import java.util.Date; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| @Entity | ||
| public class RefreshToken extends BaseEntity { | ||
| private String userId; | ||
| private String token; | ||
| private Date expiryDate; | ||
| } |
12 changes: 12 additions & 0 deletions
12
api/src/main/java/com/code4ro/nextdoor/authentication/repository/RefreshTokenRepository.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,12 @@ | ||
| package com.code4ro.nextdoor.authentication.repository; | ||
|
|
||
|
|
||
| import com.code4ro.nextdoor.authentication.entity.RefreshToken; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| public interface RefreshTokenRepository extends JpaRepository<RefreshToken, UUID> { | ||
| List<RefreshToken> findAllByUserId(String userId); | ||
| } |
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
9 changes: 9 additions & 0 deletions
9
api/src/main/java/com/code4ro/nextdoor/authentication/service/RefreshTokenService.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,9 @@ | ||
| package com.code4ro.nextdoor.authentication.service; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface RefreshTokenService { | ||
| boolean isValid(UUID userId, String jti); | ||
|
|
||
| String generate(UUID userId); | ||
| } |
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
61 changes: 61 additions & 0 deletions
61
...c/main/java/com/code4ro/nextdoor/authentication/service/impl/RefreshTokenServiceImpl.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,61 @@ | ||
| package com.code4ro.nextdoor.authentication.service.impl; | ||
|
|
||
| import com.code4ro.nextdoor.authentication.entity.RefreshToken; | ||
| import com.code4ro.nextdoor.authentication.repository.RefreshTokenRepository; | ||
| import com.code4ro.nextdoor.authentication.service.RefreshTokenService; | ||
| import com.code4ro.nextdoor.security.entity.RefreshTokenHolder; | ||
| import com.code4ro.nextdoor.security.jwt.JwtTokenProvider; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| public class RefreshTokenServiceImpl implements RefreshTokenService { | ||
| private final RefreshTokenRepository refreshTokenRepository; | ||
| private final JwtTokenProvider tokenProvider; | ||
| private final PasswordEncoder passwordEncoder; | ||
|
|
||
| @Autowired | ||
| public RefreshTokenServiceImpl(final RefreshTokenRepository refreshTokenRepository, | ||
| final JwtTokenProvider tokenProvider, | ||
| final PasswordEncoder passwordEncoder) { | ||
| this.refreshTokenRepository = refreshTokenRepository; | ||
| this.tokenProvider = tokenProvider; | ||
| this.passwordEncoder = passwordEncoder; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isValid(final UUID userId, final String jti) { | ||
| final List<RefreshToken> refreshTokens = refreshTokenRepository.findAllByUserId(userId.toString()); | ||
| final Optional<RefreshToken> refreshTokenOptional = refreshTokens.stream() | ||
| .filter(rt -> passwordEncoder.matches(jti, rt.getToken())) | ||
| .findFirst(); | ||
| if (refreshTokenOptional.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| final RefreshToken refreshToken = refreshTokenOptional.get(); | ||
| final boolean isValid = refreshToken.getExpiryDate().after(new Date()); | ||
| if (!isValid) { | ||
| refreshTokenRepository.delete(refreshToken); | ||
| } | ||
|
|
||
| return isValid; | ||
| } | ||
|
|
||
| @Override | ||
| public String generate(final UUID userId) { | ||
| final String refreshToken = UUID.randomUUID().toString(); | ||
| final RefreshTokenHolder refreshTokenHolder = tokenProvider.generateRefreshToken(userId, refreshToken); | ||
| final RefreshToken refreshTokenEntity = refreshTokenHolder.getRefreshToken(); | ||
| refreshTokenEntity.setToken(passwordEncoder.encode(refreshToken)); | ||
| refreshTokenRepository.save(refreshTokenEntity); | ||
|
|
||
| return refreshTokenHolder.getRefreshTokenJwt(); | ||
| } | ||
| } |
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
14 changes: 14 additions & 0 deletions
14
api/src/main/java/com/code4ro/nextdoor/security/entity/RefreshTokenHolder.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,14 @@ | ||
| package com.code4ro.nextdoor.security.entity; | ||
|
|
||
| import com.code4ro.nextdoor.authentication.entity.RefreshToken; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor | ||
| public class RefreshTokenHolder { | ||
| private final String refreshTokenJwt; | ||
| private final RefreshToken refreshToken; | ||
| } |
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.
Why is a custom implementation for this and not a normal authentication flow?
Default SpringSecurity Authentication using JWT is super simple to implement without extra work needed.
And for the long run is the way to go