~sircmpwn/pass-rotate

ref: 7a0f1572c61de44c5ceed81c5d65c6830bc45865 pass-rotate/passrotate/providers/cloudflare.py -rw-r--r-- 2.0 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
51
52
53
54
55
56
57
58
from passrotate.provider import Provider, ProviderOption, register_provider
from passrotate.forms import get_form
from urllib.parse import urlparse
from bs4 import BeautifulSoup
import requests
import json

def get_bootstrap(html):
    soup = BeautifulSoup(html, "html.parser")
    script = [s.text for s in soup.find_all("script") if s.text.startswith("window.bootstrap")][0]
    return json.loads(script[len("window.boostrap = "):-1])

class Cloudflare(Provider):
    """
    [cloudflare.com]
    email=Your Cloudflare email address
    """
    name = "Cloudflare"
    domains = [
        "cloudflare.com",
        "www.cloudflare.com"
    ]
    options = {
        "email": ProviderOption(str, "Your Cloudflare email address")
    }

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

    def prepare(self, old_password):
        self._session = requests.Session()
        r = self._session.get("https://www.cloudflare.com/a/login")
        bs = get_bootstrap(r.text)
        form = {
            "email": self.email,
            "password": old_password,
            "security_token": bs["data"]["security_token"]
        }
        r = self._session.post("https://www.cloudflare.com/a/login", data=form)
        url = urlparse(r.url)
        if url.path != "/a/overview":
            raise Exception("Failed to log into Cloudflare with current password")
        r = self._session.get("https://www.cloudflare.com/a/account/my-account")
        bs = get_bootstrap(r.text)
        self._atok = bs["atok"]

    def execute(self, old_password, new_password):
        r = self._session.put("https://www.cloudflare.com/api/v4/user/password", json={
            "new_password": new_password,
            "new_password_confirm": new_password,
            "old_password": old_password,
        }, headers={
            "x-atok": self._atok
        })
        if r.status_code != 200:
            raise Exception("Failed to update Cloudflare password")

register_provider(Cloudflare)