// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using IdentityServer4.MongoDB.Interfaces; using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading.Tasks; namespace Streetwriters.Identity.Services { public class TokenCleanup { private readonly IPersistedGrantDbContext _persistedGrantDbContext; private readonly ILogger _logger; public TokenCleanup(IPersistedGrantDbContext persistedGrantDbContext, ILogger logger) { _persistedGrantDbContext = persistedGrantDbContext; _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } /// /// Method to clear expired persisted grants. /// /// public async Task RemoveExpiredGrantsAsync() { try { _logger.LogTrace("Querying for expired grants to remove"); await RemoveGrantsAsync(); //TODO: await RemoveDeviceCodesAsync(); } catch (Exception ex) { _logger.LogError("Exception removing expired grants: {exception}", ex.Message); } } /// /// Removes the stale persisted grants. /// /// protected virtual async Task RemoveGrantsAsync() { await _persistedGrantDbContext.RemoveExpired(); } } }