Cotter
  • 🚀Getting Started
  • Features & Concepts
    • 💬Sign In with Email/Phone Number
    • 🔐Sign In with Device
      • How it works
    • 🧬Sign In with WebAuthn
  • 📌Quickstart Guides
    • All Guides & Tutorials
    • HTML – Sign in with Email/Phone
    • React – Sign in with Email/Phone
    • React – WebAuthn
    • ▲ Next.js
    • Angular
    • Webflow
    • Bubble.io
    • Python SDK for a CLI
    • React Native – Sign in with Device
    • iOS – Sign in with Device
    • Flutter – Sign in with Device
  • 📘SDK Reference
    • Web
      • Sign In with Email/Phone Number
        • Customize the Form
        • Checking the email or phone before sending a verification code
        • Sending Code or Link via WhatsApp
        • Styling
        • Older SDK
          • Customize the Form
      • Sign in with Social Login
        • Getting Access Tokens from Social Login Providers
        • Github Instructions
        • Google Instructions
      • Sign In with WebAuthn
        • Register WebAuthn for a logged-in user
      • Sign In with Device
        • Steps for Pop Up Authentication Prompt
        • Advanced Customization for Login Form
        • Advanced Customization for Pop Up Authentication Prompt
      • Getting Access Token and Logged-In User Info
      • Sending Successful Form Submission
      • FAQ & Troubleshooting
    • React Native
      • Installation
      • Sign In with Device
        • Add Email/Phone Verification
        • Authenticate from a Non-Trusted Device
        • Add a new Trusted Device
        • Remove Trusted Device
      • Sign In with Email/Phone Number
      • Getting Stored OAuth Tokens and User Information
      • FAQ
      • Older SDK Versions
        • Sign in with Email/Phone
        • Sending Code via WhatsApp
        • Sign In with Device
          • Authenticate from a Non-Trusted Device
          • Add a new Trusted Device
          • Customization
    • Flutter
      • Sign In with Device
        • Add Email/Phone Verification
        • Authenticate from a Non-Trusted Device
      • Sign in with Email/Phone Number
      • Getting the Logged-in User
      • Getting OAuth Tokens
      • Signing a User Out
    • iOS
      • Sign In with Email/Phone Number
      • Sign In with Device
        • Authenticate from a Non-Trusted Device
        • Push Notification
        • Check if Trusted Device is Enrolled
        • Add a New Trusted Device
        • Remove Trusted Device
      • Older Versions
        • Biometric/Pin
    • Android
      • Sign In with Device
        • Authenticate from a Non-Trusted Device
        • Check if Trusted Device is Enrolled
        • Add a new Trusted Device
        • Remove Trusted Device
        • Customization
      • Sign In with Email/Phone Number
      • Biometric/Pin
        • Advanced Methods
        • Customization
        • Setting Strings
        • Styling
      • Older SDK Version
        • Sign In with Device
          • Authenticate from a Non-Trusted Device
    • Python (for CLI)
    • API for Other Mobile Apps or CLI
      • Verify Email/Phone Number
        • Handling URL Scheme
    • Backend: Handling Response
  • 🛡️ Protecting Your Account
    • Only Allow Your Website/App to Use Your API Key
    • Rate Limit
    • Enable reCAPTCHA to Protect Against Automated Abuse
  • 🗝️ Getting Access Token
    • Cotter's OAuth 2.0 Tokens Specification
    • Getting the Tokens
      • Get Tokens during Authentication
      • Using the Refresh Token
    • Storing and Removing Tokens
    • Renewing Expired Tokens
    • Verifying JWT Tokens
    • Requesting Custom Fields on your JWT Token
    • Older API
      • Using HTTP Requests
      • Getting the Tokens
        • During Authentication
          • During Email/Phone Verification
        • During enrolling Trusted Devices
  • 🔌API Reference
    • User API
      • User Object
    • OAuth Tokens API
      • Verify JWT Token using API (serverless)
      • Requesting Custom Claims on your Access Token
      • Older API
    • OAuth Tokens from Social Login
    • Event Object
    • Reset PIN API
  • Older API
    • Validating Cotter's Identity Token
    • Validating Cotter's Event Response
Powered by GitBook
On this page
  • How it works
  • Installation
  • Usage
  • Step 1: Copy cotter_login_success.html from the example folder.
  • Step 2: Call Cotter's login function
  • Available methods:
  • Storing the tokens
  • Refreshing tokens (if not using the functions above)
  • Validating tokens
  • Troubleshooting
  1. SDK Reference

Python (for CLI)

PreviousAuthenticate from a Non-Trusted DeviceNextAPI for Other Mobile Apps or CLI

Last updated 4 years ago

Our Python SDK makes it easy to add a login flow to your python scripts and CLI.

How it works

  1. Use calls your CLI to log in, for example mycli login

  2. The CLI will attempt to open a browser with a link to log in and display the link to the user.

  3. The user logs in on their browser, which then sends a code back to the CLI

  4. Our SDK processes the code and return an access token and refresh token to your CLI.

Installation

pip install cotter

Find the latest versions here

Usage

Get your API_KEY_ID from .

Step 1: Copy from the example folder.

You can make your own Success page. After the user successfully logged-in, the website will redirect to http://localhost:port and you should show a "Success message" and tell the user to go back to your terminal. Feel free to copy our example page and modify it.

Put the success page with the name cotter_login_success.html at the same directory as where you put the code below.

Step 2: Call Cotter's login function

import cotter
api_key = "YOUR API KEY ID"
port = 8080 # Open a port to receive code from the website after successful authentication
response = cotter.login_with_email_link(api_key, port)
print(response)

Available methods:

Using Email

# Use Magic Link
response = login_with_email_link(api_key, port)
# Use OTP
response = login_with_email_otp(api_key, port)

Using Phone Number

# Use Magic Link
response = login_with_phone_link(api_key, port)
# Use OTP
response = login_with_phone_otp(api_key, port)

Storing the tokens

Store the tokens to a file:

from cotter import tokenhandler
tokenhandler.store_token_to_file(response["oauth_token"], "cottertoken.json")

Get the tokens from a file (automatically refresh if needed):

from cotter import tokenhandler
oauth_token = tokenhandler.get_token_from_file("cottertoken.json", api_key)

Refreshing tokens (if not using the functions above)

# This will only refresh if needed
from cotter import tokenhandler
oauth_token = tokenhandler.refresh_token(oauth_token, api_key)

Validating tokens

from cotter import validate
access_token_decoded = validate.validate_access_token(response["oauth_token"]["access_token"], api_key)
id_token_decoded = validate.validate_id_token(response["oauth_token"]["id_token"], api_key)

Troubleshooting

Allowed Origin Error

If you get an error like this:

{
  "msg": "The redirect URL http://localhost:1234 or the parent origin :// is not in the list of allowed URLs. Please contact the site owner.",
  "type": ""
}

You may have set up a list of Allowed URLs on the dashboard. Make sure you add these 2 URLs:

  • http://localhost:<PORT> based on the port you used above

:// (this is a bug, join our to receive updates)

📘
https://pypi.org/project/cotter/
Cotter's Dashboard
cotter_login_success.html
Slack channel