Skip to main content

(Py)thon (Pa)ssword(le)ss Tokens.

Project description

pypale

(Py)thon (Pa)ssword(le)ss Tokens.

pip install pypale

usage

from pypale import Pypale

token_ttl_minutes = 14 * 24 * 60        # 2 weeks
token_issue_ttl_seconds = 2 * 60 * 60   # 2 hours
base_url = "mydomain.com"
secret_key = "loadthisfromyoursecretsmanager"

pypale = Pypale(
    base_url=base_url,
    secret_key=secret_key,
    token_ttl_minutes=token_ttl_minutes,
    token_issue_ttl_seconds=token_issue_ttl_seconds)

email = "jane.doe@example.com"
token = pypale.generate_token(email)
assert pypale.valid_token(token, email)

an example with sendgrid

# send an email with a magic login link to "jane.doe@example.com"
import sendgrid
from sendgrid.helpers.mail import Content, Email, Mail, To

email = "jane.doe@example.com"
token = pypale.generate_token(email)
link = f"https://mydomain.com/link_login/?code={token}"
sg = sendgrid.SendGridAPIClient(api_key=your_sendgrid_api_key)
from_email = Email("bob@mydomain.co")
to_email = To(email)
subject = "Hello!"
content = Content(
            "text/html", f"Click this <a href={link}>link</a> to log in.")
mail = Mail(from_email, to_email, subject, content)
response = self.sg.client.mail.send.post(request_body=mail.get())
print (response)

# in your link_login route, mentioned in the link var above,
# make sure to call pypale.validate_token with the token and
# properly handle valid and invalid tokens.
# for example ...

def link_login(code: str):
    if not pypale.valid_token(code):
        raise Exception("Invalid login.")
    access_token = base64.b64decode(code).decode("utf8")
    return {
        "access_token": base64.b64decode(code).decode("utf8"),
        "token_type": "bearer"
    }

Contributions & Suggestions

Pull requests and issues are very welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypale-0.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

pypale-0.0.1-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pypale-0.0.1.tar.gz.

File metadata

  • Download URL: pypale-0.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for pypale-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3fb31a3884589185278accf3438357fea57c5ba2aba1ebefa33af5c3dbad4ca9
MD5 096aa0d7a81e15a58d719f764706fbea
BLAKE2b-256 713cb077da76942ccefabfe29ab5cb214d2f3d791119612517edcd5337006752

See more details on using hashes here.

File details

Details for the file pypale-0.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pypale-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for pypale-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4efe461c0bd418f6076fe9b7747ebcb4cbba2bb993e3814c575813f14c6e7cf7
MD5 273f94d5fc3c5167b7c4eba83effae23
BLAKE2b-256 eb45f2c7ba05a2ae2b39105dff308d1e89625a8a52080ae870d85cdcc4d8597b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page