"""Adds command line options related to authentication."""
parser.auth_group = optparse.OptionGroup(parser, 'Authentication')
parser.auth_group.add_option(
- '--use-cookie-auth', action='store_true',
- help='Use cookie-based authentication instead of OAuth.')
+ '--auth-method',
+ metavar='METHOD',
+ default='bot' if tools.is_headless() else 'oauth',
+ help='Authentication method to use: %s. [default: %%default]' %
+ ', '.join(net.AUTH_METHODS))
parser.add_option_group(parser.auth_group)
oauth.add_oauth_options(parser)
-def process_auth_options(options):
+def process_auth_options(parser, options):
"""Configures process-wide authentication parameters based on |options|."""
- method = 'cookie' if options.use_cookie_auth else 'oauth'
- net.configure_auth(method, oauth_options=options)
+ if options.auth_method not in net.AUTH_METHODS:
+ parser.error('Invalid --auth-method value: %s' % options.auth_method)
+ net.configure_auth(options.auth_method, oauth_options=options)
class AuthServiceError(Exception):
def __init__(self, url):
self._service = net.get_http_service(url)
- def login(self):
+ def login(self, allow_user_interaction):
"""Refreshes cached access token or creates a new one."""
- return self._service.login()
+ return self._service.login(allow_user_interaction)
def logout(self):
"""Purges cached access token."""
@subcommand.usage('[options]')
def CMDlogin(parser, args):
- """Refreshes short lived authentication token."""
+ """Runs interactive login flow and stores auth token/cookie on disk."""
(options, args) = parser.parse_args(args)
- process_auth_options(options)
+ process_auth_options(parser, options)
service = AuthService(options.service)
- if service.login():
+ if service.login(True):
print 'Logged in as \'%s\'.' % service.get_current_identity()
return 0
else:
@subcommand.usage('[options]')
def CMDlogout(parser, args):
- """Purges cached credentials."""
+ """Purges cached auth token/cookie."""
(options, args) = parser.parse_args(args)
- process_auth_options(options)
+ process_auth_options(parser, options)
service = AuthService(options.service)
service.logout()
+ return 0
@subcommand.usage('[options]')
def CMDcheck(parser, args):
- """Shows identity associated with currently cached credentials."""
+ """Shows identity associated with currently cached auth token/cookie."""
(options, args) = parser.parse_args(args)
- process_auth_options(options)
+ process_auth_options(parser, options)
service = AuthService(options.service)
+ service.login(False)
print service.get_current_identity()
+ return 0
class OptionParserAuth(tools.OptionParserWithLogging):