Author: fmui
Date: Thu Aug 20 15:54:59 2015
New Revision: 1696810

URL: http://svn.apache.org/r1696810
Log:
PortCMIS: authentication provider enhancements

Modified:
    chemistry/portcmis/trunk/PortCMIS/binding/BindingIntf.cs
    chemistry/portcmis/trunk/PortCMIS/client/SessionParameter.cs
    chemistry/portcmis/trunk/PortCMISWin/binding/WindowsBindingIntf.cs

Modified: chemistry/portcmis/trunk/PortCMIS/binding/BindingIntf.cs
URL: 
http://svn.apache.org/viewvc/chemistry/portcmis/trunk/PortCMIS/binding/BindingIntf.cs?rev=1696810&r1=1696809&r2=1696810&view=diff
==============================================================================
--- chemistry/portcmis/trunk/PortCMIS/binding/BindingIntf.cs (original)
+++ chemistry/portcmis/trunk/PortCMIS/binding/BindingIntf.cs Thu Aug 20 
15:54:59 2015
@@ -26,6 +26,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
+using System.Net.Http.Headers;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -99,6 +100,8 @@ namespace PortCMIS.Binding
         public CookieContainer CookieContainer { get; private set; }
         public string User { get { return 
Session.GetValue(SessionParameter.User) as string; } }
         public string Password { get { return 
Session.GetValue(SessionParameter.Password) as string; } }
+        public string ProxyUser { get { return 
Session.GetValue(SessionParameter.ProxyUser) as string; } }
+        public string ProxyPassword { get { return 
Session.GetValue(SessionParameter.ProxyPassword) as string; } }
 
         public virtual void PrepareHttpClientHandler(HttpClientHandler 
httpClientHandler)
         {
@@ -120,6 +123,21 @@ namespace PortCMIS.Binding
 
     public class StandardAuthenticationProvider : 
AbstractAuthenticationProvider
     {
+        public string BearerToken { get { return 
Session.GetValue(SessionParameter.OAuthBearerToken) as string; } }
+        public string CsrfHeader { get { return 
Session.GetValue(SessionParameter.CsrfHeader) as string; } }
+
+        protected AuthenticationHeaderValue AuthenticationHeader { get; set; }
+        protected AuthenticationHeaderValue ProxyAuthenticationHeader { get; 
set; }
+
+        private object tokenLock = new object();
+        private string token = "fetch";
+        protected string CsrfHeaderName { get; set; }
+        protected string CsrfToken
+        {
+            get { lock (tokenLock) { return token; } }
+            set { lock (tokenLock) { token = value; } }
+        }
+
         public override void PrepareHttpClientHandler(HttpClientHandler 
httpClientHandler)
         {
             base.PrepareHttpClientHandler(httpClientHandler);
@@ -130,7 +148,61 @@ namespace PortCMIS.Binding
             }
             else
             {
-                httpClientHandler.UseDefaultCredentials = true;
+                if (BearerToken != null)
+                {
+                    httpClientHandler.PreAuthenticate = true;
+                    httpClientHandler.UseDefaultCredentials = false;
+                    AuthenticationHeader = new 
AuthenticationHeaderValue("Bearer", BearerToken);
+                }
+                else
+                {
+                    httpClientHandler.UseDefaultCredentials = true;
+                }
+            }
+
+            if (ProxyUser != null)
+            {
+                var userPassword = Encoding.UTF8.GetBytes(ProxyUser + ":" + 
ProxyPassword);
+                ProxyAuthenticationHeader = new 
AuthenticationHeaderValue("Basic", Convert.ToBase64String(userPassword));
+            }
+
+            if (CsrfHeader != null)
+            {
+                CsrfHeaderName = CsrfHeader;
+            }
+        }
+
+        public override void PrepareHttpRequestMessage(HttpRequestMessage 
httpRequestMessage)
+        {
+            base.PrepareHttpRequestMessage(httpRequestMessage);
+
+            if (AuthenticationHeader != null)
+            {
+                httpRequestMessage.Headers.Authorization = 
AuthenticationHeader;
+            }
+
+            if (ProxyAuthenticationHeader != null)
+            {
+                httpRequestMessage.Headers.ProxyAuthorization = 
ProxyAuthenticationHeader;
+            }
+
+            if (CsrfHeaderName != null && CsrfToken != null)
+            {
+                httpRequestMessage.Headers.Add(CsrfHeaderName, CsrfToken);
+            }
+        }
+
+        public override void HandleResponse(HttpResponseMessage 
httpResponseMessage)
+        {
+            base.HandleResponse(httpResponseMessage);
+
+            if (CsrfHeaderName != null)
+            {
+                IEnumerable<string> values;
+                if (httpResponseMessage.Headers.TryGetValues(CsrfHeaderName, 
out values))
+                {
+                    CsrfToken = values.First();
+                }
             }
         }
     }

Modified: chemistry/portcmis/trunk/PortCMIS/client/SessionParameter.cs
URL: 
http://svn.apache.org/viewvc/chemistry/portcmis/trunk/PortCMIS/client/SessionParameter.cs?rev=1696810&r1=1696809&r2=1696810&view=diff
==============================================================================
--- chemistry/portcmis/trunk/PortCMIS/client/SessionParameter.cs (original)
+++ chemistry/portcmis/trunk/PortCMIS/client/SessionParameter.cs Thu Aug 20 
15:54:59 2015
@@ -60,6 +60,14 @@ namespace PortCMIS.Client
         public const string CacheTTLPathToId = 
"org.apache.chemistry.portcmis.cache.pathtoid.ttl";
         public const string CachePathOmit = 
