private static async Task UpdateSubjectRefreshTokensList(string token, RefreshTokenDescription desc, IRedisConnection c) { var skKey = SubjectRefreshTokensRedisKey.Create(desc.LoginRequest.Subject); var sk = new RedisKey <SubjectRefreshTokenList>(skKey, c); var subjectRefreshTokenList = await sk.GetAsync(); if (subjectRefreshTokenList != null) { var expiredRt = subjectRefreshTokenList .Where(t => t.NotAfter < DateTime.Now) .ToArray(); foreach (var srt in expiredRt) { subjectRefreshTokenList.Remove(srt); } } else { subjectRefreshTokenList = new SubjectRefreshTokenList(); } subjectRefreshTokenList.Add(new SubjectRefreshToken { RefreshToken = token, NotAfter = desc.NotAfter }); await sk.SetAsync(subjectRefreshTokenList); }
private static async Task SaveToken(string token, RefreshTokenDescription desc, IRedisConnection c) { var tkKey = RefreshTokenRedisKey.Create(token); var tk = new RedisKey <RefreshTokenDescription>(tkKey, c); await tk.SetAsync(desc); await tk.SetExpirationAsync(desc.NotAfter - DateTime.Now); }
public async Task Register(string token, RefreshTokenDescription desc) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (desc == null) { throw new ArgumentNullException(nameof(desc)); } if (desc.NotAfter <= DateTime.Now) { throw new InvalidOperationException("Token is expired"); } using (var c = await RedisManager.GetConnection()) { await SaveToken(token, desc, c); await UpdateSubjectRefreshTokensList(token, desc, c); } }