Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Gill <tyler.gill@byu.net>2018-03-20 23:00:52 +0300
committerTyler Gill <tyler.gill@byu.net>2018-03-20 23:00:52 +0300
commit20fc793deed429ea2e29c6f88c6ba86ddcbf93f8 (patch)
treed609e4c89bd64f3b6a4ee870500c4e8fc4060190 /Duplicati/Library/Backend/OAuthHelper
parentf9a8b30b2ff5af2670f9c1d5a9fa399133cd6d1f (diff)
Add OAuthHttpMessageHandler helper class to OAuthHelper library.
This class implements the abstract HttpMessageHandler class (mostly via the default HttpClientHandler implementation). This abstract class is used by System.Net.Http.HttpClient as the underlying HTTP mechanism, and in this particular case is used to automatically add an authorization header to each request. It also provides a method for marking a request as one that should not be authenticated.
Diffstat (limited to 'Duplicati/Library/Backend/OAuthHelper')
-rw-r--r--Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj4
-rw-r--r--Duplicati/Library/Backend/OAuthHelper/OAuthHttpMessageHandler.cs61
2 files changed, 64 insertions, 1 deletions
diff --git a/Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj b/Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj
index f3005f88e..c0515756c 100644
--- a/Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj
+++ b/Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -34,8 +34,10 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
+ <Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="OAuthHttpMessageHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="OAuthHelper.cs" />
<Compile Include="Strings.cs" />
diff --git a/Duplicati/Library/Backend/OAuthHelper/OAuthHttpMessageHandler.cs b/Duplicati/Library/Backend/OAuthHelper/OAuthHttpMessageHandler.cs
new file mode 100644
index 000000000..7bc67a69c
--- /dev/null
+++ b/Duplicati/Library/Backend/OAuthHelper/OAuthHttpMessageHandler.cs
@@ -0,0 +1,61 @@
+// Copyright (C) 2018, The Duplicati Team
+// http://www.duplicati.com, info@duplicati.com
+//
+// This library is free software; you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation; either version 2.1 of the
+// License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+using System;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Duplicati.Library
+{
+ public class OAuthHttpMessageHandler : HttpClientHandler
+ {
+ /// <summary>
+ /// Requests which contain a property with this name (in 'request.Properties') will not have the authentication header automatically added.
+ /// </summary>
+ public const string DISABLE_AUTHENTICATION_PROPERTY = "OAuthHttpMessageHandler_DisableAuthentication";
+
+ private OAuthHelper m_oauth;
+
+ public OAuthHttpMessageHandler(string authid, string protocolKey)
+ {
+ this.m_oauth = new OAuthHelper(authid, protocolKey);
+ }
+
+ /// <summary>
+ /// Prevents authentication from being applied on the given request
+ /// </summary>
+ /// <param name="request">Request to not authenticate</param>
+ /// <returns>Request to not authenticate</returns>
+ public HttpRequestMessage PreventAuthentication(HttpRequestMessage request)
+ {
+ request.Properties[DISABLE_AUTHENTICATION_PROPERTY] = true;
+ return request;
+ }
+
+ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ if (!request.Properties.ContainsKey(DISABLE_AUTHENTICATION_PROPERTY))
+ {
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.m_oauth.AccessToken);
+ }
+
+ return base.SendAsync(request, cancellationToken);
+ }
+ }
+}