|
| 1 | +using G4.Api.Abstractions; |
| 2 | +using G4.Cache; |
| 3 | +using G4.Credentials; |
| 4 | +using G4.Credentials.Models; |
| 5 | + |
| 6 | +using Microsoft.Data.Sqlite; |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | + |
| 12 | +namespace G4.Api.Clients |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Provides credential persistence and caching capabilities for OAuth and related credential types. |
| 16 | + /// </summary> |
| 17 | + internal class CredentialsClient : ClientBase, ICredentialsClient |
| 18 | + { |
| 19 | + #region *** Fields *** |
| 20 | + // Manages caching mechanisms for plugins. |
| 21 | + private readonly CacheManager _cache; |
| 22 | + |
| 23 | + // Manages credentials for external integrations. |
| 24 | + private readonly CredentialsManager _credentials; |
| 25 | + #endregion |
| 26 | + |
| 27 | + #region *** Constructors *** |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="CredentialsClient"/> class |
| 30 | + /// using the shared SQLite connection from <see cref="CacheManager"/>. |
| 31 | + /// </summary> |
| 32 | + public CredentialsClient() |
| 33 | + : this(CacheManager.SqliteConnection) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of the <see cref="CredentialsClient"/> class |
| 39 | + /// with an explicit <see cref="SqliteConnection"/>. |
| 40 | + /// </summary> |
| 41 | + /// <param name="sqliteConnection">The SQLite connection used by the underlying <see cref="CredentialsManager"/>.</param> |
| 42 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="sqliteConnection"/> is null.</exception> |
| 43 | + public CredentialsClient(SqliteConnection sqliteConnection) |
| 44 | + { |
| 45 | + // Validate that the provided SQLite connection is not null. |
| 46 | + ArgumentNullException.ThrowIfNull(sqliteConnection); |
| 47 | + |
| 48 | + // Obtain shared in-memory cache instance |
| 49 | + _cache = CacheManager.Instance; |
| 50 | + |
| 51 | + // Initialize credentials manager with provided SQLite connection |
| 52 | + _credentials = new CredentialsManager(sqliteConnection); |
| 53 | + } |
| 54 | + #endregion |
| 55 | + |
| 56 | + #region *** Methods *** |
| 57 | + /// <inheritdoc /> |
| 58 | + public OAuthCredentialModel GetCredentials(string idOrName) |
| 59 | + { |
| 60 | + // Get credentials from the underlying credentials database manager. |
| 61 | + // This will not use the cache, as credentials may be updated or created outside of this client. |
| 62 | + return _credentials.GetCredentials(idOrName); |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc /> |
| 66 | + public IDictionary<string, OAuthCredentialModel> GetCredentials() |
| 67 | + { |
| 68 | + // Get all credentials from the underlying credentials database manager. |
| 69 | + // This will not use the cache, as credentials may be updated or created outside of this client. |
| 70 | + return _credentials |
| 71 | + .GetCredentials() |
| 72 | + .ToDictionary(i => ($"{i.Name};{i.Id}".ToLower()), i => i); |
| 73 | + } |
| 74 | + |
| 75 | + /// <inheritdoc /> |
| 76 | + public OAuthCredentialsResponseModel NewCredentials(OAuthCredentialModel oauth) |
| 77 | + { |
| 78 | + // Create new credentials using the underlying credentials manager. |
| 79 | + return _credentials.NewCredentials(oauth); |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc /> |
| 83 | + public int RemoveCredentials(string idOrName) |
| 84 | + { |
| 85 | + // Resolve the credential so we can compute the cache key |
| 86 | + var credentials = _credentials.GetCredentials(idOrName); |
| 87 | + |
| 88 | + // No credential found matching the provided identifier or name, so return 0 records removed. |
| 89 | + if (credentials == null) { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + |
| 93 | + // Remove from persistent store |
| 94 | + var removedCount = _credentials.RemoveCredentials(idOrName: credentials.Id); |
| 95 | + |
| 96 | + // Build normalized cache key (Name + Id, case-insensitive) |
| 97 | + // Using invariant lowercase to ensure consistent dictionary access |
| 98 | + var key = $"{credentials.Name};{credentials.Id}".ToLowerInvariant(); |
| 99 | + |
| 100 | + // Remove from in-memory cache if it exists |
| 101 | + _cache.CredentialsCache.Remove(key); |
| 102 | + |
| 103 | + // Return the number of records removed from the persistent store |
| 104 | + return removedCount; |
| 105 | + } |
| 106 | + |
| 107 | + /// <inheritdoc /> |
| 108 | + public OAuthCredentialModel SaveCredentials(OAuthCredentialModel oauth) |
| 109 | + { |
| 110 | + // Persist credentials using the underlying storage provider |
| 111 | + var credentials = _credentials.SaveCredentials(oauth) |
| 112 | + ?? throw new InvalidOperationException("Failed to persist OAuth credentials."); |
| 113 | + |
| 114 | + // Build normalized cache key (Name + Id, case-insensitive) |
| 115 | + // Using invariant lowercase to ensure consistent dictionary access |
| 116 | + var key = $"{credentials.Name};{credentials.Id}".ToLowerInvariant(); |
| 117 | + |
| 118 | + // Store in in-memory cache for fast lookup |
| 119 | + _cache.CredentialsCache[key] = credentials; |
| 120 | + |
| 121 | + // Return the persisted credentials to the caller |
| 122 | + return credentials; |
| 123 | + } |
| 124 | + #endregion |
| 125 | + } |
| 126 | +} |
0 commit comments