-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 리프레시 토큰 로테이션 및 재사용 감지 구현 #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4355b1f
feat: 리프레시 토큰 로테이션 구현
Sean-mn 85efcc0
test: RefreshTokenService 로테이션 테스트 업데이트
Sean-mn f99a726
update: RefreshTokenRedisRepository Lua 스크립트 개선
Sean-mn 7656c1a
update: 예외 메시지 한국어로 통일
Sean-mn 1274c55
feat: RotateResult 열거형 도입 및 토큰 재사용 감지 처리
Sean-mn 10f6829
update: RefreshTokenService 테스트를 RotateResult 기반으로 업데이트
Sean-mn 7fa6f99
chore: pr 스킬 수정
Sean-mn eca5bd4
update: 리프레시 토큰에 accountId 인코딩 및 역방향 Redis 키 제거
Sean-mn 9c71dc3
update: 리프레시 토큰 accountId 인코딩 방식에 맞춰 테스트 업데이트
Sean-mn 89eb282
update: Redis 검증 후 DB 조회하도록 RefreshTokenService 실행 순서 변경
Sean-mn 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
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
Fantasy-server/Fantasy.Server/Domain/Auth/Enum/RotateResult.cs
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 @@ | ||
| namespace Fantasy.Server.Domain.Auth.Enum; | ||
|
|
||
| public enum RotateResult | ||
| { | ||
| NotFound = 0, | ||
| Reused = -1, | ||
| Success = 1 | ||
| } |
4 changes: 3 additions & 1 deletion
4
...sy-server/Fantasy.Server/Domain/Auth/Repository/Interface/IRefreshTokenRedisRepository.cs
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,10 @@ | ||
| using Fantasy.Server.Domain.Auth.Enum; | ||
|
|
||
| namespace Fantasy.Server.Domain.Auth.Repository.Interface; | ||
|
|
||
| public interface IRefreshTokenRedisRepository | ||
| { | ||
| Task SaveAsync(long id, string token, TimeSpan ttl); | ||
| Task<string?> FindByIdAsync(long id); | ||
| Task<RotateResult> RotateAsync(long id, string expectedOldToken, string newToken, TimeSpan ttl); | ||
| Task DeleteAsync(long id); | ||
| } |
56 changes: 45 additions & 11 deletions
56
Fantasy-server/Fantasy.Server/Domain/Auth/Repository/RefreshTokenRedisRepository.cs
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,29 +1,63 @@ | ||
| using Fantasy.Server.Domain.Auth.Enum; | ||
| using Fantasy.Server.Domain.Auth.Repository.Interface; | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using StackExchange.Redis; | ||
|
|
||
| namespace Fantasy.Server.Domain.Auth.Repository; | ||
|
|
||
| public class RefreshTokenRedisRepository : IRefreshTokenRedisRepository | ||
| { | ||
| private readonly IDistributedCache _cache; | ||
| private const string Prefix = "fantasy:"; | ||
|
|
||
| public RefreshTokenRedisRepository(IDistributedCache cache) | ||
| private static readonly LuaScript SaveScript = LuaScript.Prepare(@" | ||
| redis.call('SET', @forwardKey, @token, 'EX', @ttl) | ||
| return 1 | ||
| "); | ||
|
|
||
| private static readonly LuaScript RotateScript = LuaScript.Prepare(@" | ||
| local current = redis.call('GET', @forwardKey) | ||
| if not current then | ||
| return 0 | ||
| end | ||
| if current ~= @expectedOldToken then | ||
| redis.call('DEL', @forwardKey) | ||
| return -1 | ||
| end | ||
| redis.call('SET', @forwardKey, @newToken, 'EX', @ttl) | ||
| return 1 | ||
| "); | ||
|
|
||
| private readonly IDatabase _db; | ||
|
|
||
| public RefreshTokenRedisRepository(IConnectionMultiplexer multiplexer) | ||
| { | ||
| _cache = cache; | ||
| _db = multiplexer.GetDatabase(); | ||
| } | ||
|
|
||
| private static string ForwardKey(long id) => $"{Prefix}refresh:{id}"; | ||
|
|
||
| public async Task SaveAsync(long id, string token, TimeSpan ttl) | ||
| { | ||
| var options = new DistributedCacheEntryOptions | ||
| await _db.ScriptEvaluateAsync(SaveScript, new | ||
| { | ||
| AbsoluteExpirationRelativeToNow = ttl | ||
| }; | ||
| await _cache.SetStringAsync($"refresh:{id}", token, options); | ||
| forwardKey = (RedisKey)ForwardKey(id), | ||
| token = (RedisValue)token, | ||
| ttl = (RedisValue)(long)ttl.TotalSeconds | ||
| }); | ||
| } | ||
|
|
||
| public async Task<string?> FindByIdAsync(long id) | ||
| => await _cache.GetStringAsync($"refresh:{id}"); | ||
| public async Task<RotateResult> RotateAsync(long id, string expectedOldToken, string newToken, TimeSpan ttl) | ||
| { | ||
| var result = await _db.ScriptEvaluateAsync(RotateScript, new | ||
| { | ||
| forwardKey = (RedisKey)ForwardKey(id), | ||
| expectedOldToken = (RedisValue)expectedOldToken, | ||
| newToken = (RedisValue)newToken, | ||
| ttl = (RedisValue)(long)ttl.TotalSeconds | ||
| }); | ||
|
|
||
| return (RotateResult)(int)(long)result; | ||
| } | ||
|
|
||
| public async Task DeleteAsync(long id) | ||
| => await _cache.RemoveAsync($"refresh:{id}"); | ||
| => await _db.KeyDeleteAsync(ForwardKey(id)); | ||
| } | ||
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
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.
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.