Skip to content

Commit 8fe0011

Browse files
authored
Merge pull request #10027 from pradyunsg/topic/authentication
Add a topic guide for Authentication
2 parents d74062a + 3930f55 commit 8fe0011

File tree

3 files changed

+88
-60
lines changed

3 files changed

+88
-60
lines changed

docs/html/topics/authentication.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Authentication
2+
3+
## Basic HTTP authentication
4+
5+
pip supports basic HTTP-based authentication credentials. This is done by
6+
providing the username (and optionally password) in the URL:
7+
8+
```
9+
https://username:[email protected]/simple
10+
```
11+
12+
For indexes that only require single-part authentication tokens, provide the
13+
token as the "username" and do not provide a password:
14+
15+
```
16+
https://[email protected]/simple
17+
```
18+
19+
### Percent-encoding special characters
20+
21+
```{versionaddded} 10.0
22+
```
23+
24+
Certain special characters are not valid in the credential part of a URL.
25+
If the user or password part of your login credentials contain any of these
26+
[special characters][reserved-chars], then they must be percent-encoded. As an
27+
example, for a user with username `user` and password `he//o` accessing a
28+
repository at `pypi.company.com/simple`, the URL with credentials would look
29+
like:
30+
31+
```
32+
https://user:he%2F%[email protected]/simple
33+
```
34+
35+
[reserved-chars]: https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
36+
37+
## netrc support
38+
39+
pip supports loading credentials from a user's `.netrc` file. If no credentials
40+
are part of the URL, pip will attempt to get authentication credentials for the
41+
URL's hostname from the user's `.netrc` file. This behaviour comes from the
42+
underlying use of {pypi}`requests`, which in turn delegates it to the
43+
[Python standard library's `netrc` module][netrc-std-lib].
44+
45+
```{note}
46+
As mentioned in the [standard library documentation for netrc][netrc-std-lib],
47+
only ASCII characters are allowed in `.netrc` files. Whitespace and
48+
non-printable characters are not allowed in passwords.
49+
```
50+
51+
Below is an example `.netrc`, for the host `example.com`, with a user named
52+
`daniel`, using the password `qwerty`:
53+
54+
```
55+
machine example.com
56+
login daniel
57+
password qwerty
58+
```
59+
60+
More information about the `.netrc` file format can be found in the GNU [`ftp`
61+
man pages][netrc-docs].
62+
63+
[netrc-docs]: https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
64+
[netrc-std-lib]: https://docs.python.org/3/library/netrc.html
65+
66+
## Keyring Support
67+
68+
pip supports loading credentials stored in your keyring using the
69+
{pypi}`keyring` library.
70+
71+
```bash
72+
$ pip install keyring # install keyring from PyPI
73+
$ echo "your-password" | keyring set pypi.company.com your-username
74+
$ pip install your-package --index-url https://pypi.company.com/
75+
```
76+
77+
Note that `keyring` (the Python package) needs to be installed separately from
78+
pip. This can create a bootstrapping issue if you need the credentials stored in
79+
the keyring to download and install keyring.
80+
81+
It is, thus, expected that users that wish to use pip's keyring support have
82+
some mechanism for downloading and installing {pypi}`keyring` in their Python
83+
environment.

docs/html/topics/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ This section of the documentation is currently being fleshed out. See
99

1010
```{toctree}
1111
:maxdepth: 1
12+
13+
authentication
1214
```

docs/html/user_guide.rst

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -63,72 +63,17 @@ For more information and examples, see the :ref:`pip install` reference.
6363
Basic Authentication Credentials
6464
================================
6565

66-
pip supports basic authentication credentials. Basically, in the URL there is
67-
a username and password separated by ``:``.
68-
69-
``https://[username[:password]@]pypi.company.com/simple``
70-
71-
Certain special characters are not valid in the authentication part of URLs.
72-
If the user or password part of your login credentials contain any of the
73-
special characters
74-
`here <https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters>`_
75-
then they must be percent-encoded. For example, for a
76-
user with username "user" and password "he//o" accessing a repository at
77-
pypi.company.com, the index URL with credentials would look like:
78-
79-
``https://user:he%2F%[email protected]``
80-
81-
Support for percent-encoded authentication in index URLs was added in pip 10.0.0
82-
(in `#3236 <https://github.com/pypa/pip/issues/3236>`_). Users that must use authentication
83-
for their Python repository on systems with older pip versions should make the latest
84-
get-pip.py available in their environment to bootstrap pip to a recent-enough version.
85-
86-
For indexes that only require single-part authentication tokens, provide the token
87-
as the "username" and do not provide a password, for example -
88-
89-
``https://[email protected]``
90-
66+
This is now covered in {doc}`topics/authentication`.
9167

9268
netrc Support
9369
-------------
9470

95-
If no credentials are part of the URL, pip will attempt to get authentication credentials
96-
for the URL’s hostname from the user’s .netrc file. This behaviour comes from the underlying
97-
use of `requests`_ which in turn delegates it to the `Python standard library`_.
98-
99-
The .netrc file contains login and initialization information used by the auto-login process.
100-
It resides in the user's home directory. The .netrc file format is simple. You specify lines
101-
with a machine name and follow that with lines for the login and password that are
102-
associated with that machine. Machine name is the hostname in your URL.
103-
104-
An example .netrc for the host example.com with a user named 'daniel', using the password
105-
'qwerty' would look like:
106-
107-
.. code-block:: shell
108-
109-
machine example.com
110-
login daniel
111-
password qwerty
112-
113-
As mentioned in the `standard library docs <https://docs.python.org/3/library/netrc.html>`_,
114-
only ASCII characters are allowed. Whitespace and non-printable characters are not allowed in passwords.
115-
71+
This is now covered in {doc}`topics/authentication`.
11672

11773
Keyring Support
11874
---------------
11975

120-
pip also supports credentials stored in your keyring using the `keyring`_
121-
library. Note that ``keyring`` will need to be installed separately, as pip
122-
does not come with it included.
123-
124-
.. code-block:: shell
125-
126-
pip install keyring
127-
echo your-password | keyring set pypi.company.com your-username
128-
pip install your-package --index-url https://pypi.company.com/
129-
130-
.. _keyring: https://pypi.org/project/keyring/
131-
76+
This is now covered in {doc}`topics/authentication`.
13277

13378
Using a Proxy Server
13479
====================
@@ -1904,6 +1849,4 @@ announcements on the `low-traffic packaging announcements list`_ and
19041849
.. _low-traffic packaging announcements list: https://mail.python.org/mailman3/lists/pypi-announce.python.org/
19051850
.. _our survey on upgrades that create conflicts: https://docs.google.com/forms/d/e/1FAIpQLSeBkbhuIlSofXqCyhi3kGkLmtrpPOEBwr6iJA6SzHdxWKfqdA/viewform
19061851
.. _the official Python blog: https://blog.python.org/
1907-
.. _requests: https://requests.readthedocs.io/en/latest/user/authentication/#netrc-authentication
1908-
.. _Python standard library: https://docs.python.org/3/library/netrc.html
19091852
.. _Python Windows launcher: https://docs.python.org/3/using/windows.html#launcher

0 commit comments

Comments
 (0)