~sircmpwn/pass-rotate

ref: 7a0f1572c61de44c5ceed81c5d65c6830bc45865 pass-rotate/passrotate/providers/pypi.py -rw-r--r-- 1.6 KiB
7a0f1572 — Matthias Gabriel add option to override the pass name to be used when rotating the password 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from passrotate.provider import Provider, ProviderOption, register_provider
from passrotate.forms import get_form, custom_get_form
import requests


class PyPI(Provider):
    """
    [pypi.python.org]
    username=Your PyPI username
    """
    name = "PyPI"
    domains = [
        "pypi.python.org",
    ]
    options = {
        "username": ProviderOption(str, "Your PyPI username")
    }

    def __init__(self, options):
        self.username = options["username"]

    def prepare(self, old_password):
        self._session = requests.Session()
        r = self._session.get("https://pypi.python.org/pypi?%3Aaction=login_form")
        self._form = get_form(r.text, type="div", id="content")
        self._form.update({
            "username": self.username,
            "password": old_password
        })
        r = self._session.post("https://pypi.python.org/pypi",
                               data=self._form, allow_redirects=False)
        if not r.ok:
            raise Exception("Unable to log into PyPI with current password")
        r = self._session.get("https://pypi.python.org/pypi?%3Aaction=user_form")
        self._form = custom_get_form(
            r.text,
            lambda x: x.find(id="content").find("form").find_all("input")
        )

    def execute(self, old_password, new_password):
        self._form.update({
            "password": new_password,
            "confirm": new_password
        })
        r = self._session.post("https://pypi.python.org/pypi",
                               data=self._form, allow_redirects=False)
        if not r.ok:
            raise Exception("Failed to update PyPI password")

register_provider(PyPI)