From 3915e020cf831f2a30fc0bc6cdef68731383e4cb Mon Sep 17 00:00:00 2001 From: Bryan Aldrich Date: Tue, 3 Dec 2024 13:12:15 -0500 Subject: [PATCH 1/5] quick edit to allow tokens to refresh if they specify an expires_in and created fields. Also supports refresh_token if that is passed. --- .../csharp/auth/OAuthAuthenticator.mustache | 56 +++++++++++++------ .../csharp/auth/TokenResponse.mustache | 9 +++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache b/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache index d71f262a810f..2e5a05212819 100644 --- a/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache @@ -11,8 +11,22 @@ namespace {{packageName}}.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse{{nrt?}} _token; + + public string{{nrt?}} Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -31,7 +45,7 @@ namespace {{packageName}}.Client.Auth string{{nrt?}} scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -64,10 +78,10 @@ namespace {{packageName}}.Client.Auth /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -76,31 +90,39 @@ namespace {{packageName}}.Client.Auth /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache index f118b97a9dd5..443479677a9f 100644 --- a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache @@ -10,5 +10,14 @@ namespace {{packageName}}.Client.Auth public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string{{nrt?}} RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file From c37a5e771f5e830cd047b44879facfcf37ce04db Mon Sep 17 00:00:00 2001 From: Bryan Aldrich Date: Tue, 3 Dec 2024 19:52:02 -0500 Subject: [PATCH 2/5] update samples --- .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ .../Client/Auth/OAuthAuthenticator.cs | 56 +++++++++++++------ .../Client/Auth/TokenResponse.cs | 9 +++ 14 files changed, 336 insertions(+), 119 deletions(-) diff --git a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index 516b93d2f6aa..9e2c72fa2619 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse _token; + + public string Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 21fd27c376fa..a207ffbfe1c5 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index d7fb1293b34b..c956b85c618d 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse _token; + + public string Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..e91974b99a22 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index d7fb1293b34b..c956b85c618d 100644 --- a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse _token; + + public string Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..e91974b99a22 100644 --- a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index ce6261390c32..7685a3763bb9 100644 --- a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse? _token; + + public string? Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string? scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..4f525b9940e4 100644 --- a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string? RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index ce6261390c32..7685a3763bb9 100644 --- a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse? _token; + + public string? Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string? scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..4f525b9940e4 100644 --- a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string? RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index d7fb1293b34b..c956b85c618d 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse _token; + + public string Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..e91974b99a22 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index d7fb1293b34b..c956b85c618d 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -19,8 +19,22 @@ namespace Org.OpenAPITools.Client.Auth /// /// An authenticator for OAuth2 authentication flows /// - public class OAuthAuthenticator : AuthenticatorBase + public class OAuthAuthenticator : IAuthenticator { + private TokenResponse _token; + + public string Token + { + get + { + if (_token == null) return null; + if (_token.ExpiresIn == null) return _token.AccessToken; + if (_token.ExpiresAt < DateTime.Now) return null; + + return _token.AccessToken; + } + } + readonly string _tokenUrl; readonly string _clientId; readonly string _clientSecret; @@ -39,7 +53,7 @@ public OAuthAuthenticator( string scope, OAuthFlow? flow, JsonSerializerSettings serializerSettings, - IReadableConfiguration configuration) : base("") + IReadableConfiguration configuration) { _tokenUrl = tokenUrl; _clientId = clientId; @@ -72,10 +86,10 @@ public OAuthAuthenticator( /// /// Access token to create a parameter from. /// An authentication parameter. - protected override async ValueTask GetAuthenticationParameter(string accessToken) + protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token); + return new HeaderParameter(KnownHeaders.Authorization, token!); } /// @@ -84,31 +98,39 @@ protected override async ValueTask GetAuthenticationParameter(string /// An authentication token. async Task GetToken() { - var client = new RestClient(_tokenUrl, - configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); - - var request = new RestRequest() - .AddParameter("grant_type", _grantType) - .AddParameter("client_id", _clientId) - .AddParameter("client_secret", _clientSecret); + var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration))); + var request = new RestRequest(); + if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) + { + request.AddParameter("grant_type", "refresh_token") + .AddParameter("refresh_token", _token!.RefreshToken); + } + else + { + request + .AddParameter("grant_type", _grantType) + .AddParameter("client_id", _clientId) + .AddParameter("client_secret", _clientSecret); + } if (!string.IsNullOrEmpty(_scope)) { request.AddParameter("scope", _scope); } - - var response = await client.PostAsync(request).ConfigureAwait(false); - + _token = await client.PostAsync(request).ConfigureAwait(false); // RFC6749 - token_type is case insensitive. // RFC6750 - In Authorization header Bearer should be capitalized. // Fix the capitalization irrespective of token_type casing. - switch (response.TokenType?.ToLower()) + switch (_token?.TokenType?.ToLower()) { case "bearer": - return $"Bearer {response.AccessToken}"; + return $"Bearer {_token.AccessToken}"; default: - return $"{response.TokenType} {response.AccessToken}"; + return $"{_token?.TokenType} {_token?.AccessToken}"; } } + + public async ValueTask Authenticate(IRestClient client, RestRequest request) + => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 0a94e17e1228..e91974b99a22 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -18,5 +18,14 @@ class TokenResponse public string TokenType { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } + [JsonProperty("expires_in")] + public int? ExpiresIn { get; set; } + [JsonProperty("created")] + public DateTime Created { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file From b582764d99802e9e2bbbd5917fb0a6e9ea31e70c Mon Sep 17 00:00:00 2001 From: Bryan Aldrich Date: Wed, 4 Dec 2024 10:21:43 -0500 Subject: [PATCH 3/5] fix compile error for projects without default using statement. make the created date nullable. --- .../src/main/resources/csharp/auth/TokenResponse.mustache | 3 ++- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 3 ++- 8 files changed, 16 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache index 443479677a9f..becd82d87a15 100644 --- a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache @@ -1,5 +1,6 @@ {{>partial_header}} +using System; using Newtonsoft.Json; namespace {{packageName}}.Client.Auth @@ -13,7 +14,7 @@ namespace {{packageName}}.Client.Auth [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string{{nrt?}} RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index a207ffbfe1c5..ab1cf58090d7 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index e91974b99a22..d3703f73dd77 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index e91974b99a22..d3703f73dd77 100644 --- a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 4f525b9940e4..9b9fc94bbf86 100644 --- a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string? RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 4f525b9940e4..9b9fc94bbf86 100644 --- a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string? RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index e91974b99a22..d3703f73dd77 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index e91974b99a22..d3703f73dd77 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -8,6 +8,7 @@ */ +using System; using Newtonsoft.Json; namespace Org.OpenAPITools.Client.Auth @@ -21,7 +22,7 @@ class TokenResponse [JsonProperty("expires_in")] public int? ExpiresIn { get; set; } [JsonProperty("created")] - public DateTime Created { get; set; } + public DateTime? Created { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } From ee15594da701301734d1a9f2f656f5b0766cb274 Mon Sep 17 00:00:00 2001 From: Bryan Aldrich Date: Wed, 4 Dec 2024 13:37:18 -0500 Subject: [PATCH 4/5] fix nullable de-reference on Created. --- .../src/main/resources/csharp/auth/TokenResponse.mustache | 2 +- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- .../Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache index becd82d87a15..7a72e04c12c2 100644 --- a/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/auth/TokenResponse.mustache @@ -19,6 +19,6 @@ namespace {{packageName}}.Client.Auth [JsonProperty("refresh_token")] public string{{nrt?}} RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index ab1cf58090d7..5ff4349979fd 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index d3703f73dd77..95e03c36e105 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index d3703f73dd77..95e03c36e105 100644 --- a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 9b9fc94bbf86..da4f4499b1cc 100644 --- a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string? RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index 9b9fc94bbf86..da4f4499b1cc 100644 --- a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string? RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index d3703f73dd77..95e03c36e105 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs index d3703f73dd77..95e03c36e105 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/TokenResponse.cs @@ -27,6 +27,6 @@ class TokenResponse [JsonProperty("refresh_token")] public string RefreshToken { get; set; } - public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value); + public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value); } } \ No newline at end of file From 12e29e4eeb0c4bba1dad600f07de38a1d8b2efec Mon Sep 17 00:00:00 2001 From: Bryan Aldrich Date: Fri, 6 Dec 2024 12:26:00 -0500 Subject: [PATCH 5/5] fix to remove nullable reference types (the ! to remove the warning) and add documentation comments to the public members. --- .../csharp/auth/OAuthAuthenticator.mustache | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- .../Client/Auth/OAuthAuthenticator.cs | 14 +++++++++++--- 8 files changed, 88 insertions(+), 24 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache b/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache index 2e5a05212819..c336c8ad5ccf 100644 --- a/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache @@ -15,6 +15,9 @@ namespace {{packageName}}.Client.Auth { private TokenResponse{{nrt?}} _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string{{nrt?}} Token { get @@ -76,12 +79,11 @@ namespace {{packageName}}.Client.Auth /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -96,7 +98,7 @@ namespace {{packageName}}.Client.Auth if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -122,6 +124,12 @@ namespace {{packageName}}.Client.Auth } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index 9e2c72fa2619..e731f1c71bea 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index c956b85c618d..6c6817ef501a 100644 --- a/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index c956b85c618d..6c6817ef501a 100644 --- a/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net4.8/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index 7685a3763bb9..bcccf1e83f6f 100644 --- a/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net7/EnumMappings/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse? _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string? Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index 7685a3763bb9..bcccf1e83f6f 100644 --- a/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/net7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse? _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string? Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index c956b85c618d..6c6817ef501a 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/ConditionalSerialization/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); } diff --git a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs index c956b85c618d..6c6817ef501a 100644 --- a/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs +++ b/samples/client/petstore/csharp/restsharp/standard2.0/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs @@ -23,6 +23,9 @@ public class OAuthAuthenticator : IAuthenticator { private TokenResponse _token; + /// + /// Returns the current authentication token. Can return null if there is no authentication token, or it has expired. + /// public string Token { get @@ -84,12 +87,11 @@ public OAuthAuthenticator( /// /// Creates an authentication parameter from an access token. /// - /// Access token to create a parameter from. /// An authentication parameter. protected async ValueTask GetAuthenticationParameter() { var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token; - return new HeaderParameter(KnownHeaders.Authorization, token!); + return new HeaderParameter(KnownHeaders.Authorization, token); } /// @@ -104,7 +106,7 @@ async Task GetToken() if (!string.IsNullOrWhiteSpace(_token?.RefreshToken)) { request.AddParameter("grant_type", "refresh_token") - .AddParameter("refresh_token", _token!.RefreshToken); + .AddParameter("refresh_token", _token.RefreshToken); } else { @@ -130,6 +132,12 @@ async Task GetToken() } } + /// + /// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request + /// + /// + /// + /// public async ValueTask Authenticate(IRestClient client, RestRequest request) => request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false)); }