Ueberauth.Strategy.IntervalsIcu.Token (Ueberauth intervals.icu v0.1.2)

Copy Markdown View Source

The access token returned by the intervals.icu token endpoint.

intervals.icu returns a deliberately small token payload:

{
  "token_type": "Bearer",
  "access_token": "d842c1fc25f241e5ae440d09756448a9",
  "scope": "ACTIVITY:WRITE,WELLNESS:WRITE",
  "athlete": { "id": "2049151", "name": "David (intervals.icu)" }
}

Two things are worth calling out, because they differ from most OAuth 2.0 providers:

  • There is no refresh_token and no expires_in. Tokens do not expire on a timer. See Ueberauth.Strategy.IntervalsIcu for what that means for your application.

  • The athlete's id and name are included in the token response itself, so a separate user-info request is optional rather than required.

Any field intervals.icu adds in the future is preserved in :other_params rather than being discarded.

Summary

Functions

Builds a token from a decoded token-endpoint response body.

The token's granted scopes, as a list.

Types

athlete()

@type athlete() :: %{optional(String.t()) => term()}

t()

@type t() :: %Ueberauth.Strategy.IntervalsIcu.Token{
  access_token: String.t(),
  athlete: athlete(),
  other_params: %{optional(String.t()) => term()},
  scope: String.t() | nil,
  token_type: String.t()
}

Functions

from_response(body)

@spec from_response(term()) :: {:ok, t()} | :error

Builds a token from a decoded token-endpoint response body.

Returns :error when the body is not a map or carries no usable access_token.

Examples

iex> {:ok, token} =
...>   Ueberauth.Strategy.IntervalsIcu.Token.from_response(%{
...>     "token_type" => "Bearer",
...>     "access_token" => "abc123",
...>     "scope" => "ACTIVITY:READ,WELLNESS:READ",
...>     "athlete" => %{"id" => "2049151", "name" => "David"}
...>   })
iex> token.access_token
"abc123"
iex> token.athlete["id"]
"2049151"

iex> Ueberauth.Strategy.IntervalsIcu.Token.from_response(%{"error" => "invalid_grant"})
:error

scopes(token)

@spec scopes(t()) :: [String.t()]

The token's granted scopes, as a list.

intervals.icu joins scopes with commas, not the spaces used by most OAuth 2.0 providers, so this cannot be a plain String.split/2 on whitespace.

Examples

iex> alias Ueberauth.Strategy.IntervalsIcu.Token
iex> Token.scopes(%Token{scope: "ACTIVITY:READ,WELLNESS:WRITE"})
["ACTIVITY:READ", "WELLNESS:WRITE"]

iex> alias Ueberauth.Strategy.IntervalsIcu.Token
iex> Token.scopes(%Token{scope: nil})
[]