2025-03-20 15:23:31 -03:00
|
|
|
// Copyright (C) 2025, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Threading;
|
2025-03-21 13:29:29 -03:00
|
|
|
using System.Threading.Tasks;
|
2025-03-20 15:23:31 -03:00
|
|
|
using Duplicati.Library.Utility;
|
2025-06-11 09:27:40 +02:00
|
|
|
using Duplicati.Library.Utility.Options;
|
2025-03-20 15:23:31 -03:00
|
|
|
|
|
|
|
|
namespace Duplicati.Library;
|
|
|
|
|
|
|
|
|
|
public class OAuthHelperHttpClient : JsonWebHelperHttpClient
|
|
|
|
|
{
|
|
|
|
|
private string _Token;
|
|
|
|
|
private string _Authid;
|
|
|
|
|
private DateTime _mTokenExpires = DateTime.UtcNow;
|
2025-05-23 11:28:53 +02:00
|
|
|
protected string OAuthLoginUrl { get; }
|
2025-06-11 09:27:40 +02:00
|
|
|
private readonly string _OAuthUrl;
|
2025-03-24 17:14:21 +01:00
|
|
|
|
2025-03-20 15:23:31 -03:00
|
|
|
/// <summary>
|
|
|
|
|
/// Timeout for authentication requests
|
|
|
|
|
/// </summary>
|
2025-03-24 17:14:21 +01:00
|
|
|
private static readonly TimeSpan AUTHENTICATION_TIMEOUT = TimeSpan.FromSeconds(25);
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Maximum number of retries for authorization
|
|
|
|
|
/// </summary>
|
2025-03-20 15:23:31 -03:00
|
|
|
private const int MAX_AUTHORIZATION_RETRIES = 5;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to true to automatically add the Authorization header to requests
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool AutoAuthHeader { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to true if the provider does not use refresh tokens, but only access tokens
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool AccessTokenOnly { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true (the default), when a v1 authid is being used it will be swapped
|
|
|
|
|
/// with a v2 authid, when the OAuth service returns one (which it dypically
|
|
|
|
|
/// does after a provider token refresh has been performed). Some providers
|
|
|
|
|
/// are not compatible with v2 authid, tyically because they generate a new
|
|
|
|
|
/// refresh token with every access token refresh and invalidates the old.
|
|
|
|
|
/// If the oauth service still returns a v2 authid for such a provider,
|
|
|
|
|
/// set this property to false to make Duplicati ignore it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool AutoV2 { get; set; } = true;
|
|
|
|
|
|
2025-06-12 10:33:13 -03:00
|
|
|
private static HttpClient CreateHttpClientWithInfiniteTimeout()
|
|
|
|
|
{
|
|
|
|
|
var client = HttpClientHelper.CreateClient();
|
|
|
|
|
client.Timeout = Timeout.InfiniteTimeSpan;
|
|
|
|
|
return client;
|
|
|
|
|
}
|
2025-07-07 15:40:32 +02:00
|
|
|
|
2025-06-11 09:27:40 +02:00
|
|
|
public OAuthHelperHttpClient(string authid, string servicename, string oauthurl, HttpClient httpClient = null, string useragent = null)
|
2025-06-12 10:33:13 -03:00
|
|
|
: base(httpClient ?? CreateHttpClientWithInfiniteTimeout())
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
|
|
|
|
_Authid = authid;
|
2025-06-11 09:27:40 +02:00
|
|
|
_OAuthUrl = oauthurl;
|
|
|
|
|
OAuthLoginUrl = AuthIdOptionsHelper.GetOAuthLoginUrl(servicename, oauthurl);
|
2025-03-20 15:23:31 -03:00
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(authid))
|
|
|
|
|
throw new Interface.UserInformationException(
|
|
|
|
|
Strings.OAuthHelper.MissingAuthID(OAuthLoginUrl), "MissingAuthID");
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
private async Task<HttpRequestMessage> CreateRequestAsync(string url, HttpMethod method, bool noAuthorization, CancellationToken cancellationToken)
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
2025-03-24 17:14:21 +01:00
|
|
|
var request = new HttpRequestMessage(method, url);
|
2025-03-21 13:29:29 -03:00
|
|
|
request.Headers.Add("User-Agent", UserAgent);
|
2025-06-11 09:27:40 +02:00
|
|
|
if (!noAuthorization && AutoAuthHeader && !string.Equals(_OAuthUrl, url)) request.Headers.Authorization = new AuthenticationHeaderValue("Bearer",
|
2025-03-24 17:14:21 +01:00
|
|
|
await GetAccessTokenAsync(cancellationToken).ConfigureAwait(false));
|
2025-03-20 15:23:31 -03:00
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
public override Task<HttpRequestMessage> CreateRequestAsync(string url, HttpMethod method, CancellationToken cancellationToken)
|
|
|
|
|
=> CreateRequestAsync(url, method, false, cancellationToken);
|
|
|
|
|
|
2025-05-23 10:40:55 +02:00
|
|
|
public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToken)
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
2025-03-24 17:14:21 +01:00
|
|
|
if (AccessTokenOnly)
|
|
|
|
|
return _Authid;
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
if (_Token == null || _mTokenExpires < DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
var retries = 0;
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
HttpResponseMessage response = null;
|
|
|
|
|
try
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
2025-07-07 15:40:32 +02:00
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
throw new OperationCanceledException("Operation was cancelled", cancellationToken);
|
|
|
|
|
|
2025-06-11 09:27:40 +02:00
|
|
|
using var request = await CreateRequestAsync(_OAuthUrl, HttpMethod.Get, false, cancellationToken).ConfigureAwait(false);
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
return await Utility.Utility.WithTimeout(AUTHENTICATION_TIMEOUT, cancellationToken, async ct =>
|
|
|
|
|
{
|
2025-05-23 14:11:47 +02:00
|
|
|
request.Headers.Add("X-AuthID", _Authid);
|
2025-03-24 17:14:21 +01:00
|
|
|
response = await _httpClient.SendAsync(request, ct).ConfigureAwait(false);
|
2025-03-20 15:23:31 -03:00
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
var res = await ReadJsonResponseAsync<OAuthServiceResponse>(response, ct).ConfigureAwait(false);
|
2025-03-20 15:23:31 -03:00
|
|
|
|
|
|
|
|
_mTokenExpires = DateTime.UtcNow.AddSeconds(res.expires - 30);
|
|
|
|
|
if (AutoV2 && !string.IsNullOrWhiteSpace(res.v2_authid))
|
|
|
|
|
_Authid = res.v2_authid;
|
|
|
|
|
return _Token = res.access_token;
|
2025-03-24 17:14:21 +01:00
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-07-07 15:40:32 +02:00
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
throw new OperationCanceledException("Operation was cancelled", ex, cancellationToken);
|
|
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
var clientError = false;
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Only retry once on client errors
|
|
|
|
|
if (ex is HttpRequestException { StatusCode: not null } exception)
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
2025-03-24 17:14:21 +01:00
|
|
|
var sc = (int)exception.StatusCode;
|
|
|
|
|
clientError = sc is >= 400 and <= 499;
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
2025-03-24 17:14:21 +01:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-07-07 15:40:32 +02:00
|
|
|
string msg = null;
|
2025-03-24 17:14:21 +01:00
|
|
|
if (response != null && response.Headers.Contains("X-Reason"))
|
|
|
|
|
{
|
2025-07-07 15:40:32 +02:00
|
|
|
msg = response.Headers.GetValues("X-Reason").FirstOrDefault();
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(msg))
|
|
|
|
|
msg = response.StatusCode.ToString();
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
|
|
|
|
|
{
|
|
|
|
|
string errorKey = msg == response.StatusCode.ToString()
|
|
|
|
|
? "OAuthOverQuotaError"
|
|
|
|
|
: "OAuthLoginError";
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
string errorMessage = errorKey == "OAuthOverQuotaError"
|
|
|
|
|
? Strings.OAuthHelper.OverQuotaError
|
|
|
|
|
: Strings.OAuthHelper.AuthorizationFailure(msg, OAuthLoginUrl);
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
throw new Interface.UserInformationException(errorMessage, errorKey,
|
|
|
|
|
errorKey == "OAuthLoginError" ? ex : null);
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-24 17:14:21 +01:00
|
|
|
|
|
|
|
|
if (retries >= (clientError ? 1 : MAX_AUTHORIZATION_RETRIES))
|
2025-03-20 15:23:31 -03:00
|
|
|
{
|
2025-03-24 17:14:21 +01:00
|
|
|
await AttemptParseAndThrowExceptionAsync(ex, response, cancellationToken).ConfigureAwait(false);
|
2025-07-07 15:40:32 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(msg))
|
|
|
|
|
throw new Interface.UserInformationException(Strings.OAuthHelper.AuthorizationFailure(msg, OAuthLoginUrl), "OAuthLoginError", ex);
|
2025-03-24 17:14:21 +01:00
|
|
|
throw;
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
2025-03-24 17:14:21 +01:00
|
|
|
|
2025-07-07 15:40:32 +02:00
|
|
|
await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, retries)), cancellationToken).ConfigureAwait(false);
|
2025-03-24 17:14:21 +01:00
|
|
|
retries++;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
response?.Dispose();
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
2025-03-24 17:14:21 +01:00
|
|
|
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
2025-03-24 17:14:21 +01:00
|
|
|
}
|
2025-03-20 15:23:31 -03:00
|
|
|
|
2025-03-24 17:14:21 +01:00
|
|
|
return _Token;
|
2025-03-20 15:23:31 -03:00
|
|
|
}
|
|
|
|
|
}
|