Skip to content

Commit

Permalink
add new client auth step
Browse files Browse the repository at this point in the history
  • Loading branch information
nevrfl committed Sep 2, 2024
1 parent aa0729e commit 37a51ba
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 13 deletions.
10 changes: 10 additions & 0 deletions app/controllers/redmine_telegram_setup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def step_2
end
end

def step_3
RedmineBots::Telegram::Tdlib.wrap do
promise = RedmineBots::Telegram::Tdlib::Authenticate.(params).rescue do |error|
redirect_to plugin_settings_path('redmine_bots'), alert: error.message
end

RedmineBots::Telegram::Tdlib.permit_concurrent_loads { promise.wait! }
end
end

def authorize
RedmineBots::Telegram::Tdlib.wrap do
promise = RedmineBots::Telegram::Tdlib::Authenticate.(params).then do
Expand Down
2 changes: 1 addition & 1 deletion app/views/redmine_telegram_setup/step_1.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
<hr>
<p><strong>1. <%= t 'redmine_bots.settings.telegram.auth_step_1' %></strong> — 2. <%= t 'redmine_bots.settings.telegram.auth_step_2' %></p>
<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>
<hr>

<%= form_tag telegram_setup_2_path, method: :post do %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/redmine_telegram_setup/step_2.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
<hr>
<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>
<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>
<hr>

<%= form_tag telegram_setup_authorize_path, method: :post do %>
<%= form_tag telegram_setup_3_path, method: :post do %>
<p>
<label>
<%= t 'redmine_bots.settings.telegram.phone_code' %>
Expand Down
19 changes: 19 additions & 0 deletions app/views/redmine_telegram_setup/step_3.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h2><%= t 'redmine_bots.settings.telegram.authorize_client' %></h2>
<hr>
<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>
<hr>

<%= form_tag telegram_setup_authorize_path, method: :post do %>
<p>
<label>
<%= t 'redmine_bots.settings.telegram.password' %>
</label>
<%= text_field_tag 'password' %>
<em class="info"><%= t 'redmine_bots.settings.telegram.password_hint' %></em>
</p>

<input type="hidden" name="phone_number" value="<%= params[:phone_number] %>">
<input type="hidden" name="phone_code_hash" value="<%= params[:phone_code_hash] %>">

<input type="submit" value="<%= t 'redmine_bots.settings.telegram.authorize_button' %>">
<% end %>
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ en:
phone_number: "Phone Number"
phone_number_hint: "Format: 78005553535"
phone_code: "Telegram code"
password: "Telegram password"
password_hint: "If you do not have two-factor authentication enabled in the Telegram application, click the «Authorize» button."
authorize_button_code: "Receive code"
authorize_button: "Authorize"
authorize_client: "Authorize Telegram client"
authorize_hint: "To authorize Telegram, press the button below. Current authorization will be drop."
plugin_link: "Back to the plugin settings"
auth_step_1: "Receiving Telegram code"
auth_step_2: "Authorization"
auth_step_2: "Code authorization"
auth_step_3: "Password authorization"
reset: "Reset cache and authorization"
bot_init: Initialize bot
bot_deinit: Deinitialize bot
Expand Down
5 changes: 4 additions & 1 deletion config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ ru:
phone_number: "Номер телефона"
phone_number_hint: "В формате: 78005553535"
phone_code: "Код из Telegram"
password: "Пароль из Telegram"
password_hint: "Если у вас не включена двухфакторная аутентификация в приложении Telegram, нажмите кнопку «Авторизовать»."
authorize_button_code: "Получить код авторизации"
authorize_client: "Авторизовать клиент Telegram"
authorize_hint: "Чтобы авторизировать Telegram, нажмите кнопку ниже. Текущая авторизация сбросится."
authorize_button: "Авторизовать"
plugin_link: "К настройкам плагина"
auth_step_1: "Получение кода авторизации"
auth_step_2: "Авторизация"
auth_step_2: "Авторизация кода"
auth_step_3: "Авторизация пароля"
reset: "Сбросить кеш и авторизацию"
bot_init: Инициализировать бота
bot_deinit: Деинициализировать бота
Expand Down
5 changes: 4 additions & 1 deletion config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ zh-TW:
phone_number: "電話號碼"
phone_number_hint: "以台灣手機 0912-345-678 為例,需輸入:886912345678"
phone_code: "Telegram 代碼"
password: "Telegram 密碼"
password_hint: "如果您沒有在 Telegram 應用程式中啟用雙重認證,請按一下「授權」按鈕。"
authorize_button_code: "接收代碼"
authorize_button: "授權"
authorize_client: "授權給 Telegram 用戶端"
authorize_hint: "請按下面的按鈕授權給 Telegram。目前的授權則會被清除。"
plugin_link: "回到外掛設定"
auth_step_1: "接收 Telegram 代碼"
auth_step_2: "授權"
auth_step_2: "代碼授權"
auth_step_3: "密碼授權"
reset: "重設快取及授權"
bot_init: 初始化機器人
bot_deinit: 停用機器人
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
scope :setup do
get 'step_1' => 'redmine_telegram_setup#step_1', as: :telegram_setup_1
post 'step_2' => 'redmine_telegram_setup#step_2', as: :telegram_setup_2
post 'step_3' => 'redmine_telegram_setup#step_3', as: :telegram_setup_3
post 'authorize' => 'redmine_telegram_setup#authorize', as: :telegram_setup_authorize
delete 'reset' => 'redmine_telegram_setup#reset', as: :telegram_setup_reset
end
Expand Down
22 changes: 17 additions & 5 deletions lib/redmine_bots/telegram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,25 @@ def self.tdlib_client
settings = Setting.find_by_name(:plugin_redmine_bots).value
TD::Api.set_log_file_path(Rails.root.join('log', 'redmine_bots', 'tdlib.log').to_s)
config = {
api_id: settings['telegram_api_id'],
api_hash: settings['telegram_api_hash'],
database_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'db').to_s,
files_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'files').to_s,
api_id: settings['telegram_api_id'],
api_hash: settings['telegram_api_hash'],
use_test_dc: false,
database_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'db').to_s,
files_directory: Rails.root.join('tmp', 'redmine_bots', 'tdlib', 'files').to_s,
use_file_database: true,
use_chat_info_database: true,
use_secret_chats: true,
use_message_database: true,
system_language_code: 'en',
device_model: 'Ruby TD client',
system_version: 'Unknown',
application_version: '1.0',
}

TD::Client.new(timeout: 300, **config)
client = TD::Client.new(timeout: 300, **config)
client.set_tdlib_parameters(**config)

client
end

def self.init_bot
Expand Down
4 changes: 2 additions & 2 deletions lib/redmine_bots/telegram/tdlib/authenticate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def call(params)
promise = client.set_authentication_phone_number(phone_number: params[:phone_number], settings: nil)
when AuthorizationState::WaitCode
promise = client.check_authentication_code(code: params[:phone_code]) if params[:phone_code]
when AuthorizationState::WaitPassword
promise = client.check_authentication_password(password: params[:password]) if params[:password]
when AuthorizationState::Ready
puts 'lok'
promise = Promises.fulfilled_future(true)
else
puts update.authorization_state
next
end

Expand Down

0 comments on commit 37a51ba

Please sign in to comment.