@@ -148,14 +148,14 @@ public async Task<Result<SecurityToken>> GenerateRefreshTokenAsync(User user, Ca
148148 public async Task < Result > ValidateTokenAsync ( SecurityToken token )
149149 {
150150 var tokenHandler = new JwtSecurityTokenHandler ( ) ;
151- var publicKey = await GetPublicKeyAsync ( ) ;
151+ var publicKeys = await GetPublicKeyAsync ( ) ;
152152
153153 var validationParameters = new TokenValidationParameters
154154 {
155155 ValidateIssuer = false ,
156156 ValidateAudience = false ,
157157 ValidateLifetime = true ,
158- IssuerSigningKey = publicKey ,
158+ IssuerSigningKeys = publicKeys ,
159159 ValidateIssuerSigningKey = true ,
160160 ClockSkew = TimeSpan . FromSeconds ( 30 )
161161 } ;
@@ -214,13 +214,41 @@ public Task<Result> ValidateRefreshTokenAsync(SecurityToken token, CancellationT
214214
215215 private async Task < RsaSecurityKey > GetPrivateKeyAsync ( CancellationToken cancellation = default )
216216 {
217- var secret = await secretCollection . GetSecretAsync ( cancellation ) ;
218- return Common . Utilities . RsaHelper . CreateSecurityKeyFromPrivateKey ( secret . PrivateKey ) ;
217+ var realm = realmProvider . GetCurrentRealm ( ) ;
218+ var filters = SecretFilters . WithSpecifications ( )
219+ . WithRealm ( realm . Id )
220+ . WithCanSign ( )
221+ . Build ( ) ;
222+
223+ var secrets = await secretCollection . GetSecretsAsync ( filters , cancellation ) ;
224+ var secret = secrets
225+ . OrderByDescending ( secret => secret . CreatedAt )
226+ . First ( ) ;
227+
228+ var key = Common . Utilities . RsaHelper . CreateSecurityKeyFromPrivateKey ( secret . PrivateKey ) ;
229+
230+ key . KeyId = secret . Id ;
231+
232+ return key ;
219233 }
220234
221- private async Task < RsaSecurityKey > GetPublicKeyAsync ( CancellationToken cancellation = default )
235+ private async Task < IReadOnlyCollection < RsaSecurityKey > > GetPublicKeyAsync ( CancellationToken cancellation = default )
222236 {
223- var secret = await secretCollection . GetSecretAsync ( cancellation ) ;
224- return Common . Utilities . RsaHelper . CreateSecurityKeyFromPublicKey ( secret . PublicKey ) ;
237+ var realm = realmProvider . GetCurrentRealm ( ) ;
238+ var filters = SecretFilters . WithSpecifications ( )
239+ . WithRealm ( realm . Id )
240+ . WithCanValidate ( )
241+ . Build ( ) ;
242+
243+ var secrets = await secretCollection . GetSecretsAsync ( filters , cancellation ) ;
244+
245+ return [ .. secrets . Select ( secret =>
246+ {
247+ var key = Common . Utilities . RsaHelper . CreateSecurityKeyFromPublicKey ( secret . PublicKey ) ;
248+
249+ key . KeyId = secret . Id ;
250+
251+ return key ;
252+ } ) ] ;
225253 }
226254}
0 commit comments