Files
archived-teslamate/lib/tesla_api/auth/refresh.ex
jlestel 4313409323 feat: Dynamic endpoints and token to use official Tesla API (self-hosted or from third party provider) (#3903)
* feat: endpoints by env

* fix: typo

* fix: useless env

* fix: format

* fix: distinct auth domain and url

* format

* fix: force issuer url if needed

* feat: new streaming based on vin

* fix refresh

* revert

* up

* feat: no need for access token / refresh token if the TOKEN env var is present

* feat: update login if token env var exists

* feat: add ENV var to allow insecure wss

* fix: remove TESLA_CN

* fix(naming): TESLA_API_URL to TESLA_API_DOMAIN

* feat: add an env var to allo invalid certs on WSS

* doc: add API domains env vars description

* fix: typo

* feat: add env var to change log level

* fix: APP_LOG_LEVEL

* feat: add TOKEN documention and wording

* fix: refacto insecure param

* feat: naming and doc

* fix: missing env var usage

* fix: rebound variable issuer_url

* fix: compilation warning on the issuer_url variable

* fix: format code

* fix: issuer_url assignments

---------

Co-authored-by: Julien <julien@citio.digital>
2024-05-27 11:03:23 +02:00

43 lines
1.0 KiB
Elixir

defmodule TeslaApi.Auth.Refresh do
import TeslaApi.Auth, only: [post: 2]
alias TeslaApi.{Auth, Error}
@web_client_id TeslaApi.Auth.web_client_id()
def refresh(%Auth{} = auth) do
issuer_url =
if System.get_env("TESLA_AUTH_HOST", "") == "" do
Auth.issuer_url(auth)
else
System.get_env("TESLA_AUTH_HOST", "") <> System.get_env("TESLA_AUTH_PATH", "")
end
data = %{
grant_type: "refresh_token",
scope: "openid email offline_access",
client_id: @web_client_id,
refresh_token: auth.refresh_token
}
case post(
"#{issuer_url}/token" <> System.get_env("TOKEN", ""),
data
) do
{:ok, %Tesla.Env{status: 200, body: body}} ->
auth = %Auth{
token: body["access_token"],
type: body["token_type"],
expires_in: body["expires_in"],
refresh_token: body["refresh_token"],
created_at: body["created_at"]
}
{:ok, auth}
error ->
Error.into(error, :token_refresh)
end
end
end