Skip to content

Commit 37a51ba

Browse files
committed
add new client auth step
1 parent aa0729e commit 37a51ba

File tree

10 files changed

+64
-13
lines changed

10 files changed

+64
-13
lines changed

app/controllers/redmine_telegram_setup_controller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ def step_2
1414
end
1515
end
1616

17+
def step_3
18+
RedmineBots::Telegram::Tdlib.wrap do
19+
promise = RedmineBots::Telegram::Tdlib::Authenticate.(params).rescue do |error|
20+
redirect_to plugin_settings_path('redmine_bots'), alert: error.message
21+
end
22+
23+
RedmineBots::Telegram::Tdlib.permit_concurrent_loads { promise.wait! }
24+
end
25+
end
26+
1727
def authorize
1828
RedmineBots::Telegram::Tdlib.wrap do
1929
promise = RedmineBots::Telegram::Tdlib::Authenticate.(params).then do

app/views/redmine_telegram_setup/step_1.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
22
<hr>
3-
<p><strong>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %></strong> — 2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %></p>
3+
<p><strong>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %></strong> — 2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %> — 3. <%= t 'redmine_bots.settings.telegram.auth_step_3' %></p>
44
<hr>
55

66
<%= form_tag telegram_setup_2_path, method: :post do %>

app/views/redmine_telegram_setup/step_2.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
22
<hr>
3-
<p><%= link_to telegram_setup_1_path do %>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %><% end %><strong>2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %></strong></p>
3+
<p><%= link_to telegram_setup_1_path do %>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %><% end %><strong>2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %></strong> — 3. <%= t 'redmine_bots.settings.telegram.auth_step_3' %></p>
44
<hr>
55

6-
<%= form_tag telegram_setup_authorize_path, method: :post do %>
6+
<%= form_tag telegram_setup_3_path, method: :post do %>
77
<p>
88
<label>
99
<%= t 'redmine_bots.settings.telegram.phone_code' %>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
2+
<hr>
3+
<p><%= link_to telegram_setup_1_path do %>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %><% end %> — 2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %><strong>3. <%= t 'redmine_bots.settings.telegram.auth_step_3' %></strong></p>
4+
<hr>
5+
6+
<%= form_tag telegram_setup_authorize_path, method: :post do %>
7+
<p>
8+
<label>
9+
<%= t 'redmine_bots.settings.telegram.password' %>
10+
</label>
11+
<%= text_field_tag 'password' %>
12+
<em class="info"><%= t 'redmine_bots.settings.telegram.password_hint' %></em>
13+
</p>
14+
15+
<input type="hidden" name="phone_number" value="<%= params[:phone_number] %>">
16+
<input type="hidden" name="phone_code_hash" value="<%= params[:phone_code_hash] %>">
17+
18+
<input type="submit" value="<%= t 'redmine_bots.settings.telegram.authorize_button' %>">
19+
<% end %>

config/locales/en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ en:
1414
phone_number: "Phone Number"
1515
phone_number_hint: "Format: 78005553535"
1616
phone_code: "Telegram code"
17+
password: "Telegram password"
18+
password_hint: "If you do not have two-factor authentication enabled in the Telegram application, click the «Authorize» button."
1719
authorize_button_code: "Receive code"
1820
authorize_button: "Authorize"
1921
authorize_client: "Authorize Telegram client"
2022
authorize_hint: "To authorize Telegram, press the button below. Current authorization will be drop."
2123
plugin_link: "Back to the plugin settings"
2224
auth_step_1: "Receiving Telegram code"
23-
auth_step_2: "Authorization"
25+
auth_step_2: "Code authorization"
26+
auth_step_3: "Password authorization"
2427
reset: "Reset cache and authorization"
2528
bot_init: Initialize bot
2629
bot_deinit: Deinitialize bot

