From e19c5ad4b824cc5719b221e4b5526251a6491e0f Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Fri, 20 Aug 2021 10:57:41 +1000
Subject: Import libcmis_oauth_pw_as_refreshtoken.patch from LibreOffice


diff --git a/inc/libcmis/session.hxx b/inc/libcmis/session.hxx
index 0a768a8..ec95ab4 100644
--- a/inc/libcmis/session.hxx
+++ b/inc/libcmis/session.hxx
@@ -95,6 +95,8 @@ namespace libcmis
                 certificate exception feature available on common web browser.
               */
             virtual void setNoSSLCertificateCheck( bool noCheck ) = 0;
+
+            virtual std::string getRefreshToken() { return ""; };
     };
 }
 
diff --git a/src/libcmis/gdrive-session.cxx b/src/libcmis/gdrive-session.cxx
index c31637a..2c8e70d 100644
--- a/src/libcmis/gdrive-session.cxx
+++ b/src/libcmis/gdrive-session.cxx
@@ -70,6 +70,46 @@ GDriveSession::~GDriveSession()
 {
 }
 
+
+void GDriveSession::setOAuth2Data( libcmis::OAuth2DataPtr oauth2 )
+{
+    m_oauth2Handler = new OAuth2Handler( this, oauth2 );
+    m_oauth2Handler->setOAuth2Parser( OAuth2Providers::getOAuth2Parser( getBindingUrl( ) ) );
+
+    oauth2Authenticate( );
+}
+
+void GDriveSession::oauth2Authenticate()
+{
+    // treat the supplied password as refresh token
+    if (!m_password.empty())
+    {
+        try
+        {
+            m_inOAuth2Authentication = true;
+
+            m_oauth2Handler->setRefreshToken(m_password);
+            // Try to get new access tokens using the stored refreshtoken
+            m_oauth2Handler->refresh();
+            m_inOAuth2Authentication = false;
+        }
+        catch (const CurlException &e)
+        {
+            m_inOAuth2Authentication = false;
+            // refresh token expired or invalid, trigger initial auth (that in turn will hit the fallback with copy'n'paste method)
+            BaseSession::oauth2Authenticate();
+        }
+    }
+    else
+    {
+        BaseSession::oauth2Authenticate();
+    }
+}
+
+string GDriveSession::getRefreshToken() {
+    return HttpSession::getRefreshToken();
+}
+
 libcmis::RepositoryPtr GDriveSession::getRepository( )
 {
     // Return a dummy repository since GDrive doesn't have that notion
diff --git a/src/libcmis/gdrive-session.hxx b/src/libcmis/gdrive-session.hxx
index 6fd0777..cb9e71e 100644
--- a/src/libcmis/gdrive-session.hxx
+++ b/src/libcmis/gdrive-session.hxx
@@ -57,8 +57,14 @@ class GDriveSession : public BaseSession
 
         virtual std::vector< libcmis::ObjectTypePtr > getBaseTypes( );
 
+        virtual std::string getRefreshToken();
+
     private:
         GDriveSession( );
+
+        virtual void setOAuth2Data( libcmis::OAuth2DataPtr oauth2 );
+
+        void oauth2Authenticate( );
 };
 
 #endif /* _GDRIVE_SESSION_HXX_ */
diff --git a/src/libcmis/http-session.hxx b/src/libcmis/http-session.hxx
index 851d52d..b62474a 100644
--- a/src/libcmis/http-session.hxx
+++ b/src/libcmis/http-session.hxx
@@ -145,7 +145,7 @@ class HttpSession
 
         void setNoSSLCertificateCheck( bool noCheck );
 
-        std::string getRefreshToken( );
+        virtual std::string getRefreshToken( );
 
     protected:
         HttpSession( );
diff --git a/src/libcmis/oauth2-handler.cxx b/src/libcmis/oauth2-handler.cxx
index a3320e3..65d6acf 100644
--- a/src/libcmis/oauth2-handler.cxx
+++ b/src/libcmis/oauth2-handler.cxx
@@ -159,6 +159,11 @@ string OAuth2Handler::getRefreshToken( )
     return m_refresh;
 }
 
+void OAuth2Handler::setRefreshToken( string refreshToken )
+{
+    m_refresh = refreshToken;
+}
+
 string OAuth2Handler::getHttpHeader( )
 {
     string header;
diff --git a/src/libcmis/oauth2-handler.hxx b/src/libcmis/oauth2-handler.hxx
index 83e91cf..bb9a394 100644
--- a/src/libcmis/oauth2-handler.hxx
+++ b/src/libcmis/oauth2-handler.hxx
@@ -61,6 +61,7 @@ class OAuth2Handler
 
         std::string getAccessToken( ) ;
         std::string getRefreshToken( ) ;
+        void setRefreshToken( std::string refreshToken ) ;
 
         // adding HTTP auth header
         std::string getHttpHeader( ) ;
diff --git a/src/libcmis/onedrive-session.cxx b/src/libcmis/onedrive-session.cxx
index c6f4270..680725a 100644
--- a/src/libcmis/onedrive-session.cxx
+++ b/src/libcmis/onedrive-session.cxx
@@ -68,6 +68,45 @@ OneDriveSession::~OneDriveSession()
 {
 }
 
+void OneDriveSession::setOAuth2Data( libcmis::OAuth2DataPtr oauth2 )
+{
+    m_oauth2Handler = new OAuth2Handler( this, oauth2 );
+    m_oauth2Handler->setOAuth2Parser( OAuth2Providers::getOAuth2Parser( getBindingUrl( ) ) );
+
+    oauth2Authenticate( );
+}
+
+void OneDriveSession::oauth2Authenticate()
+{
+    // treat the supplied password as refresh token
+    if (!m_password.empty())
+    {
+        try
+        {
+            m_inOAuth2Authentication = true;
+
+            m_oauth2Handler->setRefreshToken(m_password);
+            // Try to get new access tokens using the stored refreshtoken
+            m_oauth2Handler->refresh();
+            m_inOAuth2Authentication = false;
+        }
+        catch (const CurlException &e)
+        {
+            m_inOAuth2Authentication = false;
+            // refresh token expired or invalid, trigger initial auth (that in turn will hit the fallback with copy'n'paste method)
+            BaseSession::oauth2Authenticate();
+        }
+    }
+    else
+    {
+        BaseSession::oauth2Authenticate();
+    }
+}
+
+string OneDriveSession::getRefreshToken() {
+    return HttpSession::getRefreshToken();
+}
+
 libcmis::RepositoryPtr OneDriveSession::getRepository( )
 {
     // Return a dummy repository since OneDrive doesn't have that notion
diff --git a/src/libcmis/onedrive-session.hxx b/src/libcmis/onedrive-session.hxx
index 441e110..f2529be 100644
--- a/src/libcmis/onedrive-session.hxx
+++ b/src/libcmis/onedrive-session.hxx
@@ -62,8 +62,14 @@ class OneDriveSession : public BaseSession
 
        bool isAPathMatch( Json objectJson, std::string path );
 
+       virtual std::string getRefreshToken();
+
     private:
         OneDriveSession( );
+
+        virtual void setOAuth2Data( libcmis::OAuth2DataPtr oauth2 );
+
+        void oauth2Authenticate( );
 };
 
 #endif /* _ONEDRIVE_SESSION_HXX_ */
-- 
2.30.2

