GetSupportX
Portal sign-in docs
Portal sign-in guide

Python (Flask)

For Python websites built on Flask, with their own login.

Needs a developer

Before you start

In GetSupportX, open Settings → Help center & portal → Portal sign-in and switch on Sign in through your website. Answer What is your website built on? with Other code — PHP, Node.js, Python.

Under 1 · Secret key, click Create secret key. Copy the key straight away: it is shown once. The code on this page needs it.

Copy the code from 2 · Add the code to your website (pick Python (Flask) under “Which language is your website’s code in?”): your callback URL is already filled in there. Copying the code from this page instead? Replace the whole https://<your-help-center>/portal/handoff address with the Callback URL Settings shows under For developers in step 2. Don’t build it by hand: it isn’t the same for every help center.

Steps

  1. Add your secret key to your environment

    As GSX_SSO_SECRET.

  2. Add the code below to your Flask app

    In the file where you create the app (app = Flask(__name__)).

  3. Read your own logged-in user

    Fill in current_user() so it returns a dictionary with the user’s id, email and name, or None when nobody is logged in. If your app already has something called current_user (Flask-Login does), give this function another name.

  4. Set your site’s address and login page

    Set GSX_SITE, and change /login if your login page lives elsewhere. Your login page should send people on to the next address after they log in.

  5. Set your sign-in link

    https://yourwebsite.com/support

The code

Your Flask app
# Flask. Your sign-in link: https://yourwebsite.com/support
# Add this to your Flask app (the file with app = Flask(__name__)).
# Needs GSX_SSO_SECRET in your environment.
import base64, hashlib, hmac, json, os, secrets, time
from urllib.parse import quote, urlsplit
from flask import redirect, request

GSX_CALLBACK = "https://<your-help-center>/portal/handoff"
# Your site's address. It keeps customer ids unique if several websites share
# one GetSupportX workspace — set it once and never change it.
GSX_SITE = "https://yourwebsite.com"


def current_user():
    # REPLACE THIS with however your app reads its logged-in user. Return
    # {"id": ..., "email": ..., "name": ...}, or None when logged out.
    return None


def gsx_redirect(url):
    # Never cached: the link inside signs one person in.
    response = redirect(url, code=302)
    response.headers["Cache-Control"] = "no-store"
    return response


@app.route("/support")
def gsx_support():
    user = current_user()
    if not user:
        # Your login page. "next" is this full address, so the customer comes
        # back here, and on to the page they wanted, after logging in.
        # script_root: the prefix your app is mounted under, if any.
        here = request.script_root + request.full_path.rstrip("?")
        return gsx_redirect("/login?next=" + quote(here, safe=""))

    secret = os.environ.get("GSX_SSO_SECRET")
    if not secret:
        raise RuntimeError("GSX_SSO_SECRET is not set — paste your SSO secret into your environment.")

    now = int(time.time())
    payload = {
        "externalId": urlsplit(GSX_SITE).netloc + ":" + str(user["id"]),
        "site": GSX_SITE,
        "legacyExternalId": str(user["id"]),  # keeps customers an older snippet created
        "email": user.get("email"),
        "name": user.get("name"),
        "iat": now,
        "exp": now + 300,
        "jti": secrets.token_hex(16),  # one-time id: this exact link works once
    }
    # Drop empty values so a blank field never overwrites something an agent typed.
    payload = {k: v for k, v in payload.items() if v not in ("", None)}

    b64 = base64.b64encode(json.dumps(payload).encode("utf-8")).decode("ascii")
    sig = hmac.new(secret.encode("utf-8"), b64.encode("ascii"), hashlib.sha256).hexdigest()
    url = GSX_CALLBACK + "?payload=" + quote(b64, safe="") + "&sig=" + sig

    # The portal page they were heading to (?gsx_return=). Only a plain path is
    # passed on — GetSupportX checks it again before using it.
    back = request.args.get("gsx_return", "")
    if back.startswith("/") and not back.startswith("//") and not back.startswith("/\\"):
        url += "&redirect=" + quote(back, safe="")

    return gsx_redirect(url)

Every version of this code signs the same way. The protocol reference lists each field and rule, including the optional customer details (plan, phone and more) you can add.

Check it works

  1. Save your sign-in link

    In Settings → Help center & portal → Portal sign-in, enter it under 3 · Sign-in link (for example https://yourwebsite.com/support) and click Save. What is the sign-in link?

  2. Click Test sign-in

    Under Check it works. We open your link as a visitor who isn’t logged in. “Your sign-in link is live” means your link sent that visitor somewhere other than your homepage or the portal, and names the address. Check that address is your login page.

  3. Try it as a customer

    Log in to your website, then open your sign-in link. You should land in your portal, signed in. If not, Check it works lists the reason under recent failed sign-ins, and Troubleshooting says how to fix it.

Good to know

  • Keep GSX_SITE the same forever. It is part of every customer’s id.
  • On another Python framework? The steps are the same; the protocol reference lists every rule.

Read next