-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from centosadmin/develop
Develop
- Loading branch information
Showing
36 changed files
with
437 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
<script async src="https://telegram.org/js/telegram-widget.js?4" data-telegram-login="<%= Setting.plugin_redmine_bots['telegram_bot_name'] %>" data-size="large" data-auth-url="<%= Setting.protocol %>://<%= Setting.host_name %>/telegram/check_auth?<%= params.slice(:autologin, :back_url).to_query %>" data-request-access="write"></script> | ||
<script async src="https://telegram.org/js/telegram-widget.js?4" data-telegram-login="<%= Setting.plugin_redmine_bots['telegram_bot_name'] %>" data-size="large" data-auth-url="<%= Setting.protocol %>://<%= Setting.host_name %>/telegram/check_auth?<%= params.slice(:autologin, :back_url).merge(context: context).to_query %>" data-request-access="write"></script> | ||
<% current_user = @user || User.current %> | ||
<% telegram_account = | ||
case context | ||
when 'account_connection' | ||
current_user.telegram_account | ||
when '2fa_connection' | ||
current_user.telegram_connection | ||
end | ||
%> | ||
<br> | ||
|
||
<% if current_user.logged? && telegram_account.present? %> | ||
<%= link_to send_telegram_sign_in_link_path(params.slice(:autologin, :back_url)), method: :post, remote: true do %> | ||
<%= t('redmine_bots.telegram.bot.login.send_to_telegram') %> | ||
<% end %> (<%= t('redmine_bots.telegram.bot.login.widget_not_visible') %>) | ||
<% else %> | ||
<%= t('redmine_bots.telegram.bot.login.write_to_bot', bot: Setting.plugin_redmine_bots['telegram_bot_name']) %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div align="center" style="margin-top: 20%"> | ||
<%= render partial: 'telegram_login/widget' %> | ||
<%= render partial: 'telegram_login/widget', locals: { context: 'account_connection' } %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module RedmineBots | ||
class Result | ||
attr_reader :value | ||
|
||
def initialize(success, value) | ||
@success, @value = success, value | ||
end | ||
|
||
def success? | ||
@success | ||
end | ||
|
||
def failure? | ||
!success? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module RedmineBots | ||
module Telegram | ||
class Bot::AuthenticateByToken | ||
def self.call(*args) | ||
new(*args).call | ||
end | ||
|
||
def initialize(user, token, context:) | ||
@user, @token, @context = user, token, context | ||
end | ||
|
||
def call | ||
return failure(I18n.t('redmine_bots.telegram.bot.login.errors.not_logged')) if @user.anonymous? | ||
|
||
case @context | ||
when '2fa_connection' | ||
telegram_account = prepare_telegram_account(model_class: Redmine2FA::TelegramConnection) | ||
when 'account_connection' | ||
telegram_account = prepare_telegram_account(model_class: TelegramAccount) | ||
else | ||
return failure('Invalid context') | ||
end | ||
|
||
return failure(I18n.t('redmine_bots.telegram.bot.login.errors.wrong_account')) unless telegram_account | ||
|
||
if telegram_account.save | ||
success(telegram_account) | ||
else | ||
failure(I18n.t('redmine_bots.telegram.bot.login.errors.not_persisted')) | ||
end | ||
rescue JWT::DecodeError, JWT::ExpiredSignature, JWT::InvalidIssuerError, JWT::InvalidIatError | ||
failure(I18n.t('redmine_bots.telegram.bot.login.errors.invalid_token')) | ||
end | ||
|
||
def prepare_telegram_account(model_class:) | ||
telegram_data = Jwt.decode_token(@token).first | ||
telegram_id = telegram_data['telegram_id'].to_i | ||
telegram_account = model_class.find_by(user_id: @user.id) | ||
|
||
if telegram_account.present? | ||
if telegram_account.telegram_id | ||
unless telegram_id == telegram_account.telegram_id | ||
return nil | ||
end | ||
else | ||
telegram_account.telegram_id = telegram_id | ||
end | ||
else | ||
telegram_account = model_class.find_or_initialize_by(telegram_id: telegram_id) | ||
if telegram_account.user_id | ||
unless telegram_account.user_id == @user.id | ||
return nil | ||
end | ||
else | ||
telegram_account.user_id = @user.id | ||
end | ||
end | ||
telegram_account | ||
end | ||
|
||
def success(value) | ||
Result.new(true, value) | ||
end | ||
|
||
def failure(value) | ||
Result.new(false, value) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.