Skip to content

Commit 362d3b5

Browse files
wing328mjansrud
andauthored
[csharp] Add scope for oauth2 (#19234)
* feat: add scope for oauth2 * fix: add parameter AlwaysMultipartFormData * fix: string to boolean * fix: optional bool * fix: null checks * Optional string * Remove all references to OAuthMultipartFormData * Remove _multipartFormData = multipartFormData; * Remove typo * Run generate-samples and export_docs_generators * Revert "Run generate-samples and export_docs_generators" This reverts commit f051f26. * Switch to string.IsNullOrEmpty, add langVersion 8 * Add langVersion 8 in ConditionalSerialization * Use regular strings for netstandard2.0 * Remove references to langVersion 8 * Fix variable * Use template engine to toggle nullable string * Trigger tests * Generate samples * Trigger build * Use {{nrt?}} * update samples --------- Co-authored-by: Morten Jansrud <[email protected]>
1 parent 9a673ea commit 362d3b5

File tree

32 files changed

+184
-0
lines changed

32 files changed

+184
-0
lines changed

modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ namespace {{packageName}}.Client
474474
configuration.OAuthTokenUrl,
475475
configuration.OAuthClientId,
476476
configuration.OAuthClientSecret,
477+
configuration.OAuthScope,
477478
configuration.OAuthFlow,
478479
SerializerSettings,
479480
configuration);

modules/openapi-generator/src/main/resources/csharp/Configuration.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ namespace {{packageName}}.Client
366366
/// <value>The OAuth Client Secret.</value>
367367
public virtual string OAuthClientSecret { get; set; }
368368

369+
/// <summary>
370+
/// Gets or sets the client scope for OAuth2 authentication.
371+
/// </summary>
372+
/// <value>The OAuth Client Scope.</value>
373+
public virtual string{{nrt?}} OAuthScope { get; set; }
374+
369375
/// <summary>
370376
/// Gets or sets the flow for OAuth2 authentication.
371377
/// </summary>
@@ -711,6 +717,7 @@ namespace {{packageName}}.Client
711717
OAuthTokenUrl = second.OAuthTokenUrl ?? first.OAuthTokenUrl,
712718
OAuthClientId = second.OAuthClientId ?? first.OAuthClientId,
713719
OAuthClientSecret = second.OAuthClientSecret ?? first.OAuthClientSecret,
720+
OAuthScope = second.OAuthScope ?? first.OAuthScope,
714721
OAuthFlow = second.OAuthFlow ?? first.OAuthFlow,
715722
{{/hasOAuthMethods}}
716723
{{/useRestSharp}}

modules/openapi-generator/src/main/resources/csharp/IReadableConfiguration.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ namespace {{packageName}}.Client
4343
/// <value>OAuth Client Secret.</value>
4444
string OAuthClientSecret { get; }
4545

46+
/// <summary>
47+
/// Gets the OAuth token scope.
48+
/// </summary>
49+
/// <value>OAuth Token scope.</value>
50+
string{{nrt?}} OAuthScope { get; }
51+
4652
/// <summary>
4753
/// Gets the OAuth flow.
4854
/// </summary>

modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace {{packageName}}.Client.Auth
1616
readonly string _tokenUrl;
1717
readonly string _clientId;
1818
readonly string _clientSecret;
19+
readonly string{{nrt?}} _scope;
1920
readonly string _grantType;
2021
readonly JsonSerializerSettings _serializerSettings;
2122
readonly IReadableConfiguration _configuration;
@@ -27,13 +28,15 @@ namespace {{packageName}}.Client.Auth
2728
string tokenUrl,
2829
string clientId,
2930
string clientSecret,
31+
string{{nrt?}} scope,
3032
OAuthFlow? flow,
3133
JsonSerializerSettings serializerSettings,
3234
IReadableConfiguration configuration) : base("")
3335
{
3436
_tokenUrl = tokenUrl;
3537
_clientId = clientId;
3638
_clientSecret = clientSecret;
39+
_scope = scope;
3740
_serializerSettings = serializerSettings;
3841
_configuration = configuration;
3942
@@ -80,6 +83,12 @@ namespace {{packageName}}.Client.Auth
8083
.AddParameter("grant_type", _grantType)
8184
.AddParameter("client_id", _clientId)
8285
.AddParameter("client_secret", _clientSecret);
86+
87+
if (!string.IsNullOrEmpty(_scope))
88+
{
89+
request.AddParameter("scope", _scope);
90+
}
91+
8392
var response = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
8493

8594
// RFC6749 - token_type is case insensitive.

samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ private ApiResponse<T> ExecClient<T>(Func<RestClient, RestResponse<T>> getRespon
474474
configuration.OAuthTokenUrl,
475475
configuration.OAuthClientId,
476476
configuration.OAuthClientSecret,
477+
configuration.OAuthScope,
477478
configuration.OAuthFlow,
478479
SerializerSettings,
479480
configuration);

samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class OAuthAuthenticator : AuthenticatorBase
2424
readonly string _tokenUrl;
2525
readonly string _clientId;
2626
readonly string _clientSecret;
27+
readonly string _scope;
2728
readonly string _grantType;
2829
readonly JsonSerializerSettings _serializerSettings;
2930
readonly IReadableConfiguration _configuration;
@@ -35,13 +36,15 @@ public OAuthAuthenticator(
3536
string tokenUrl,
3637
string clientId,
3738
string clientSecret,
39+
string scope,
3840
OAuthFlow? flow,
3941
JsonSerializerSettings serializerSettings,
4042
IReadableConfiguration configuration) : base("")
4143
{
4244
_tokenUrl = tokenUrl;
4345
_clientId = clientId;
4446
_clientSecret = clientSecret;
47+
_scope = scope;
4548
_serializerSettings = serializerSettings;
4649
_configuration = configuration;
4750

@@ -88,6 +91,12 @@ async Task<string> GetToken()
8891
.AddParameter("grant_type", _grantType)
8992
.AddParameter("client_id", _clientId)
9093
.AddParameter("client_secret", _clientSecret);
94+
95+
if (!string.IsNullOrEmpty(_scope))
96+
{
97+
request.AddParameter("scope", _scope);
98+
}
99+
91100
var response = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
92101

93102
// RFC6749 - token_type is case insensitive.

samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/Configuration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ public string GetApiKeyWithPrefix(string apiKeyIdentifier)
294294
/// <value>The OAuth Client Secret.</value>
295295
public virtual string OAuthClientSecret { get; set; }
296296

297+
/// <summary>
298+
/// Gets or sets the client scope for OAuth2 authentication.
299+
/// </summary>
300+
/// <value>The OAuth Client Scope.</value>
301+
public virtual string OAuthScope { get; set; }
302+
297303
/// <summary>
298304
/// Gets or sets the flow for OAuth2 authentication.
299305
/// </summary>
@@ -622,6 +628,7 @@ public static IReadableConfiguration MergeConfigurations(IReadableConfiguration
622628
OAuthTokenUrl = second.OAuthTokenUrl ?? first.OAuthTokenUrl,
623629
OAuthClientId = second.OAuthClientId ?? first.OAuthClientId,
624630
OAuthClientSecret = second.OAuthClientSecret ?? first.OAuthClientSecret,
631+
OAuthScope = second.OAuthScope ?? first.OAuthScope,
625632
OAuthFlow = second.OAuthFlow ?? first.OAuthFlow,
626633
TempFolderPath = second.TempFolderPath ?? first.TempFolderPath,
627634
DateTimeFormat = second.DateTimeFormat ?? first.DateTimeFormat,

samples/client/petstore/csharp/restsharp/net4.7/MultipleFrameworks/src/Org.OpenAPITools/Client/IReadableConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public interface IReadableConfiguration
4646
/// <value>OAuth Client Secret.</value>
4747
string OAuthClientSecret { get; }
4848

49+
/// <summary>
50+
/// Gets the OAuth token scope.
51+
/// </summary>
52+
/// <value>OAuth Token scope.</value>
53+
string OAuthScope { get; }
54+
4955
/// <summary>
5056
/// Gets the OAuth flow.
5157
/// </summary>

samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ private ApiResponse<T> ExecClient<T>(Func<RestClient, RestResponse<T>> getRespon
474474
configuration.OAuthTokenUrl,
475475
configuration.OAuthClientId,
476476
configuration.OAuthClientSecret,
477+
configuration.OAuthScope,
477478
configuration.OAuthFlow,
478479
SerializerSettings,
479480
configuration);

samples/client/petstore/csharp/restsharp/net4.7/Petstore/src/Org.OpenAPITools/Client/Auth/OAuthAuthenticator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class OAuthAuthenticator : AuthenticatorBase
2424
readonly string _tokenUrl;
2525
readonly string _clientId;
2626
readonly string _clientSecret;
27+
readonly string _scope;
2728
readonly string _grantType;
2829
readonly JsonSerializerSettings _serializerSettings;
2930
readonly IReadableConfiguration _configuration;
@@ -35,13 +36,15 @@ public OAuthAuthenticator(
3536
string tokenUrl,
3637
string clientId,
3738
string clientSecret,
39+
string scope,
3840
OAuthFlow? flow,
3941
JsonSerializerSettings serializerSettings,
4042
IReadableConfiguration configuration) : base("")
4143
{
4244
_tokenUrl = tokenUrl;
4345
_clientId = clientId;
4446
_clientSecret = clientSecret;
47+
_scope = scope;
4548
_serializerSettings = serializerSettings;
4649
_configuration = configuration;
4750

@@ -88,6 +91,12 @@ async Task<string> GetToken()
8891
.AddParameter("grant_type", _grantType)
8992
.AddParameter("client_id", _clientId)
9093
.AddParameter("client_secret", _clientSecret);
94+
95+
if (!string.IsNullOrEmpty(_scope))
96+
{
97+
request.AddParameter("scope", _scope);
98+
}
99+
91100
var response = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
92101

93102
// RFC6749 - token_type is case insensitive.

0 commit comments

Comments
 (0)