Skip to content

Commit

Permalink
Api keys move into auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfil committed Jun 28, 2024
1 parent 13b9312 commit 31b2b22
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
19 changes: 11 additions & 8 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ You can also use the `ApiKeys` array in order to manage multiple API keys for mu

```json
{
"ApiKeys": [
{
"Key" : "NUGET-SERVER-API-KEY-1"
},
{
"Key" : "NUGET-SERVER-API-KEY-2"
}
]
"Authentication": {
"ApiKeys": [
{
"Key" : "NUGET-SERVER-API-KEY-1"
},
{
"Key" : "NUGET-SERVER-API-KEY-2"
}
]
...
}
...
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ApiKeyAuthenticationService(IOptionsSnapshot<BaGetterOptions> options)
ArgumentNullException.ThrowIfNull(options);

_apiKey = string.IsNullOrEmpty(options.Value.ApiKey) ? null : options.Value.ApiKey;
_apiKeys = options.Value.ApiKeys;
_apiKeys = options.Value.Authentication?.ApiKeys ?? [];
}

public Task<bool> AuthenticateAsync(string apiKey, CancellationToken cancellationToken)
Expand All @@ -26,8 +26,8 @@ public Task<bool> AuthenticateAsync(string apiKey, CancellationToken cancellatio
private bool Authenticate(string apiKey)
{
// No authentication is necessary if there is no required API key.
if (_apiKey == null && (_apiKeys is null || _apiKeys.Length==0)) return true;
if (_apiKey == null && (_apiKeys.Length==0)) return true;

return _apiKey == apiKey || _apiKeys?.Any(x=> x.Key.Equals(apiKey)) == true;
return _apiKey == apiKey || _apiKeys.Any(x=> x.Key.Equals(apiKey));
}
}
6 changes: 0 additions & 6 deletions src/BaGetter.Core/Configuration/BaGetterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ public class BaGetterOptions
/// </summary>
public string ApiKey { get; set; }

/// <summary>
/// The API Keys required to authenticate package
/// operations. If <see cref="ApiKeys"/> and <see cref="ApiKey"/> are not set, package operations do not require authentication.
/// </summary>
public ApiKey[] ApiKeys { get; set; }

/// <summary>
/// The application root URL for usage in reverse proxy scenarios.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/BaGetter.Core/Configuration/NugetAuthenticationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using BaGetter.Core.Configuration;

namespace BaGetter.Core;

public sealed class NugetAuthenticationOptions
{
public NugetCredentials[] Credentials { get; set; }

public ApiKey[] ApiKeys { get; set; }
}
11 changes: 6 additions & 5 deletions src/BaGetter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@
// "Username": "username",
// "Password": "password"
// }
// ],
// "ApiKeys": [
// {
// "Key": "key"
// }
// ]
//}

//"ApiKeys": [
// {
// "Key": "key"
// }
//]

}

0 comments on commit 31b2b22

Please sign in to comment.