Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MFA for Chinese accounts #2234

Merged
merged 1 commit into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/tesla_api/auth/login.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,25 @@ defmodule TeslaApi.Auth.Login do
String.contains?(body, "Recaptcha is required") ->
{:error, %Error{reason: :recaptcha_required, env: env}}

String.contains?(body, "/oauth2/v3/authorize/mfa/verify") ->
String.contains?(body, "/authorize/mfa/verify") ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to change this as telsa.cn uses /oauth2/v1/authorize/mfa/verify in javascript

headers = [{"referer", env.url}, {"cookie", ctx.cookies}]

with {:ok, devices} <- MFA.list_devices(transaction_id, headers) do
with {:ok, devices} <- MFA.list_devices(ctx.base_url, transaction_id, headers) do
callback = fn device_id, mfa_passcode ->
try do
with {:ok, env} <-
MFA.verify_passcode(device_id, mfa_passcode, transaction_id, headers),
MFA.verify_passcode(
ctx.base_url,
device_id,
mfa_passcode,
transaction_id,
headers
),
{:ok, {redirect_uri, code}} <- Util.parse_location_header(env, ctx.state),
{:ok, auth} <-
get_web_token(code, ctx.code_verifier, redirect_uri, ctx.state),
get_web_token(code, ctx.code_verifier, redirect_uri, ctx.state,
base: ctx.base_url
),
{:ok, auth} <- maybe_exchange_sso_tokens(auth) do
{:ok, auth}
end
Expand Down Expand Up @@ -224,7 +232,7 @@ defmodule TeslaApi.Auth.Login do
end
end

defp get_web_token(code, code_verifier, redirect_uri, state, opts \\ []) do
defp get_web_token(code, code_verifier, redirect_uri, state, opts) do
data = %{
grant_type: "authorization_code",
client_id: @web_client_id,
Expand Down
10 changes: 5 additions & 5 deletions lib/tesla_api/auth/mfa.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ defmodule TeslaApi.Auth.MFA do

alias TeslaApi.{Error}

def list_devices(transaction_id, headers) do
def list_devices(base_url, transaction_id, headers) do
params = [transaction_id: transaction_id]

case get("/oauth2/v3/authorize/mfa/factors", query: params, headers: headers) do
case get("#{base_url}/oauth2/v3/authorize/mfa/factors", query: params, headers: headers) do
{:ok, %Tesla.Env{status: 200, body: %{"data" => devices}}} ->
{:ok, devices}

Expand All @@ -15,7 +15,7 @@ defmodule TeslaApi.Auth.MFA do
end
end

def verify_passcode(device_id, mfa_passcode, transaction_id, headers) do
def verify_passcode(base_url, device_id, mfa_passcode, transaction_id, headers) do
params = [transaction_id: transaction_id]

data = %{
Expand All @@ -24,11 +24,11 @@ defmodule TeslaApi.Auth.MFA do
passcode: mfa_passcode
}

case post("/oauth2/v3/authorize/mfa/verify", data, headers: headers) do
case post("#{base_url}/oauth2/v3/authorize/mfa/verify", data, headers: headers) do
{:ok, %Tesla.Env{status: 200, body: body} = env} ->
case body do
%{"data" => %{"approved" => true, "valid" => true}} ->
case get("/oauth2/v3/authorize", query: params, headers: headers) do
case get("#{base_url}/oauth2/v3/authorize", query: params, headers: headers) do
{:ok, %Tesla.Env{status: 302} = env} ->
{:ok, env}

Expand Down