Incorporate redirect_uri into OAuth and Config modules

pull/369/head
Brian Clark 2022-08-28 16:04:29 -04:00 committed by Brian
parent 8bfa97c709
commit 3b5cb0f099
4 changed files with 25 additions and 13 deletions

View File

@ -249,11 +249,11 @@ int Main( int argc, char **argv )
std::string secret = vm.count( "secret" ) > 0 std::string secret = vm.count( "secret" ) > 0
? vm["secret"].as<std::string>() ? vm["secret"].as<std::string>()
: default_secret ; : default_secret ;
std::string uri = vm.count( "redirect-uri" ) > 0 std::string redirect_uri = vm.count( "redirect-uri" ) > 0
? vm["redirect-uri"].as<std::string>() ? vm["redirect-uri"].as<std::string>()
: default_redirect_uri ; : default_redirect_uri ;
OAuth2 token( http.get(), id, secret ) ; OAuth2 token( http.get(), id, secret, redirect_uri ) ;
if ( vm.count("print-url") ) if ( vm.count("print-url") )
{ {
@ -267,24 +267,27 @@ int Main( int argc, char **argv )
<< token.MakeAuthURL() << token.MakeAuthURL()
<< std::endl ; << std::endl ;
std::string code = AuthCode(uri); std::string code = AuthCode(redirect_uri);
token.Auth( code ) ; token.Auth( code ) ;
// save to config // save to config
config.Set( "id", Val( id ) ) ; config.Set( "id", Val( id ) ) ;
config.Set( "secret", Val( secret ) ) ; config.Set( "secret", Val( secret ) ) ;
config.Set( "refresh_token", Val( token.RefreshToken() ) ) ; config.Set( "refresh_token", Val( token.RefreshToken() ) ) ;
config.Set( "redirect_uri", Val( redirect_uri ) ) ;
config.Save() ; config.Save() ;
} }
std::string refresh_token ; std::string refresh_token ;
std::string id ; std::string id ;
std::string secret ; std::string secret ;
std::string redirect_uri ;
try try
{ {
refresh_token = config.Get("refresh_token").Str() ; refresh_token = config.Get("refresh_token").Str() ;
id = config.Get("id").Str() ; id = config.Get("id").Str() ;
secret = config.Get("secret").Str() ; secret = config.Get("secret").Str() ;
redirect_uri = config.Get("redirect_uri").Str() ;
} }
catch ( Exception& e ) catch ( Exception& e )
{ {
@ -296,7 +299,7 @@ int Main( int argc, char **argv )
return -1; return -1;
} }
OAuth2 token( http.get(), refresh_token, id, secret ) ; OAuth2 token( http.get(), refresh_token, id, secret, redirect_uri ) ;
AuthAgent agent( token, http.get() ) ; AuthAgent agent( token, http.get() ) ;
v2::Syncer2 syncer( &agent ); v2::Syncer2 syncer( &agent );

View File

@ -35,12 +35,14 @@ const std::string token_url = "https://accounts.google.com/o/oauth2/token" ;
OAuth2::OAuth2( OAuth2::OAuth2(
http::Agent* agent, http::Agent* agent,
const std::string& refresh_code, const std::string& refresh_code,
const std::string& client_id, const std::string& client_id,
const std::string& client_secret ) : const std::string& client_secret,
const std::string& redirect_uri ) :
m_refresh( refresh_code ), m_refresh( refresh_code ),
m_agent( agent ), m_agent( agent ),
m_client_id( client_id ), m_client_id( client_id ),
m_client_secret( client_secret ) m_client_secret( client_secret ),
m_redirect_uri ( redirect_uri )
{ {
Refresh( ) ; Refresh( ) ;
} }
@ -48,10 +50,12 @@ OAuth2::OAuth2(
OAuth2::OAuth2( OAuth2::OAuth2(
http::Agent* agent, http::Agent* agent,
const std::string& client_id, const std::string& client_id,
const std::string& client_secret ) : const std::string& client_secret,
const std::string& redirect_uri ) :
m_agent( agent ), m_agent( agent ),
m_client_id( client_id ), m_client_id( client_id ),
m_client_secret( client_secret ) m_client_secret( client_secret ),
m_redirect_uri( redirect_uri )
{ {
} }
@ -61,7 +65,7 @@ void OAuth2::Auth( const std::string& auth_code )
"code=" + auth_code + "code=" + auth_code +
"&client_id=" + m_client_id + "&client_id=" + m_client_id +
"&client_secret=" + m_client_secret + "&client_secret=" + m_client_secret +
"&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" + "&redirect_uri=" + m_redirect_uri +
"&grant_type=authorization_code" ; "&grant_type=authorization_code" ;
http::ValResponse resp ; http::ValResponse resp ;
@ -85,7 +89,7 @@ std::string OAuth2::MakeAuthURL()
{ {
return "https://accounts.google.com/o/oauth2/auth" return "https://accounts.google.com/o/oauth2/auth"
"?scope=" + m_agent->Escape( "https://www.googleapis.com/auth/drive" ) + "?scope=" + m_agent->Escape( "https://www.googleapis.com/auth/drive" ) +
"&redirect_uri=urn:ietf:wg:oauth:2.0:oob" "&redirect_uri=" + m_redirect_uri +
"&response_type=code" "&response_type=code"
"&client_id=" + m_client_id ; "&client_id=" + m_client_id ;
} }

View File

@ -35,12 +35,14 @@ public :
OAuth2( OAuth2(
http::Agent* agent, http::Agent* agent,
const std::string& client_id, const std::string& client_id,
const std::string& client_secret ) ; const std::string& client_secret,
const std::string& redirect_uri ) ;
OAuth2( OAuth2(
http::Agent* agent, http::Agent* agent,
const std::string& refresh_code, const std::string& refresh_code,
const std::string& client_id, const std::string& client_id,
const std::string& client_secret ) ; const std::string& client_secret,
const std::string& redirect_uri ) ;
std::string Str() const ; std::string Str() const ;
@ -62,6 +64,7 @@ private :
const std::string m_client_id ; const std::string m_client_id ;
const std::string m_client_secret ; const std::string m_client_secret ;
const std::string m_redirect_uri ;
} ; } ;
} // end of namespace } // end of namespace

View File

@ -47,6 +47,8 @@ Config::Config( const po::variables_map& vm )
m_cmd.Add( "path", Val(vm.count("path") > 0 m_cmd.Add( "path", Val(vm.count("path") > 0
? vm["path"].as<std::string>() ? vm["path"].as<std::string>()
: default_root_folder ) ) ; : default_root_folder ) ) ;
if ( vm.count( "redirect_uri" ) > 0 )
m_cmd.Add( "redirect_uri", Val( vm["redirect_uri"].as<std::string>() ) ) ;
m_cmd.Add( "dir", Val(vm.count("dir") > 0 m_cmd.Add( "dir", Val(vm.count("dir") > 0
? vm["dir"].as<std::string>() ? vm["dir"].as<std::string>()
: "" ) ) ; : "" ) ) ;