Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[48619] Added validation to API token

Andreas Pfohla1dffe650a59
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 3f047b58a93..482dab08cf0 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -30,6 +30,7 @@ class MyController < ApplicationController
include PasswordConfirmation
include Accounts::UserPasswordChange
include ActionView::Helpers::TagHelper
+ include OpTurbo::ComponentStream
layout "my"
@@ -165,8 +166,8 @@ class MyController < ApplicationController
t("my.access_token.token_value_warning")
]
rescue StandardError => e
- Rails.logger.error "Failed to reset user ##{current_user.id} API key: #{e}"
- flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)
+ Rails.logger.error "Failed to create API key for user ##{current_user.id}: #{e}"
+ flash[:error] = t("my.access_token.failed_to_create_token", error: e.message)
ensure
redirect_to action: "access_token"
end
@@ -176,7 +177,7 @@ class MyController < ApplicationController
flash[:info] = t("my.access_token.notice_api_token_revoked")
rescue StandardError => e
Rails.logger.error "Failed to revoke api token ##{current_user.id}: #{e}"
- flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)
+ flash[:error] = t("my.access_token.failed_to_revoke_token", error: e.message)
ensure
redirect_to action: "access_token"
end
diff --git a/app/models/token/api.rb b/app/models/token/api.rb
index c6e4244d0ca..56ea3bc8fcf 100644
--- a/app/models/token/api.rb
+++ b/app/models/token/api.rb
@@ -28,12 +28,18 @@
module Token
class API < HashedToken
+ validate :token_name_exist
+
def name
data[:name]
end
private
+ def token_name_exist
+ errors.add(:token_name, I18n.t("activerecord.errors.messages.blank")) if data.blank? || data[:name].blank?
+ end
+
def single_value?
false
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index eb54fc06699..f0451b4e402 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -436,6 +436,8 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
new_access_token_dialog_text: "This token will allow third-party applications to communicate with your instance. To generate a new API token please introduce a token name."
new_access_token_dialog_attention_text: "Treat API tokens like passwords. Anyone with this link will have access to information from this instance, share it only with trusted users."
failed_to_reset_token: "Failed to reset access token: %{error}"
+ failed_to_create_token: "Failed to create access token: %{error}"
+ failed_to_revoke_token: "Failed to revoke access token: %{error}"
notice_reset_token: "A new %{type} token has been generated. Your access token is:"
token_value_warning: "Note: This is the only time you will see this token, make sure to copy it now."
no_results_title_text: There are currently no access tokens available.