|
1 | | -CredentialCache |
| 1 | +# ktsu.CredentialCache |
| 2 | + |
| 3 | +> A secure credential storage and management system for .NET applications. |
| 4 | +
|
| 5 | +[](https://github.com/ktsu-dev/CredentialCache/blob/main/LICENSE.md) |
| 6 | +[](https://www.nuget.org/packages/ktsu.CredentialCache/) |
| 7 | +[](https://www.nuget.org/packages/ktsu.CredentialCache/) |
| 8 | +[](https://github.com/ktsu-dev/CredentialCache/actions) |
| 9 | +[](https://github.com/ktsu-dev/CredentialCache/stargazers) |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +CredentialCache is a .NET library that provides a secure and convenient way to store, retrieve, and manage credentials in applications. It offers a flexible caching mechanism for various credential types while handling encryption, security, and persistence automatically. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Secure Storage**: Encrypts sensitive credentials using platform-specific security features |
| 18 | +- **Memory Caching**: Efficiently caches credentials in memory for quick access |
| 19 | +- **Multiple Credential Types**: Supports usernames/passwords, API keys, tokens, and certificates |
| 20 | +- **Automatic Expiration**: Time-based expiration for cached credentials |
| 21 | +- **Credential Rotation**: Support for credential rotation and refresh workflows |
| 22 | +- **Thread Safety**: Safe for concurrent access from multiple threads |
| 23 | +- **Extensible**: Easily extend with custom credential types and storage providers |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +### Package Manager Console |
| 28 | + |
| 29 | +```powershell |
| 30 | +Install-Package ktsu.CredentialCache |
| 31 | +``` |
| 32 | + |
| 33 | +### .NET CLI |
| 34 | + |
| 35 | +```bash |
| 36 | +dotnet add package ktsu.CredentialCache |
| 37 | +``` |
| 38 | + |
| 39 | +### Package Reference |
| 40 | + |
| 41 | +```xml |
| 42 | +<PackageReference Include="ktsu.CredentialCache" Version="x.y.z" /> |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage Examples |
| 46 | + |
| 47 | +### Basic Example |
| 48 | + |
| 49 | +```csharp |
| 50 | +using ktsu.CredentialCache; |
| 51 | + |
| 52 | +// Create a credential cache |
| 53 | +var cache = new CredentialCache(); |
| 54 | + |
| 55 | +// Store a credential |
| 56 | +var credential = new UsernamePasswordCredential |
| 57 | +{ |
| 58 | + Username = "user@example.com", |
| 59 | + Password = "securePassword123", |
| 60 | + Domain = "example.com" |
| 61 | +}; |
| 62 | + |
| 63 | +cache.Store("myAppLogin", credential); |
| 64 | + |
| 65 | +// Retrieve the credential later |
| 66 | +if (cache.TryGet("myAppLogin", out UsernamePasswordCredential retrievedCredential)) |
| 67 | +{ |
| 68 | + Console.WriteLine($"Retrieved username: {retrievedCredential.Username}"); |
| 69 | + // Use the credential for authentication |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Working with API Credentials |
| 74 | + |
| 75 | +```csharp |
| 76 | +// Store an API key |
| 77 | +var apiCredential = new ApiKeyCredential |
| 78 | +{ |
| 79 | + Key = "api_12345abcde", |
| 80 | + Secret = "apisecret_xyz789", |
| 81 | + Endpoint = "https://api.example.com" |
| 82 | +}; |
| 83 | + |
| 84 | +// Store with expiration |
| 85 | +cache.Store("apiAccess", apiCredential, TimeSpan.FromHours(1)); |
| 86 | + |
| 87 | +// Check if credential exists and is not expired |
| 88 | +if (cache.Contains("apiAccess")) |
| 89 | +{ |
| 90 | + var cred = cache.Get<ApiKeyCredential>("apiAccess"); |
| 91 | + // Use the API credential |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### Advanced Usage with Persistent Storage |
| 96 | + |
| 97 | +```csharp |
| 98 | +// Create a cache with persistent storage |
| 99 | +var options = new CredentialCacheOptions |
| 100 | +{ |
| 101 | + PersistToStorage = true, |
| 102 | + StoragePath = "credentials.dat", |
| 103 | + EncryptionLevel = EncryptionLevel.High |
| 104 | +}; |
| 105 | + |
| 106 | +var persistentCache = new CredentialCache(options); |
| 107 | + |
| 108 | +// Store a credential that will be saved to disk |
| 109 | +var oauthCredential = new OAuthCredential |
| 110 | +{ |
| 111 | + AccessToken = "access_token_123", |
| 112 | + RefreshToken = "refresh_token_456", |
| 113 | + ExpiresAt = DateTimeOffset.UtcNow.AddHours(1) |
| 114 | +}; |
| 115 | + |
| 116 | +persistentCache.Store("oauth", oauthCredential); |
| 117 | + |
| 118 | +// Later, even after application restart: |
| 119 | +var loadedCache = new CredentialCache(options); |
| 120 | +if (loadedCache.TryGet("oauth", out OAuthCredential oauth)) |
| 121 | +{ |
| 122 | + if (oauth.IsExpired) |
| 123 | + { |
| 124 | + // Refresh the token |
| 125 | + oauth = RefreshOAuthToken(oauth); |
| 126 | + loadedCache.Store("oauth", oauth); |
| 127 | + } |
| 128 | + |
| 129 | + // Use the OAuth token |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## API Reference |
| 134 | + |
| 135 | +### `CredentialCache` Class |
| 136 | + |
| 137 | +The main class for storing and retrieving credentials. |
| 138 | + |
| 139 | +#### Properties |
| 140 | + |
| 141 | +| Name | Type | Description | |
| 142 | +|------|------|-------------| |
| 143 | +| `Count` | `int` | Number of credentials in the cache | |
| 144 | +| `Options` | `CredentialCacheOptions` | Configuration options for this cache instance | |
| 145 | + |
| 146 | +#### Methods |
| 147 | + |
| 148 | +| Name | Return Type | Description | |
| 149 | +|------|-------------|-------------| |
| 150 | +| `Store(string key, ICredential credential, TimeSpan? expiration = null)` | `void` | Stores a credential with optional expiration | |
| 151 | +| `Get<T>(string key) where T : ICredential` | `T` | Gets a credential by key (throws if not found) | |
| 152 | +| `TryGet<T>(string key, out T credential) where T : ICredential` | `bool` | Tries to get a credential by key | |
| 153 | +| `Remove(string key)` | `bool` | Removes a credential from the cache | |
| 154 | +| `Contains(string key)` | `bool` | Checks if a credential exists and is not expired | |
| 155 | +| `Clear()` | `void` | Removes all credentials from the cache | |
| 156 | + |
| 157 | +### Credential Types |
| 158 | + |
| 159 | +| Type | Description | |
| 160 | +|------|-------------| |
| 161 | +| `UsernamePasswordCredential` | Standard username and password combination | |
| 162 | +| `ApiKeyCredential` | API key and optional secret | |
| 163 | +| `OAuthCredential` | OAuth access and refresh tokens | |
| 164 | +| `CertificateCredential` | Certificate-based authentication | |
| 165 | + |
| 166 | +## Contributing |
| 167 | + |
| 168 | +Contributions are welcome! Here's how you can help: |
| 169 | + |
| 170 | +1. Fork the repository |
| 171 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 172 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 173 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 174 | +5. Open a Pull Request |
| 175 | + |
| 176 | +Please ensure your code follows security best practices when dealing with sensitive credential information. |
| 177 | + |
| 178 | +## License |
| 179 | + |
| 180 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
0 commit comments