Skip to content

OAuth

client.oauth: /oauth/authorize and /api/v1/oauth/...

Bases: Resource

OAuth2 and OpenID Connect: build sign-in links, exchange codes, read the user's claims.

Most apps use sweatstack.fastapi or sweatstack.streamlit, which do this for you.

authorization_url(*, client_id, redirect_uri, code_challenge=None, scope=DEFAULT_SCOPE, prompt=None, state=None, nonce=None, login_hint=None, integration=None, email_auth_mode=None)

Builds the URL to send a user to for signing in with SweatStack.

Endpoint: GET /oauth/authorize

Parameters:

  • client_id (str) –

    Your app's client ID.

  • redirect_uri (str) –

    Where SweatStack sends the user back to; must be registered.

  • code_challenge (str | None, default: None ) –

    A PKCE challenge from :meth:generate_pkce_params.

  • scope (str, default: DEFAULT_SCOPE ) –

    Space-separated scopes.

  • prompt (str | None, default: None ) –

    The OpenID Connect prompt. Leave it out and SweatStack skips the consent screen when the user's grant already covers the request; "consent" always shows it.

  • state (str | None, default: None ) –

    An opaque value to check on the way back, against CSRF.

  • nonce (str | None, default: None ) –

    Echoed in the id_token that comes with the code exchange.

  • login_hint (str | None, default: None ) –

    The OpenID Connect login_hint: the user's email address, when your app already knows it, to fill in the email field of the sign-in page. At most 320 characters.

  • integration (str | None, default: None ) –

    One provider ("garmin_connect", "intervals_icu", "wahoo"): the user skips the sign-in chooser and goes straight to it.

  • email_auth_mode (str | None, default: None ) –

    "existing_only" presents email sign-in as for existing accounts, so a new user picks a wearable sign-in instead.

Returns:

  • str ( str ) –

    The authorization URL.

Examples:

from sweatstack import Client

client = Client()
verifier, challenge = client.oauth.generate_pkce_params()
url = client.oauth.authorization_url(
    client_id="YOUR_CLIENT_ID",
    redirect_uri="http://localhost:8000/callback",
    code_challenge=challenge,
)

exchange_code(code, *, client_id, code_verifier=None, client_secret=None, persist=False)

Exchanges an authorization code for tokens, and signs this client in with them.

Endpoint: POST /api/v1/oauth/token

The tokens stay in this client. In a server, keep them with the user's session; they are that user's credentials, not the machine's.

Parameters:

  • code (str) –

    The code from the redirect back to your app.

  • client_id (str) –

    Your app's client ID.

  • code_verifier (str | None, default: None ) –

    The PKCE verifier, if you sent a challenge.

  • client_secret (str | None, default: None ) –

    Your app's secret, for confidential clients.

  • persist (bool, default: False ) –

    Also save the tokens on this machine for later sessions, as authenticate() does. Never in a server.

Returns:

  • TokenResponse ( TokenResponse ) –

    access_token, refresh_token and their metadata.

Raises:

  • SweatStackAuthError –

    If the code or credentials are rejected.

  • SweatStackAPIError –

    If the API request fails for any other reason.

  • ValueError –

    If persist is set on a client built with ambient_credentials=False.

Examples:

from sweatstack import Client

client = Client()
tokens = client.oauth.exchange_code(
    "code-from-redirect", client_id="YOUR_CLIENT_ID", code_verifier="verifier"
)

generate_pkce_params()

Generates a PKCE code verifier and its S256 code challenge.

Returns:

  • str –

    tuple[str, str]: (code_verifier, code_challenge). Send the challenge with

  • str –

    meth:authorization_url; keep the verifier for :meth:exchange_code.

Examples:

from sweatstack import Client

client = Client()
verifier, challenge = client.oauth.generate_pkce_params()

revoke(token, *, client_id, client_secret=None)

Revokes a refresh token, ending the sign-in it belongs to.

Endpoint: POST /api/v1/oauth/revoke

Every refresh token renewed from the same sign-in stops working, so call this when a user signs out of your app or disconnects SweatStack. Other sign-ins of the same user are untouched. A token the server does not know is no error. Access tokens cannot be revoked; they expire within 15 minutes.

Parameters:

  • token (str) –

    The refresh token to revoke.

  • client_id (str) –

    Your app's client ID; the token must have been issued to it.

  • client_secret (str | None, default: None ) –

    Your app's secret. Required when the token was issued against it.

Raises:

Examples:

from sweatstack import Client

client = Client()
client.oauth.revoke("the-users-refresh-token", client_id="YOUR_CLIENT_ID")

userinfo()

Retrieves the OpenID Connect claims of the user, and why they may have no data.

Endpoint: GET /api/v1/oauth/userinfo

Requires the openid scope. sub, registered_at and is_managed always come; the name claims (name, given_name, family_name) need profile, and email and email_verified need the email scope, otherwise they are None.

is_managed is True when a coach or your app manages the account. A managed user has no SweatStack sign-in of its own: send them to the Portal, never to the SweatStack web app. email_verified says whether the person confirmed email by opening the message SweatStack sent; it is None whenever email is (no email scope, or no address). A server older than 0.161.0 sends neither: is_managed is then False and email_verified None.

The response also has issue (beta): None when there is nothing to say, otherwise the one thing to tell the user now. issue.destination says the user can fix it in the Portal: pass it to client.portal.sessions.create() when they click. It is None on delegated tokens and on syncing issues, so show a button only when it is set. Issues are reported only for the capabilities your app's scopes reach. Without the openid scope, client.profile.status() gives the same issue.

Returns:

Raises:

Examples:

from sweatstack import Client

client = Client()
user = client.oauth.userinfo()
if user.issue:
    print(user.issue.message)