Skip to content

Commit 20436d4

Browse files
woodruffwewdurbin
authored andcommitted
warehouse: Friendlier token "username", prefix (#6342)
* warehouse: Friendlier token "username", prefix * warehouse: raw_macaroon -> prefixed_macaroon * tests: Update macaroons tests * warehouse: Add TODOs, switch to ^token * tests: Update auth policy tests * tests: Update comments * warehouse: Link TODOs to tracking issue * warehouse: Use __token__ for token usernames * tests: Update auth policy tests
1 parent c31a3cf commit 20436d4

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

tests/unit/macaroons/test_auth_policy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
("maybeafuturemethod foobar", None),
3333
("token foobar", "foobar"),
3434
("basic QHRva2VuOmZvb2Jhcg==", "foobar"), # "@token:foobar"
35+
("basic X190b2tlbl9fOmZvb2Jhcg==", "foobar"), # "__token__:foobar"
3536
],
3637
)
3738
def test_extract_http_macaroon(auth, result):
@@ -49,6 +50,7 @@ def test_extract_http_macaroon(auth, result):
4950
("bm90YXJlYWx0b2tlbg==", None), # "notarealtoken"
5051
("QGJhZHVzZXI6Zm9vYmFy", None), # "@baduser:foobar"
5152
("QHRva2VuOmZvb2Jhcg==", "foobar"), # "@token:foobar"
53+
("X190b2tlbl9fOmZvb2Jhcg==", "foobar"), # "__token__:foobar"
5254
],
5355
)
5456
def test_extract_basic_macaroon(auth, result):

tests/unit/macaroons/test_services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_creation(self):
4545
("noprefixhere", None),
4646
("invalid:prefix", None),
4747
("pypi:validprefix", "validprefix"),
48+
("pypi-validprefix", "validprefix"),
4849
],
4950
)
5051
def test_extract_raw_macaroon(self, macaroon_service, raw_macaroon, result):
@@ -74,7 +75,7 @@ def test_find_userid_invalid_macaroon(self, macaroon_service):
7475
key=b"fake key",
7576
version=pymacaroons.MACAROON_V2,
7677
).serialize()
77-
raw_macaroon = f"pypi:{raw_macaroon}"
78+
raw_macaroon = f"pypi-{raw_macaroon}"
7879

7980
assert macaroon_service.find_userid(raw_macaroon) is None
8081

@@ -107,7 +108,7 @@ def test_verify_no_macaroon(self, macaroon_service):
107108
key=b"fake key",
108109
version=pymacaroons.MACAROON_V2,
109110
).serialize()
110-
raw_macaroon = f"pypi:{raw_macaroon}"
111+
raw_macaroon = f"pypi-{raw_macaroon}"
111112

112113
with pytest.raises(services.InvalidMacaroon):
113114
macaroon_service.verify(

warehouse/macaroons/auth_policy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def _extract_basic_macaroon(auth):
3838
except ValueError:
3939
return None
4040

41-
if auth_method != "@token":
41+
# TODO: Remove @token as an acceptable token username (GH-6345)
42+
if auth_method != "@token" and auth_method != "__token__":
4243
return None
4344

4445
return auth

warehouse/macaroons/services.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ class DatabaseMacaroonService:
3131
def __init__(self, db_session):
3232
self.db = db_session
3333

34-
def _extract_raw_macaroon(self, raw_macaroon):
34+
def _extract_raw_macaroon(self, prefixed_macaroon):
3535
"""
3636
Returns the base64-encoded macaroon component of a PyPI macaroon,
3737
dropping the prefix.
3838
3939
Returns None if the macaroon is None, has no prefix, or has the
4040
wrong prefix.
4141
"""
42-
if raw_macaroon is None:
42+
if prefixed_macaroon is None:
4343
return None
4444

45-
try:
46-
prefix, raw_macaroon = raw_macaroon.split(":", 1)
47-
except ValueError:
48-
return None
45+
prefix, split, raw_macaroon = prefixed_macaroon.partition("-")
46+
# TODO: Remove ':' as an acceptable delimiter for tokens (GH-6345)
47+
if prefix != "pypi" or not split:
48+
prefix, _, raw_macaroon = prefixed_macaroon.partition(":")
4949

5050
if prefix != "pypi":
5151
return None
@@ -129,7 +129,7 @@ def create_macaroon(self, location, user_id, description, caveats):
129129
version=pymacaroons.MACAROON_V2,
130130
)
131131
m.add_first_party_caveat(json.dumps(caveats))
132-
serialized_macaroon = f"pypi:{m.serialize()}"
132+
serialized_macaroon = f"pypi-{m.serialize()}"
133133
return serialized_macaroon, dm
134134

135135
def delete_macaroon(self, macaroon_id):

warehouse/templates/pages/help.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ <h3 id="apitoken">{{ apitoken() }}</h3>
289289
<p>To use an API token:</p>
290290

291291
<ul>
292-
<li>Set your username to <code>@token</code></li>
293-
<li>Set your password to the token value</li>
292+
<li>Set your username to <code>__token__</code></li>
293+
<li>Set your password to the token value, including the <code>pypi-</code> prefix</li>
294294
</ul>
295295

296296
<p>Where you edit or add these values will depend on your individual use case. For example, some users may need to edit <a href="https://packaging.python.org/guides/distributing-packages-using-setuptools/#create-an-account" title="External link" target="_blank" rel="noopener">their <code>.pypirc</code> file</a>, while others may need to update their CI configuration file (e.g. <a href="https://docs.travis-ci.com/user/deployment/pypi/" title="External link" target="_blank" rel="noopener"><code>travis.yml</code> if you are using Travis</a>).</p>

0 commit comments

Comments
 (0)