|
| 1 | +// Project: aguacongas/DymamicAuthProviders |
| 2 | +// Copyright (c) 2021 @Olivier Lefebvre |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using Raven.Client.Documents; |
| 5 | +using Raven.Client.Documents.Session; |
| 6 | +using System; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Aguacongas.AspNetCore.Authentication.RavenDb |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Implement a store for <see cref="NoPersistentDynamicManager{TSchemeDefinition}"/> with EntityFramework. |
| 15 | + /// </summary> |
| 16 | + /// <seealso cref="IDynamicProviderStore{TSchemeDefinition}" /> |
| 17 | + public class DynamicProviderStore : DynamicProviderStore<SchemeDefinition> |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="DynamicProviderStore"/> class. |
| 21 | + /// </summary> |
| 22 | + /// <param name="session">The document session.</param> |
| 23 | + /// <param name="authenticationSchemeOptionsSerializer">The authentication scheme options serializer.</param> |
| 24 | + /// <param name="logger">The logger.</param> |
| 25 | + public DynamicProviderStore(IAsyncDocumentSession session, IAuthenticationSchemeOptionsSerializer authenticationSchemeOptionsSerializer, ILogger<DynamicProviderStore> logger) : base(session, authenticationSchemeOptionsSerializer, logger) |
| 26 | + { |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Implement a store for <see cref="NoPersistentDynamicManager{TSchemeDefinition}"/> with EntityFramework. |
| 32 | + /// </summary> |
| 33 | + /// <typeparam name="TSchemeDefinition">The type of the definition.</typeparam> |
| 34 | + public class DynamicProviderStore<TSchemeDefinition> : IDynamicProviderStore<TSchemeDefinition> |
| 35 | + where TSchemeDefinition : SchemeDefinition, new() |
| 36 | + { |
| 37 | + private readonly IAsyncDocumentSession _session; |
| 38 | + private readonly IAuthenticationSchemeOptionsSerializer _authenticationSchemeOptionsSerializer; |
| 39 | + private readonly ILogger<DynamicProviderStore<TSchemeDefinition>> _logger; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets the scheme definitions list. |
| 43 | + /// </summary> |
| 44 | + /// <value> |
| 45 | + /// The scheme definitions list. |
| 46 | + /// </value> |
| 47 | + public virtual IQueryable<TSchemeDefinition> SchemeDefinitions => _session.Query<SerializedData>() |
| 48 | + .ToListAsync().ConfigureAwait(false).GetAwaiter().GetResult() |
| 49 | + .Select(Deserialize) |
| 50 | + .AsQueryable(); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="DynamicProviderStore{TSchemeDefinition}"/> class. |
| 54 | + /// </summary> |
| 55 | + /// <param name="session">The document session.</param> |
| 56 | + /// <param name="authenticationSchemeOptionsSerializer">The authentication scheme options serializer.</param> |
| 57 | + /// <param name="logger">The logger.</param> |
| 58 | + /// <exception cref="ArgumentNullException"> |
| 59 | + /// context |
| 60 | + /// or |
| 61 | + /// authenticationSchemeOptionsSerializer |
| 62 | + /// or |
| 63 | + /// logger |
| 64 | + /// </exception> |
| 65 | + public DynamicProviderStore(IAsyncDocumentSession session, IAuthenticationSchemeOptionsSerializer authenticationSchemeOptionsSerializer, ILogger<DynamicProviderStore<TSchemeDefinition>> logger) |
| 66 | + { |
| 67 | + _session = session ?? throw new ArgumentNullException(nameof(session)); |
| 68 | + _authenticationSchemeOptionsSerializer = authenticationSchemeOptionsSerializer ?? throw new ArgumentNullException(nameof(authenticationSchemeOptionsSerializer)); |
| 69 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Adds a defnition asynchronously. |
| 74 | + /// </summary> |
| 75 | + /// <param name="definition">The definition.</param> |
| 76 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 77 | + /// <returns></returns> |
| 78 | + /// <exception cref="ArgumentNullException">definition</exception> |
| 79 | + public virtual async Task AddAsync(TSchemeDefinition definition, CancellationToken cancellationToken = default) |
| 80 | + { |
| 81 | + definition = definition ?? throw new ArgumentNullException(nameof(definition)); |
| 82 | + |
| 83 | + cancellationToken.ThrowIfCancellationRequested(); |
| 84 | + |
| 85 | + var data = Serialize(definition); |
| 86 | + await _session.StoreAsync(data, cancellationToken).ConfigureAwait(false); |
| 87 | + await _session.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| 88 | + |
| 89 | + _logger.LogInformation("Scheme {scheme} added for {handlerType} with options: {options}", definition.Scheme, definition.HandlerType, data.SerializedOptions); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Removes a scheme definition asynchronous. |
| 94 | + /// </summary> |
| 95 | + /// <param name="definition">The definition.</param> |
| 96 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 97 | + /// <returns></returns> |
| 98 | + /// <exception cref="ArgumentNullException">definition</exception> |
| 99 | + public virtual async Task RemoveAsync(TSchemeDefinition definition, CancellationToken cancellationToken = default) |
| 100 | + { |
| 101 | + definition = definition ?? throw new ArgumentNullException(nameof(definition)); |
| 102 | + |
| 103 | + cancellationToken.ThrowIfCancellationRequested(); |
| 104 | + |
| 105 | + var data = await _session.LoadAsync<SerializedData>(definition.Scheme, cancellationToken).ConfigureAwait(false); |
| 106 | + _session.Delete(data); |
| 107 | + await _session.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| 108 | + |
| 109 | + _logger.LogInformation("Scheme {scheme} removed", definition.Scheme); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Updates a scheme definition asynchronous. |
| 114 | + /// </summary> |
| 115 | + /// <param name="definition">The definition.</param> |
| 116 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 117 | + /// <returns></returns> |
| 118 | + /// <exception cref="ArgumentNullException">definition</exception> |
| 119 | + public virtual async Task UpdateAsync(TSchemeDefinition definition, CancellationToken cancellationToken = default) |
| 120 | + { |
| 121 | + definition = definition ?? throw new ArgumentNullException(nameof(definition)); |
| 122 | + |
| 123 | + cancellationToken.ThrowIfCancellationRequested(); |
| 124 | + |
| 125 | + var serialized = Serialize(definition); |
| 126 | + |
| 127 | + var data = await _session.LoadAsync<SerializedData>(definition.Scheme, cancellationToken).ConfigureAwait(false); |
| 128 | + |
| 129 | + data.SerializedOptions = serialized.SerializedOptions; |
| 130 | + data.SerializedHandlerType = serialized.SerializedHandlerType; |
| 131 | + |
| 132 | + await _session.StoreAsync(data).ConfigureAwait(false); |
| 133 | + await _session.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| 134 | + |
| 135 | + _logger.LogInformation("Scheme {scheme} updated for {handlerType} with options: {options}", definition.Scheme, definition.HandlerType, data.SerializedOptions); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Finds scheme definition by scheme asynchronous. |
| 140 | + /// </summary> |
| 141 | + /// <param name="scheme">The scheme.</param> |
| 142 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 143 | + /// <returns> |
| 144 | + /// An instance of TSchemeDefinition or null. |
| 145 | + /// </returns> |
| 146 | + /// <exception cref="ArgumentException">Parameter {nameof(scheme)}</exception> |
| 147 | + public virtual async Task<TSchemeDefinition> FindBySchemeAsync(string scheme, CancellationToken cancellationToken = default) |
| 148 | + { |
| 149 | + CheckScheme(scheme); |
| 150 | + |
| 151 | + cancellationToken.ThrowIfCancellationRequested(); |
| 152 | + var data = await _session.LoadAsync<SerializedData>(scheme, cancellationToken).ConfigureAwait(false); |
| 153 | + |
| 154 | + if (data != null) |
| 155 | + { |
| 156 | + return Deserialize(data); |
| 157 | + } |
| 158 | + |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + private static void CheckScheme(string scheme) |
| 163 | + { |
| 164 | + if (string.IsNullOrWhiteSpace(scheme)) |
| 165 | + { |
| 166 | + throw new ArgumentException($"Parameter {nameof(scheme)} cannor be null or empty"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private SerializedData Serialize(TSchemeDefinition definition) |
| 171 | + { |
| 172 | + return new SerializedData |
| 173 | + { |
| 174 | + Id = definition.Scheme, |
| 175 | + SerializedHandlerType = _authenticationSchemeOptionsSerializer.SerializeType(definition.HandlerType), |
| 176 | + SerializedOptions = _authenticationSchemeOptionsSerializer.SerializeOptions(definition.Options, definition.HandlerType.GetAuthenticationSchemeOptionsType()) |
| 177 | + }; |
| 178 | + } |
| 179 | + |
| 180 | + private TSchemeDefinition Deserialize(SerializedData data) |
| 181 | + { |
| 182 | + var handlerType = _authenticationSchemeOptionsSerializer.DeserializeType(data.SerializedHandlerType); |
| 183 | + return new TSchemeDefinition |
| 184 | + { |
| 185 | + Scheme = data.Id, |
| 186 | + HandlerType = handlerType, |
| 187 | + Options = _authenticationSchemeOptionsSerializer.DeserializeOptions(data.SerializedOptions, handlerType.GetAuthenticationSchemeOptionsType()) |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + class SerializedData |
| 192 | + { |
| 193 | + public string Id { get; set; } |
| 194 | + |
| 195 | + public string SerializedOptions { get; set; } |
| 196 | + |
| 197 | + public string SerializedHandlerType { get; set; } |
| 198 | + |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments