Skip to main content

Python Passwordless 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.1.3.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

pypale-0.1.3-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pypale-0.1.3.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for pypale-0.1.3.tar.gz
Algorithm Hash digest
SHA256 363a2e5c7ece82d75f2aca3b76a63756c84b6d6f16538c04250c30d88ae3f3f1
MD5 df6d77a636bef778c553246010f09526
BLAKE2b-256 5f6d52f5dc175d64df7a6474318cad3832fc47797268bf40c16b09e951752da9

See more details on using hashes here.

File details

Details for the file pypale-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pypale-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for pypale-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e41e717e0b31d5960da8fa7b1159063ea0aacda6246a563cf7ed7d79849568fa
MD5 3f7e82d9a69ad9772892136d314ae92c
BLAKE2b-256 14c9f2c7c29464bf7f07555c5d0ecd4ed8b4b66a591d23571f2f10f77d3a9197

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