config/locales/ru.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ ru:
1515
phone_number: "Номер телефона"
1616
phone_number_hint: "В формате: 78005553535"
1717
phone_code: "Код из Telegram"
18+
password: "Пароль из Telegram"
19+
password_hint: "Если у вас не включена двухфакторная аутентификация в приложении Telegram, нажмите кнопку «Авторизовать»."
1820
authorize_button_code: "Получить код авторизации"
1921
authorize_client: "Авторизовать клиент Telegram"
2022
authorize_hint: "Чтобы авторизировать Telegram, нажмите кнопку ниже. Текущая авторизация сбросится."
2123
authorize_button: "Авторизовать"
2224
plugin_link: "К настройкам плагина"
2325
auth_step_1: "Получение кода авторизации"
24-
auth_step_2: "Авторизация"
26+
auth_step_2: "Авторизация кода"
27+
auth_step_3: "Авторизация пароля"
2528
reset: "Сбросить кеш и авторизацию"
2629
bot_init: Инициализировать бота
2730
bot_deinit: Деинициализировать бота

config/locales/zh-TW.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ zh-TW:
1414
phone_number: "電話號碼"
1515
phone_number_hint: "以台灣手機 0912-345-678 為例,需輸入:886912345678"
1616
phone_code: "Telegram 代碼"
17+
password: "Telegram 密碼"
18+
password_hint: "如果您沒有在 Telegram 應用程式中啟用雙重認證,請按一下「授權」按鈕。"
1719
authorize_button_code: "接收代碼"
1820
authorize_button: "授權"
1921
authorize_client: "授權給 Telegram 用戶端"
2022
authorize_hint: "請按下面的按鈕授權給 Telegram。目前的授權則會被清除。"
2123
plugin_link: "回到外掛設定"
2224
auth_step_1: "接收 Telegram 代碼"
23-
auth_step_2: "授權"
25+
auth_step_2: "代碼授權"
26+
auth_step_3: "密碼授權"
2427
reset: "重設快取及授權"
2528
bot_init: 初始化機器人
2629
bot_deinit: 停用機器人

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
scope :setup do
1010
get 'step_1' => 'redmine_telegram_setup#step_1', as: :telegram_setup_1
1111
post 'step_2' => 'redmine_telegram_setup#step_2', as: :telegram_setup_2
12+
post 'step_3' => 'redmine_telegram_setup#step_3', as: :telegram_setup_3
1213
post 'authorize' => 'redmine_telegram_setup#authorize', as: :telegram_setup_authorize
1314
delete 'reset' => 'redmine_telegram_setup#reset', as: :telegram_setup_reset
1415
end

lib/redmine_bots/telegram.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,25 @@ def self.tdlib_client
3939
settings = Setting.find_by_name(:plugin_redmine_bots).value
4040
TD::Api.set_log_file_path(Rails.root.join('log', 'redmine_bots', 'tdlib.log').to_s)
4141
config = {
42-
api_id: settings['telegram_api_id'],
43-
api_hash: settings['telegram_api_hash'],
44-
database_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'db').to_s,
45-
files_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'files').to_s,
42+
api_id: settings['telegram_api_id'],
43+
api_hash: settings['telegram_api_hash'],
44+
use_test_dc: false,
45+
database_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'db').to_s,
46+
files_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'files').to_s,
47+
use_file_database: true,
48+
use_chat_info_database: true,
49+
use_secret_chats: true,
50+
use_message_database: true,
51+
system_language_code: 'en',
52+
device_model: 'Ruby TD client',
53+
system_version: 'Unknown',
54+
application_version: '1.0',
4655
}
4756

48-
TD::Client.new(timeout: 300, **config)
57+
client = TD::Client.new(timeout: 300, **config)
58+
client.set_tdlib_parameters(**config)
59+
60+
client
4961
end
5062

5163
def self.init_bot

lib/redmine_bots/telegram/tdlib/authenticate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def call(params)
1919
promise = client.set_authentication_phone_number(phone_number: params[:phone_number], settings: nil)
2020
when AuthorizationState::WaitCode
2121
promise = client.check_authentication_code(code: params[:phone_code]) if params[:phone_code]
22+
when AuthorizationState::WaitPassword
23+
promise = client.check_authentication_password(password: params[:password]) if params[:password]
2224
when AuthorizationState::Ready
23-
puts 'lok'
2425
promise = Promises.fulfilled_future(true)
2526
else
26-
puts update.authorization_state
2727
next
2828
end
2929

0 commit comments

Comments
 (0)