/// <summary> /// Stores the asynchronous. /// </summary> /// <param name="token">The token.</param> /// <returns></returns> public Task StoreAsync(PersistedGrant token) { var existing = PersistedGrants.FindByKey(token.Key); if (existing == null) { _logger.LogDebug("{persistedGrantKey} not found in database", token.Key); existing = token.ToEntity(); } else { _logger.LogDebug("{persistedGrantKey} found in database", token.Key); token.UpdateEntity(existing); } try { existing.Save(); } catch (Exception ex) { _logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message); } return(Task.FromResult(0)); }
/// <summary> /// Gets the grant. /// </summary> /// <param name="key">The key.</param> /// <returns></returns> public Task <PersistedGrant> GetAsync(string key) { var persistedGrant = PersistedGrants.Find(PersistedGrants._.Key == key); var model = persistedGrant?.ToModel(); _logger.LogDebug("{persistedGrantKey} found in database: {persistedGrantKeyFound}", key, model != null); return(Task.FromResult(model)); }
/// <summary> /// Gets all grants for a given subject id. /// </summary> /// <param name="subjectId">The subject identifier.</param> /// <returns></returns> public Task <IEnumerable <PersistedGrant> > GetAllAsync(string subjectId) { var persistedGrants = PersistedGrants.FindAllBySubjectId(subjectId); var model = persistedGrants.Select(x => x.ToModel()); _logger.LogDebug("{persistedGrantCount} persisted grants found for {subjectId}", persistedGrants.Count, subjectId); return(Task.FromResult(model)); }
public static PersistedGrant MapPersistedGrant(this PersistedGrants persistedGrants) { return(new PersistedGrant() { Key = persistedGrants.Key, Type = persistedGrants.Type, SubjectId = persistedGrants.SubjectId, ClientId = persistedGrants.ClientId, CreationTime = persistedGrants.CreationTime, Expiration = persistedGrants.Expiration, Data = persistedGrants.Data }); }
/// <summary> /// Maps an entity to a model. /// </summary> /// <param name="entity">The entity.</param> /// <returns></returns> public static PersistedGrant ToModel(this PersistedGrants entity) { if (entity == null) { return(null); } var model = new PersistedGrant(); Mapper.Copy(model, entity); return(model); }
/// <summary> /// Maps a model to an entity. /// </summary> /// <param name="model">The model.</param> /// <returns></returns> public static PersistedGrants ToEntity(this PersistedGrant model) { if (model == null) { return(null); } var entity = new PersistedGrants(); Mapper.Copy(entity, model); return(entity); }
/// <summary> /// Method to clear expired persisted grants. /// </summary> /// <returns></returns> public async Task RemoveExpiredGrantsAsync() { try { _logger.LogTrace("Querying for expired grants to remove"); var found = Int32.MaxValue; using (var serviceScope = _serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { var tokenCleanupNotification = ServiceProviderServiceExtensions.GetService <IOperationalStoreNotification>(serviceScope.ServiceProvider); { while (found >= _options.TokenCleanupBatchSize) { var expired = PersistedGrants.FindAllByExpirationAndBatchSize(_options.TokenCleanupBatchSize); found = expired.Count; _logger.LogInformation("Removing {grantCount} grants", found); if (found > 0) { try { expired.Delete(); if (tokenCleanupNotification != null) { await tokenCleanupNotification.PersistedGrantsRemovedAsync(expired); } } catch (Exception ex) { // we get this if/when someone else already deleted the records // we want to essentially ignore this, and keep working _logger.LogDebug("Concurrency exception removing expired grants: {exception}", ex.Message); } } } } } } catch (Exception ex) { _logger.LogError("Exception removing expired grants: {exception}", ex.Message); } }
/// <summary> /// Removes all grants of a give type for a given subject id and client id combination. /// </summary> /// <param name="subjectId">The subject identifier.</param> /// <param name="clientId">The client identifier.</param> /// <param name="type">The type.</param> /// <returns></returns> public Task RemoveAllAsync(string subjectId, string clientId, string type) { var persistedGrants = PersistedGrants.FindAllByTypeAndSubjectIdAndClientId(type, subjectId, type); _logger.LogDebug("removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}, grantType {persistedGrantType}", persistedGrants.Count, subjectId, clientId, type); try { persistedGrants.Delete(); } catch (Exception ex) { _logger.LogInformation("exception removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}, grantType {persistedGrantType}: {error}", persistedGrants.Count, subjectId, clientId, type, ex.Message); } return(Task.FromResult(0)); }
/// <summary> /// Removes the grant by key. /// </summary> /// <param name="key">The key.</param> /// <returns></returns> public Task RemoveAsync(string key) { var persistedGrant = PersistedGrants.FindByKey(key); if (persistedGrant != null) { _logger.LogDebug("removing {persistedGrantKey} persisted grant from database", key); try { persistedGrant.Delete(); } catch (Exception ex) { _logger.LogInformation("exception removing {persistedGrantKey} persisted grant from database: {error}", key, ex.Message); } } else { _logger.LogDebug("no {persistedGrantKey} persisted grant found in database", key); } return(Task.FromResult(0)); }
/// <summary> /// Updates an entity from a model. /// </summary> /// <param name="model">The model.</param> /// <param name="entity">The entity.</param> public static void UpdateEntity(this PersistedGrant model, PersistedGrants entity) { Mapper.Copy(entity, model); }