"org.apache.chemistry.portcmis.cache.path.omit";
 
+        // OAuth 2
+        public const string OAuthBearerToken = 
"org.apache.chemistry.portcmis.binding.auth.http.oauth.bearer";
+        // proxy
+        public const string ProxyUser = 
"org.apache.chemistry.portcmis.binding.proxyuser";
+        public const string ProxyPassword = 
"org.apache.chemistry.portcmis.binding.proxypassword";
+        // CSRF 
+        public const string CsrfHeader = 
"org.apache.chemistry.portcmis.binding.csrfheader";
+
         //  workarounds 
         public const string IncludeObjectIdUrlParamOnCheckout = 
"org.apache.chemistry.portcmis.workaround.includeObjectIdOnCheckout";
         public const string IncludeObjectIdUrlParamOnMove = 
"org.apache.chemistry.portcmis.workaround.includeObjectIdOnMove";

Modified: chemistry/portcmis/trunk/PortCMISWin/binding/WindowsBindingIntf.cs
URL: 
http://svn.apache.org/viewvc/chemistry/portcmis/trunk/PortCMISWin/binding/WindowsBindingIntf.cs?rev=1696810&r1=1696809&r2=1696810&view=diff
==============================================================================
--- chemistry/portcmis/trunk/PortCMISWin/binding/WindowsBindingIntf.cs 
(original)
+++ chemistry/portcmis/trunk/PortCMISWin/binding/WindowsBindingIntf.cs Thu Aug 
20 15:54:59 2015
@@ -19,8 +19,10 @@
 
 using PortCMIS.Client;
 using Windows.Security.Credentials;
+using Windows.Security.Cryptography;
 using Windows.Web.Http;
 using Windows.Web.Http.Filters;
+using Windows.Web.Http.Headers;
 
 namespace PortCMIS.Binding
 {
@@ -37,6 +39,8 @@ namespace PortCMIS.Binding
         public HttpCookieManager CookieManager { get; private set; }
         public string User { get { return 
Session.GetValue(SessionParameter.User) as string; } }
         public string Password { get { return 
Session.GetValue(SessionParameter.Password) as string; } }
+        public string ProxyUser { get { return 
Session.GetValue(SessionParameter.ProxyUser) as string; } }
+        public string ProxyPassword { get { return 
Session.GetValue(SessionParameter.ProxyPassword) as string; } }
 
         public virtual void PrepareHttpClientFilter(HttpBaseProtocolFilter 
httpClientFilter)
         {
@@ -61,6 +65,21 @@ namespace PortCMIS.Binding
 
     public class StandardWindowsAuthenticationProvider : 
AbstractWindowsAuthenticationProvider
     {
+        public string BearerToken { get { return 
Session.GetValue(SessionParameter.OAuthBearerToken) as string; } }
+        public string CsrfHeader { get { return 
Session.GetValue(SessionParameter.CsrfHeader) as string; } }
+
+        protected HttpCredentialsHeaderValue AuthenticationHeader { get; set; }
+        protected HttpCredentialsHeaderValue ProxyAuthenticationHeader { get; 
set; }
+
+        private object tokenLock = new object();
+        private string token = "fetch";
+        protected string CsrfHeaderName { get; set; }
+        protected string CsrfToken
+        {
+            get { lock (tokenLock) { return token; } }
+            set { lock (tokenLock) { token = value; } }
+        }
+
         public override void PrepareHttpClientFilter(HttpBaseProtocolFilter 
httpClientFilter)
         {
             base.PrepareHttpClientFilter(httpClientFilter);
@@ -69,6 +88,55 @@ namespace PortCMIS.Binding
             {
                 httpClientFilter.ServerCredential = new 
PasswordCredential("cmis", User, Password);
             }
+            else if (BearerToken != null)
+            {
+                AuthenticationHeader = new 
HttpCredentialsHeaderValue("Bearer", BearerToken);
+            }
+
+            if (ProxyUser != null)
+            {
+                var userPassword = 
CryptographicBuffer.ConvertStringToBinary(ProxyUser + ":" + ProxyPassword, 
BinaryStringEncoding.Utf16LE);
+                ProxyAuthenticationHeader = new 
HttpCredentialsHeaderValue("Basic", 
CryptographicBuffer.EncodeToBase64String(userPassword));
+            }
+
+            if (CsrfHeader != null)
+            {
+                CsrfHeaderName = CsrfHeader;
+            }
+        }
+
+        public override void PrepareHttpRequestMessage(HttpRequestMessage 
httpRequestMessage)
+        {
+            base.PrepareHttpRequestMessage(httpRequestMessage);
+
+            if (AuthenticationHeader != null)
+            {
+                httpRequestMessage.Headers.Authorization = 
AuthenticationHeader;
+            }
+
+            if (ProxyAuthenticationHeader != null)
+            {
+                httpRequestMessage.Headers.ProxyAuthorization = 
ProxyAuthenticationHeader;
+            }
+
+            if (CsrfHeaderName != null && CsrfToken != null)
+            {
+                httpRequestMessage.Headers.Add(CsrfHeaderName, CsrfToken);
+            }
+        }
+
+        public override void HandleResponse(HttpResponseMessage 
httpResponseMessage)
+        {
+            base.HandleResponse(httpResponseMessage);
+
+            if (CsrfHeaderName != null)
+            {
+                string value;
+                if (httpResponseMessage.Headers.TryGetValue(CsrfHeaderName, 
out value))
+                {
+                    CsrfToken = value;
+                }
+            }
         }
     }
 }


Reply via email to