Python (for CLI)
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

1
pip install cotter
Copied!
Find the latest versions here https://pypi.org/project/cotter/​

Usage

Get your API_KEY_ID from Cotter's Dashboard.

Step 1: Copy cotter_login_success.html 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

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

Available methods:

Using Email

1
# Use Magic Link
2
response = login_with_email_link(api_key, port)
3
# Use OTP
4
response = login_with_email_otp(api_key, port)
Copied!

Using Phone Number

1
# Use Magic Link
2
response = login_with_phone_link(api_key, port)
3
# Use OTP
4
response = login_with_phone_otp(api_key, port)
Copied!

Storing the tokens

Store the tokens to a file:

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

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

1
from cotter import tokenhandler
2
oauth_token = tokenhandler.get_token_from_file("cottertoken.json", api_key)
Copied!

Refreshing tokens (if not using the functions above)

1
# This will only refresh if needed
2
from cotter import tokenhandler
3
oauth_token = tokenhandler.refresh_token(oauth_token, api_key)
Copied!

Validating tokens

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

Troubleshooting

Allowed Origin Error

If you get an error like this:
1
{
2
"msg": "The redirect URL http://localhost:1234 or the parent origin :// is not in the list of allowed URLs. Please contact the site owner.",
3
"type": ""
4
}
Copied!
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 Slack channel to receive updates)
Last modified 10mo ago