Skip to content

Commit

Permalink
Telegram strategy: allow to invoke callback with string "init_data" k…
Browse files Browse the repository at this point in the history
…ey to support post request params
  • Loading branch information
vheathen authored and danschultzer committed Dec 27, 2024
1 parent 65e9ce3 commit 44aa2f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/assent/strategies/telegram.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,19 @@ defmodule Assent.Strategies.Telegram do

@type login_widget_response :: %{String.t() => String.t()}
@type mini_app_init_data :: String.t()
@type response_params :: %{init_data: mini_app_init_data()} | login_widget_response()
@type mini_app_response ::
%{init_data: mini_app_init_data()} | %{String.t() => mini_app_init_data()}
@type response_params :: mini_app_response() | login_widget_response()

@impl Assent.Strategy
def authorize_url(_config) do
{:error, "Telegram does not support direct authorization request, please check docs"}
end

@impl Assent.Strategy
def callback(config, %{"init_data" => init_data} = _response_params),
do: callback(config, %{init_data: init_data})

def callback(config, %{} = response_params) do
config = enrich_config(config)

Expand Down
10 changes: 9 additions & 1 deletion test/assent/strategies/telgram_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Assent.Strategies.TelgramTest do
use ExUnit.Case


alias Assent.Strategies.Telegram

# 1_000 years
Expand Down Expand Up @@ -79,6 +78,15 @@ defmodule Assent.Strategies.TelgramTest do
Telegram.callback(@config_mini_app, @web_app_callback_request_params)

assert user == @web_app_claims

request_params_with_string_key = %{
"init_data" => @web_app_callback_request_params.init_data
}

assert {:ok, %{user: user}} =
Telegram.callback(@config_mini_app, request_params_with_string_key)

assert user == @web_app_claims
end

test "error if max auth validity exceeded for the login widget" do
Expand Down

0 comments on commit 44aa2f9

Please sign in to comment.