diff --git a/dotnet/examples/VaasExample/Program.cs b/dotnet/examples/VaasExample/Program.cs index 72ddc63e..669d3186 100644 --- a/dotnet/examples/VaasExample/Program.cs +++ b/dotnet/examples/VaasExample/Program.cs @@ -6,18 +6,9 @@ namespace VaasExample; public static class Program { - private static string ClientId => Environment.GetEnvironmentVariable("CLIENT_ID") ?? string.Empty; - private static string ClientIdForResourceOwnerPasswordGrant => Environment.GetEnvironmentVariable("VAAS_CLIENT_ID") ?? "vaas-customer"; - private static string ClientSecret => Environment.GetEnvironmentVariable("CLIENT_SECRET") ?? string.Empty; - private static string UserName => Environment.GetEnvironmentVariable("VAAS_USER_NAME") ?? string.Empty; - private static string Password => Environment.GetEnvironmentVariable("VAAS_PASSWORD") ?? string.Empty; - private static Uri VaasUrl => new(Environment.GetEnvironmentVariable("VAAS_URL") ?? - "wss://gateway.production.vaas.gdatasecurity.de"); - private static Uri TokenUrl => new(Environment.GetEnvironmentVariable("TOKEN_URL") ?? - "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"); - public static async Task Main(string[] args) { + DotNetEnv.Env.TraversePath().Load(); if (args.Contains("UrlScan")) await UrlScan(); if (args.Contains("FileScan")) @@ -42,7 +33,7 @@ private static async Task HashsumScan() Console.WriteLine($"{verdict.Sha256} is detected as {verdict.Verdict}"); } - + private static async Task UrlScan() { var vaas = CreateVaas(); @@ -55,27 +46,52 @@ private static async Task UrlScan() private static IVaas CreateVaas() { - // If you got a username and password from us, you can use the GrantType.Password like this - // You may use self registration and create a new username and password for the - // Credentials by yourself like the example above on https://vaas.gdata.de/login - var authenticator = new ResourceOwnerPasswordGrantAuthenticator( - ClientIdForResourceOwnerPasswordGrant, - UserName, - Password, - TokenUrl - ); - // // Else if you got a client id and client secret from us, you should use the GrantType.ClientCredentials like this - // var authenticator = new ClientCredentialsGrantAuthenticator( - // ClientId, - // ClientSecret, - // TokenUrl - // ); + var clientId = Environment.GetEnvironmentVariable("CLIENT_ID"); + var clientIdForResourceOwnerPasswordGrant = Environment.GetEnvironmentVariable("VAAS_CLIENT_ID") ?? "vaas-customer"; + var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET"); + var userName = Environment.GetEnvironmentVariable("VAAS_USER_NAME"); + var password = Environment.GetEnvironmentVariable("VAAS_PASSWORD"); + var vaasUrl = new Uri(Environment.GetEnvironmentVariable("VAAS_URL") ?? + "https://gateway.production.vaas.gdatasecurity.de"); + var tokenUrl = new Uri(Environment.GetEnvironmentVariable("TOKEN_URL") ?? + "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"); + + IAuthenticator authenticator; + if (userName is not null) + { + if (password is null) + throw new ArgumentNullException("VAAS_PASSWORD", "Password must be set when using Resource Owner Password Grant"); + // If you got a username and password from us, you can use the GrantType.Password like this + // You may use self registration and create a new username and password for the + // Credentials by yourself like the example above on https://vaas.gdata.de/login + authenticator = new ResourceOwnerPasswordGrantAuthenticator( + clientIdForResourceOwnerPasswordGrant, + userName, + password, + tokenUrl + ); + + } + else + { + if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret)) + { + throw new ArgumentNullException("CLIENT_ID or CLIENT_SECRET", "CLIENT_ID and CLIENT_SECRET must be set when using Client Credentials Grant"); + } + + // Else if you got a client id and client secret from us, you should use the GrantType.ClientCredentials like this + authenticator = new ClientCredentialsGrantAuthenticator( + clientId, + clientSecret, + tokenUrl + ); + } var options = new VaasOptions { UseCache = true, UseHashLookup = true, - VaasUrl = VaasUrl, + VaasUrl = vaasUrl, Timeout = TimeSpan.FromSeconds(300) }; diff --git a/dotnet/examples/VaasExample/VaasExample.csproj b/dotnet/examples/VaasExample/VaasExample.csproj index f1f03b65..f32391a9 100644 --- a/dotnet/examples/VaasExample/VaasExample.csproj +++ b/dotnet/examples/VaasExample/VaasExample.csproj @@ -2,14 +2,15 @@ Exe - net8.0 + net10.0 enable enable Readme.md - + +