google_drive.diff 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. diff --git a/src/libcmis/oauth2-providers.cxx b/src/libcmis/oauth2-providers.cxx
  2. index 5e7f3bf..68a6aa5 100644
  3. --- a/src/libcmis/oauth2-providers.cxx
  4. +++ b/src/libcmis/oauth2-providers.cxx
  5. @@ -37,11 +37,28 @@ using namespace std;
  6. string OAuth2Providers::OAuth2Gdrive( HttpSession* session, const string& authUrl,
  7. const string& username, const string& password )
  8. {
  9. + /* This member function implements 'Google OAuth 2.0'
  10. + *
  11. + * The interaction is carried out by libcmis, with no web browser involved.
  12. + *
  13. + * Normal sequence (without 2FA) is:
  14. + * 1) a get to activate login page
  15. + * receive first login page, html format
  16. + * 2) subsequent post to sent email
  17. + * receive html page for password input
  18. + * 3) subsequent post to send password
  19. + * receive html page for application consent
  20. + * 4) subsequent post to send a consent for the application
  21. + * receive a single-use authorization code
  22. + * this code is returned as a string
  23. + */
  24. +
  25. static const string CONTENT_TYPE( "application/x-www-form-urlencoded" );
  26. // STEP 1: Log in
  27. string res;
  28. try
  29. {
  30. + // send the first get, receive the html login page
  31. res = session->httpGetRequest( authUrl )->getStream( )->str( );
  32. }
  33. catch ( const CurlException& e )
  34. @@ -49,20 +66,39 @@ string OAuth2Providers::OAuth2Gdrive( HttpSession* session, const string& authUr
  35. return string( );
  36. }
  37. - string loginPost, loginLink;
  38. - if ( !parseResponse( res.c_str( ), loginPost, loginLink ) )
  39. + string loginEmailPost, loginEmailLink;
  40. + if ( !parseResponse( res.c_str( ), loginEmailPost, loginEmailLink ) )
  41. return string( );
  42. -
  43. - loginPost += "Email=";
  44. - loginPost += string( username );
  45. - loginPost += "&Passwd=";
  46. - loginPost += string( password );
  47. -
  48. - istringstream loginIs( loginPost );
  49. - string loginRes;
  50. - try
  51. +
  52. + loginEmailPost += "Email=";
  53. + loginEmailPost += string( username );
  54. +
  55. + istringstream loginEmailIs( loginEmailPost );
  56. + string loginEmailRes;
  57. + try
  58. + {
  59. + // send a post with user email, receive the html page for password input
  60. + loginEmailRes = session->httpPostRequest ( loginEmailLink, loginEmailIs, CONTENT_TYPE )
  61. + ->getStream( )->str( );
  62. + }
  63. + catch ( const CurlException& e )
  64. + {
  65. + return string( );
  66. + }
  67. +
  68. + string loginPasswdPost, loginPasswdLink;
  69. + if ( !parseResponse( loginEmailRes.c_str( ), loginPasswdPost, loginPasswdLink ) )
  70. + return string( );
  71. +
  72. + loginPasswdPost += "Passwd=";
  73. + loginPasswdPost += string( password );
  74. +
  75. + istringstream loginPasswdIs( loginPasswdPost );
  76. + string loginPasswdRes;
  77. + try
  78. {
  79. - loginRes = session->httpPostRequest ( loginLink, loginIs, CONTENT_TYPE )
  80. + // send a post with user password, receive the application consent page
  81. + loginPasswdRes = session->httpPostRequest ( loginPasswdLink, loginPasswdIs, CONTENT_TYPE )
  82. ->getStream( )->str( );
  83. }
  84. catch ( const CurlException& e )
  85. @@ -71,8 +107,8 @@ string OAuth2Providers::OAuth2Gdrive( HttpSession* session, const string& authUr
  86. }
  87. // STEP 2: allow libcmis to access google drive
  88. - string approvalPost, approvalLink;
  89. - if ( !parseResponse( loginRes. c_str( ), approvalPost, approvalLink) )
  90. + string approvalPost, approvalLink;
  91. + if ( !parseResponse( loginPasswdRes. c_str( ), approvalPost, approvalLink) )
  92. return string( );
  93. approvalPost += "submit_access=true";
  94. @@ -80,7 +116,8 @@ string OAuth2Providers::OAuth2Gdrive( HttpSession* session, const string& authUr
  95. string approvalRes;
  96. try
  97. {
  98. - approvalRes = session->httpPostRequest ( approvalLink, approvalIs,
  99. + // send a post with application consent
  100. + approvalRes = session->httpPostRequest ( approvalLink, approvalIs,
  101. CONTENT_TYPE) ->getStream( )->str( );
  102. }
  103. catch ( const CurlException& e )