Skip to content

Commit 3915e02

Browse files
committed
quick edit to allow tokens to refresh if they specify an expires_in and created fields. Also supports refresh_token if that is passed.
1 parent 423ba67 commit 3915e02

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@ namespace {{packageName}}.Client.Auth
1111
/// <summary>
1212
/// An authenticator for OAuth2 authentication flows
1313
/// </summary>
14-
public class OAuthAuthenticator : AuthenticatorBase
14+
public class OAuthAuthenticator : IAuthenticator
1515
{
16+
private TokenResponse{{nrt?}} _token;
17+
18+
public string{{nrt?}} Token
19+
{
20+
get
21+
{
22+
if (_token == null) return null;
23+
if (_token.ExpiresIn == null) return _token.AccessToken;
24+
if (_token.ExpiresAt < DateTime.Now) return null;
25+
26+
return _token.AccessToken;
27+
}
28+
}
29+
1630
readonly string _tokenUrl;
1731
readonly string _clientId;
1832
readonly string _clientSecret;
@@ -31,7 +45,7 @@ namespace {{packageName}}.Client.Auth
3145
string{{nrt?}} scope,
3246
OAuthFlow? flow,
3347
JsonSerializerSettings serializerSettings,
34-
IReadableConfiguration configuration) : base("")
48+
IReadableConfiguration configuration)
3549
{
3650
_tokenUrl = tokenUrl;
3751
_clientId = clientId;
@@ -64,10 +78,10 @@ namespace {{packageName}}.Client.Auth
6478
/// </summary>
6579
/// <param name="accessToken">Access token to create a parameter from.</param>
6680
/// <returns>An authentication parameter.</returns>
67-
protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
81+
protected async ValueTask<Parameter> GetAuthenticationParameter()
6882
{
6983
var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token;
70-
return new HeaderParameter(KnownHeaders.Authorization, token);
84+
return new HeaderParameter(KnownHeaders.Authorization, token!);
7185
}
7286

7387
/// <summary>
@@ -76,31 +90,39 @@ namespace {{packageName}}.Client.Auth
7690
/// <returns>An authentication token.</returns>
7791
async Task<string> GetToken()
7892
{
79-
var client = new RestClient(_tokenUrl,
80-
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
81-
82-
var request = new RestRequest()
83-
.AddParameter("grant_type", _grantType)
84-
.AddParameter("client_id", _clientId)
85-
.AddParameter("client_secret", _clientSecret);
93+
var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
8694
95+
var request = new RestRequest();
96+
if (!string.IsNullOrWhiteSpace(_token?.RefreshToken))
97+
{
98+
request.AddParameter("grant_type", "refresh_token")
99+
.AddParameter("refresh_token", _token!.RefreshToken);
100+
}
101+
else
102+
{
103+
request
104+
.AddParameter("grant_type", _grantType)
105+
.AddParameter("client_id", _clientId)
106+
.AddParameter("client_secret", _clientSecret);
107+
}
87108
if (!string.IsNullOrEmpty(_scope))
88109
{
89110
request.AddParameter("scope", _scope);
90111
}
91-
92-
var response = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
93-
112+
_token = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
94113
// RFC6749 - token_type is case insensitive.
95114
// RFC6750 - In Authorization header Bearer should be capitalized.
96115
// Fix the capitalization irrespective of token_type casing.
97-
switch (response.TokenType?.ToLower())
116+
switch (_token?.TokenType?.ToLower())
98117
{
99118
case "bearer":
100-
return $"Bearer {response.AccessToken}";
119+
return $"Bearer {_token.AccessToken}";
101120
default:
102-
return $"{response.TokenType} {response.AccessToken}";
121+
return $"{_token?.TokenType} {_token?.AccessToken}";
103122
}
104123
}
124+
125+
public async ValueTask Authenticate(IRestClient client, RestRequest request)
126+
=> request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false));
105127
}
106128
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@ namespace {{packageName}}.Client.Auth
1010
public string TokenType { get; set; }
1111
[JsonProperty("access_token")]
1212
public string AccessToken { get; set; }
13+
[JsonProperty("expires_in")]
14+
public int? ExpiresIn { get; set; }
15+
[JsonProperty("created")]
16+
public DateTime Created { get; set; }
17+
18+
[JsonProperty("refresh_token")]
19+
public string{{nrt?}} RefreshToken { get; set; }
20+
21+
public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value);
1322
}
1423
}

0 commit comments

Comments
 (0)