Skip to content

Commit fdce935

Browse files
author
Bryan Aldrich
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 fdce935

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

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

Lines changed: 38 additions & 15 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? _token;
17+
18+
public string? 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>
@@ -79,28 +93,37 @@ namespace {{packageName}}.Client.Auth
7993
var client = new RestClient(_tokenUrl,
8094
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
8195
82-
var request = new RestRequest()
83-
.AddParameter("grant_type", _grantType)
84-
.AddParameter("client_id", _clientId)
85-
.AddParameter("client_secret", _clientSecret);
86-
96+
var request = new RestRequest();
97+
if (!string.IsNullOrWhiteSpace(_token?.RefreshToken))
98+
{
99+
request.AddParameter("grant_type", "refresh_token")
100+
.AddParameter("refresh_token", _token!.RefreshToken);
101+
}
102+
else
103+
{
104+
request
105+
.AddParameter("grant_type", _grantType)
106+
.AddParameter("client_id", _clientId)
107+
.AddParameter("client_secret", _clientSecret);
108+
}
87109
if (!string.IsNullOrEmpty(_scope))
88110
{
89111
request.AddParameter("scope", _scope);
90112
}
91-
92-
var response = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
93-
113+
_token = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
94114
// RFC6749 - token_type is case insensitive.
95115
// RFC6750 - In Authorization header Bearer should be capitalized.
96116
// Fix the capitalization irrespective of token_type casing.
97-
switch (response.TokenType?.ToLower())
117+
switch (_token?.TokenType?.ToLower())
98118
{
99119
case "bearer":
100-
return $"Bearer {response.AccessToken}";
120+
return $"Bearer {_token.AccessToken}";
101121
default:
102-
return $"{response.TokenType} {response.AccessToken}";
122+
return $"{_token?.TokenType} {_token?.AccessToken}";
103123
}
104124
}
125+
126+
public async ValueTask Authenticate(IRestClient client, RestRequest request)
127+
=> request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false));
105128
}
106129
}

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? RefreshToken { get; set; }
20+
21+
public DateTime? ExpiresAt => ExpiresIn == null ? null : Created.AddSeconds(ExpiresIn.Value);
1322
}
1423
}

0 commit comments

Comments
 (0)