Show More
@@ -262,7 +262,9 b' Supported arguments:' | |||
|
262 | 262 | Optional. Username to authenticate with. If not given, and the |
|
263 | 263 | remote site requires basic or digest authentication, the user will |
|
264 | 264 | be prompted for it. Environment variables are expanded in the |
|
265 | username letting you do ``foo.username = $USER``. | |
|
265 | username letting you do ``foo.username = $USER``. If the URI | |
|
266 | includes a username, only ``[auth]`` entries with a matching | |
|
267 | username or without a username will be considered. | |
|
266 | 268 | |
|
267 | 269 | ``password`` |
|
268 | 270 | Optional. Password to authenticate with. If not given, and the |
@@ -72,10 +72,19 b' def readauthforuri(ui, uri):' | |||
|
72 | 72 | gdict[setting] = val |
|
73 | 73 | |
|
74 | 74 | # Find the best match |
|
75 | uri = util.url(uri) | |
|
76 | user = uri.user | |
|
77 | uri.user = uri.password = None | |
|
78 | uri = str(uri) | |
|
75 | 79 | scheme, hostpath = uri.split('://', 1) |
|
80 | bestuser = None | |
|
76 | 81 | bestlen = 0 |
|
77 | 82 | bestauth = None |
|
78 | 83 | for group, auth in config.iteritems(): |
|
84 | if user and user != auth.get('username', user): | |
|
85 | # If a username was set in the URI, the entry username | |
|
86 | # must either match it or be unset | |
|
87 | continue | |
|
79 | 88 | prefix = auth.get('prefix') |
|
80 | 89 | if not prefix: |
|
81 | 90 | continue |
@@ -85,9 +94,14 b' def readauthforuri(ui, uri):' | |||
|
85 | 94 | else: |
|
86 | 95 | schemes = (auth.get('schemes') or 'https').split() |
|
87 | 96 | if (prefix == '*' or hostpath.startswith(prefix)) and \ |
|
88 |
len(prefix) > bestlen and |
|
|
97 | (len(prefix) > bestlen or (len(prefix) == bestlen and \ | |
|
98 | not bestuser and 'username' in auth)) \ | |
|
99 | and scheme in schemes: | |
|
89 | 100 | bestlen = len(prefix) |
|
90 | 101 | bestauth = group, auth |
|
102 | bestuser = auth.get('username') | |
|
103 | if user and not bestuser: | |
|
104 | auth['username'] = user | |
|
91 | 105 | return bestauth |
|
92 | 106 | |
|
93 | 107 | # Mercurial (at least until we can remove the old codepath) requires |
@@ -25,7 +25,7 b' class passwordmgr(urllib2.HTTPPasswordMg' | |||
|
25 | 25 | self._writedebug(user, passwd) |
|
26 | 26 | return (user, passwd) |
|
27 | 27 | |
|
28 | if not user: | |
|
28 | if not user or not passwd: | |
|
29 | 29 | res = httpconnectionmod.readauthforuri(self.ui, authuri) |
|
30 | 30 | if res: |
|
31 | 31 | group, auth = res |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | from mercurial import demandimport; demandimport.enable() |
|
2 | from mercurial import ui | |
|
2 | from mercurial import ui, util | |
|
3 | 3 | from mercurial import url |
|
4 | 4 | from mercurial.error import Abort |
|
5 | 5 | |
@@ -19,13 +19,16 b' def dumpdict(dict):' | |||
|
19 | 19 | return '{' + ', '.join(['%s: %s' % (k, dict[k]) |
|
20 | 20 | for k in sorted(dict.iterkeys())]) + '}' |
|
21 | 21 | |
|
22 | def test(auth): | |
|
22 | def test(auth, urls=None): | |
|
23 | 23 | print 'CFG:', dumpdict(auth) |
|
24 | 24 | prefixes = set() |
|
25 | 25 | for k in auth: |
|
26 | 26 | prefixes.add(k.split('.', 1)[0]) |
|
27 | 27 | for p in prefixes: |
|
28 |
|
|
|
28 | for name in ('.username', '.password'): | |
|
29 | if (p + name) not in auth: | |
|
30 | auth[p + name] = p | |
|
31 | auth = dict((k, v) for k, v in auth.iteritems() if v is not None) | |
|
29 | 32 | |
|
30 | 33 | ui = writeauth(auth) |
|
31 | 34 | |
@@ -33,16 +36,26 b' def test(auth):' | |||
|
33 | 36 | print 'URI:', uri |
|
34 | 37 | try: |
|
35 | 38 | pm = url.passwordmgr(ui) |
|
39 | authinfo = util.url(uri).authinfo()[1] | |
|
40 | if authinfo is not None: | |
|
41 | pm.add_password(*authinfo) | |
|
36 | 42 | print ' ', pm.find_user_password('test', uri) |
|
37 | 43 | except Abort, e: |
|
38 | 44 | print 'abort' |
|
39 | 45 | |
|
40 | _test('http://example.org/foo') | |
|
41 | _test('http://example.org/foo/bar') | |
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 | if not urls: | |
|
47 | urls = [ | |
|
48 | 'http://example.org/foo', | |
|
49 | 'http://example.org/foo/bar', | |
|
50 | 'http://example.org/bar', | |
|
51 | 'https://example.org/foo', | |
|
52 | 'https://example.org/foo/bar', | |
|
53 | 'https://example.org/bar', | |
|
54 | 'https://x@example.org/bar', | |
|
55 | 'https://y@example.org/bar', | |
|
56 | ] | |
|
57 | for u in urls: | |
|
58 | _test(u) | |
|
46 | 59 | |
|
47 | 60 | |
|
48 | 61 | print '\n*** Test in-uri schemes\n' |
@@ -62,3 +75,23 b" test({'x.prefix': 'http://example.org/fo" | |||
|
62 | 75 | test({'x.prefix': 'http://example.org/foo', |
|
63 | 76 | 'y.prefix': 'http://example.org/foo/bar'}) |
|
64 | 77 | test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'}) |
|
78 | ||
|
79 | print '\n*** Test user matching\n' | |
|
80 | test({'x.prefix': 'http://example.org/foo', | |
|
81 | 'x.username': None, | |
|
82 | 'x.password': 'xpassword'}, | |
|
83 | urls=['http://y@example.org/foo']) | |
|
84 | test({'x.prefix': 'http://example.org/foo', | |
|
85 | 'x.username': None, | |
|
86 | 'x.password': 'xpassword', | |
|
87 | 'y.prefix': 'http://example.org/foo', | |
|
88 | 'y.username': 'y', | |
|
89 | 'y.password': 'ypassword'}, | |
|
90 | urls=['http://y@example.org/foo']) | |
|
91 | test({'x.prefix': 'http://example.org/foo/bar', | |
|
92 | 'x.username': None, | |
|
93 | 'x.password': 'xpassword', | |
|
94 | 'y.prefix': 'http://example.org/foo', | |
|
95 | 'y.username': 'y', | |
|
96 | 'y.password': 'ypassword'}, | |
|
97 | urls=['http://y@example.org/foo/bar']) |
@@ -14,6 +14,10 b' URI: https://example.org/foo/bar' | |||
|
14 | 14 | abort |
|
15 | 15 | URI: https://example.org/bar |
|
16 | 16 | abort |
|
17 | URI: https://x@example.org/bar | |
|
18 | abort | |
|
19 | URI: https://y@example.org/bar | |
|
20 | abort | |
|
17 | 21 | CFG: {x.prefix: https://example.org} |
|
18 | 22 | URI: http://example.org/foo |
|
19 | 23 | abort |
@@ -27,6 +31,10 b' URI: https://example.org/foo/bar' | |||
|
27 | 31 | ('x', 'x') |
|
28 | 32 | URI: https://example.org/bar |
|
29 | 33 | ('x', 'x') |
|
34 | URI: https://x@example.org/bar | |
|
35 | ('x', 'x') | |
|
36 | URI: https://y@example.org/bar | |
|
37 | abort | |
|
30 | 38 | CFG: {x.prefix: http://example.org, x.schemes: https} |
|
31 | 39 | URI: http://example.org/foo |
|
32 | 40 | ('x', 'x') |
@@ -40,6 +48,10 b' URI: https://example.org/foo/bar' | |||
|
40 | 48 | abort |
|
41 | 49 | URI: https://example.org/bar |
|
42 | 50 | abort |
|
51 | URI: https://x@example.org/bar | |
|
52 | abort | |
|
53 | URI: https://y@example.org/bar | |
|
54 | abort | |
|
43 | 55 | CFG: {x.prefix: https://example.org, x.schemes: http} |
|
44 | 56 | URI: http://example.org/foo |
|
45 | 57 | abort |
@@ -53,6 +65,10 b' URI: https://example.org/foo/bar' | |||
|
53 | 65 | ('x', 'x') |
|
54 | 66 | URI: https://example.org/bar |
|
55 | 67 | ('x', 'x') |
|
68 | URI: https://x@example.org/bar | |
|
69 | ('x', 'x') | |
|
70 | URI: https://y@example.org/bar | |
|
71 | abort | |
|
56 | 72 | |
|
57 | 73 | *** Test separately configured schemes |
|
58 | 74 | |
@@ -69,6 +85,10 b' URI: https://example.org/foo/bar' | |||
|
69 | 85 | abort |
|
70 | 86 | URI: https://example.org/bar |
|
71 | 87 | abort |
|
88 | URI: https://x@example.org/bar | |
|
89 | abort | |
|
90 | URI: https://y@example.org/bar | |
|
91 | abort | |
|
72 | 92 | CFG: {x.prefix: example.org, x.schemes: https} |
|
73 | 93 | URI: http://example.org/foo |
|
74 | 94 | abort |
@@ -82,6 +102,10 b' URI: https://example.org/foo/bar' | |||
|
82 | 102 | ('x', 'x') |
|
83 | 103 | URI: https://example.org/bar |
|
84 | 104 | ('x', 'x') |
|
105 | URI: https://x@example.org/bar | |
|
106 | ('x', 'x') | |
|
107 | URI: https://y@example.org/bar | |
|
108 | abort | |
|
85 | 109 | CFG: {x.prefix: example.org, x.schemes: http https} |
|
86 | 110 | URI: http://example.org/foo |
|
87 | 111 | ('x', 'x') |
@@ -95,6 +119,10 b' URI: https://example.org/foo/bar' | |||
|
95 | 119 | ('x', 'x') |
|
96 | 120 | URI: https://example.org/bar |
|
97 | 121 | ('x', 'x') |
|
122 | URI: https://x@example.org/bar | |
|
123 | ('x', 'x') | |
|
124 | URI: https://y@example.org/bar | |
|
125 | abort | |
|
98 | 126 | |
|
99 | 127 | *** Test prefix matching |
|
100 | 128 | |
@@ -111,6 +139,10 b' URI: https://example.org/foo/bar' | |||
|
111 | 139 | abort |
|
112 | 140 | URI: https://example.org/bar |
|
113 | 141 | abort |
|
142 | URI: https://x@example.org/bar | |
|
143 | abort | |
|
144 | URI: https://y@example.org/bar | |
|
145 | abort | |
|
114 | 146 | CFG: {x.prefix: http://example.org/foo, y.prefix: http://example.org/foo/bar} |
|
115 | 147 | URI: http://example.org/foo |
|
116 | 148 | ('x', 'x') |
@@ -124,6 +156,10 b' URI: https://example.org/foo/bar' | |||
|
124 | 156 | abort |
|
125 | 157 | URI: https://example.org/bar |
|
126 | 158 | abort |
|
159 | URI: https://x@example.org/bar | |
|
160 | abort | |
|
161 | URI: https://y@example.org/bar | |
|
162 | abort | |
|
127 | 163 | CFG: {x.prefix: *, y.prefix: https://example.org/bar} |
|
128 | 164 | URI: http://example.org/foo |
|
129 | 165 | abort |
@@ -137,3 +173,19 b' URI: https://example.org/foo/bar' | |||
|
137 | 173 | ('x', 'x') |
|
138 | 174 | URI: https://example.org/bar |
|
139 | 175 | ('y', 'y') |
|
176 | URI: https://x@example.org/bar | |
|
177 | ('x', 'x') | |
|
178 | URI: https://y@example.org/bar | |
|
179 | ('y', 'y') | |
|
180 | ||
|
181 | *** Test user matching | |
|
182 | ||
|
183 | CFG: {x.password: xpassword, x.prefix: http://example.org/foo, x.username: None} | |
|
184 | URI: http://y@example.org/foo | |
|
185 | ('y', 'xpassword') | |
|
186 | CFG: {x.password: xpassword, x.prefix: http://example.org/foo, x.username: None, y.password: ypassword, y.prefix: http://example.org/foo, y.username: y} | |
|
187 | URI: http://y@example.org/foo | |
|
188 | ('y', 'ypassword') | |
|
189 | CFG: {x.password: xpassword, x.prefix: http://example.org/foo/bar, x.username: None, y.password: ypassword, y.prefix: http://example.org/foo, y.username: y} | |
|
190 | URI: http://y@example.org/foo/bar | |
|
191 | ('y', 'xpassword') |
General Comments 0
You need to be logged in to leave comments.
Login now