[Python] How can I get TOTP into my code (python -post request) and add a hash function

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    so I'm working on this challenge, and I'm having some difficulties. I finished the first two parts of this 3 part challenge but I'm stumped with the 3rd part. I just started learning Python and have never seen this before so I'm quite lost. The rules are to make a JSON string with my email, gist URL, and coding language. That is meant to be in the body of the post request. The post request has these rules:

    The Content-Type: of the request must be application/JSON.

    Authorization The URL is protected by HTTP Basic Authentication, which is explained on Chapter 2 of RFC2617, so you have to provide an Authorization: header field in your POST request

    For the userid of HTTP Basic Authentication, use the same email address you put in the JSON string. For the password, provide a 10-digit time-based one time password conforming to RFC6238 TOTP. Authorization password For generating the TOTP password, you will need to use the following setup:

    You have to read RFC6238 (and the errata too!) and get a correct one time password by yourself. TOTP's Time Step X is 30 seconds. T0 is 0. Use HMAC-SHA-512 for the hash function, instead of the default HMAC-SHA-1. Token shared secret is the userid followed by ASCII string value "STRING" (not including double quotations).

    What I have so far after lots of research:


    import requests
    import json
    from requests.auth import HTTPBasicAuth
    from base64 import b64encode

    url = 'https://api.url.com'
    payload = {
    "github_url": "https://gist.github.com/gist-url",
    "contact_email": "EMAIL",
    "solution_language": "python"
    }
    userid = "EMAIL"
    name = "STRING"
    shared_secret = userid+name
    timestep = 30
    T0 = 0
    header = {"Content-type": "application/json",
    "Authorization": "Basic {}".format(
    b64encode(bytes(f"{userid}:{name}", "utf-8")).decode("ascii"))
    }
    r = requests.post(url, data=json.dumps(payload), headers=header)



    I'm doing this in PYCHARM since thats what I'm using in my uni class if it matters. I'm confused about how to use TOTP (and what it is to be honest) and the hash function to make this work. Right now, if I run this in pycharm I get nothing back.

    Continue reading...

Compartilhe esta Página