blob: 97520deca3cdf4f47300fa694194a7ae2dc2b031 [file] [log] [blame]
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00001# Copyright (c) 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Authentication related functions."""
6
7import collections
8import optparse
9
10
11# Authentication configuration extracted from command line options.
12# See doc string for 'make_auth_config' for meaning of fields.
13AuthConfig = collections.namedtuple('AuthConfig', [
14 'use_oauth2', # deprecated, will be always True
15 'save_cookies', # deprecated, will be removed
16 'use_local_webserver',
17 'webserver_port',
18])
19
20
21def make_auth_config(
22 use_oauth2=None,
23 save_cookies=None,
24 use_local_webserver=None,
25 webserver_port=None):
26 """Returns new instance of AuthConfig.
27
28 If some config option is None, it will be set to a reasonable default value.
29 This function also acts as an authoritative place for default values of
30 corresponding command line options.
31 """
32 default = lambda val, d: val if val is not None else d
33 return AuthConfig(
34 default(use_oauth2, False),
35 default(save_cookies, True),
36 default(use_local_webserver, True),
37 default(webserver_port, 8090))
38
39
40def add_auth_options(parser):
41 """Appends OAuth related options to OptionParser."""
42 default_config = make_auth_config()
43 parser.auth_group = optparse.OptionGroup(parser, 'Auth options')
44 parser.auth_group.add_option(
45 '--oauth2',
46 action='store_true',
47 dest='use_oauth2',
48 default=default_config.use_oauth2,
49 help='Use OAuth 2.0 instead of a password.')
50 parser.auth_group.add_option(
51 '--no-oauth2',
52 action='store_false',
53 dest='use_oauth2',
54 default=default_config.use_oauth2,
55 help='Use password instead of OAuth 2.0.')
56 parser.auth_group.add_option(
57 '--no-cookies',
58 action='store_false',
59 dest='save_cookies',
60 default=default_config.save_cookies,
61 help='Do not save authentication cookies to local disk.')
62 parser.auth_group.add_option(
63 '--auth-no-local-webserver',
64 action='store_false',
65 dest='use_local_webserver',
66 default=default_config.use_local_webserver,
67 help='Do not run a local web server when performing OAuth2 login flow.')
68 parser.auth_group.add_option(
69 '--auth-host-port',
70 type=int,
71 default=default_config.webserver_port,
72 help='Port a local web server should listen on. Used only if '
73 '--auth-no-local-webserver is not set. [default: %default]')
74 parser.add_option_group(parser.auth_group)
75
76
77def extract_auth_config_from_options(options):
78 """Given OptionParser parsed options, extracts AuthConfig from it.
79
80 OptionParser should be populated with auth options by 'add_auth_options'.
81 """
82 return make_auth_config(
83 use_oauth2=options.use_oauth2,
84 save_cookies=False if options.use_oauth2 else options.save_cookies,
85 use_local_webserver=options.use_local_webserver,
86 webserver_port=options.auth_host_port)
87
88
89def auth_config_to_command_options(auth_config):
90 """AuthConfig -> list of strings with command line options."""
91 if not auth_config:
92 return []
93 opts = ['--oauth2' if auth_config.use_oauth2 else '--no-oauth2']
94 if not auth_config.save_cookies:
95 opts.append('--no-cookies')
96 if not auth_config.use_local_webserver:
97 opts.append('--auth-no-local-webserver')
98 opts.extend(['--auth-host-port', str(auth_config.webserver_port)])
99 return opts