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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
implemented feedback from @ulferts around ical token management and token usage
Jonas Jabari19c5b2d75573
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rbindex 229a0acf617..95d3ee71c6e 100644--- a/app/controllers/my_controller.rb+++ b/app/controllers/my_controller.rb@@ -36,6 +36,7 @@ class MyController < ApplicationControllerbefore_action :require_loginbefore_action :set_current_userbefore_action :check_password_confirmation, only: %i[update_account]+ before_action :set_ical_tokens, only: %i[access_token]menu_item :account, only: [:account]menu_item :settings, only: [:settings]@@ -130,7 +131,7 @@ class MyController < ApplicationControllerenddef revoke_all_ical_tokens- Token::Ical.where(user: current_user).destroy_all+ current_user.ical_tokens.destroy_allflash[:info] = t('my.access_token.notice_ical_tokens_reverted')rescue StandardError => eRails.logger.error "Failed to revoke all ical tokens for ##{current_user.id}: #{e}"@@ -175,7 +176,7 @@ class MyController < ApplicationControllerhelper_method :has_tokens?def has_tokens?- Setting.feeds_enabled? || Setting.rest_api_enabled? || Token::Ical.where(user: current_user).any?+ Setting.feeds_enabled? || Setting.rest_api_enabled? || current_user.ical_tokens.any?enddef user_params@@ -200,4 +201,8 @@ class MyController < ApplicationControllerdef get_current_layout@user.pref[:my_page_layout] || DEFAULT_LAYOUT.dupend++ def set_ical_tokens+ @ical_tokens = current_user.ical_tokens+ endenddiff --git a/app/models/token/ical.rb b/app/models/token/ical.rbindex 3095960b450..b66688d5687 100644--- a/app/models/token/ical.rb+++ b/app/models/token/ical.rb@@ -27,7 +27,7 @@#++module Token- class Ical < HashedToken+ class ICal < HashedToken# Prevent deleting previous tokens# Every time an ical url is generated, a new ical token will be generated for this url as well# the existing ical tokens (and thus urls) should still be validdiff --git a/app/models/user.rb b/app/models/user.rbindex ba92a09603f..965b83d1489 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -54,6 +54,12 @@ class User < Principalinverse_of: :userhas_one :rss_token, class_name: '::Token::RSS', dependent: :destroyhas_one :api_token, class_name: '::Token::API', dependent: :destroy++ # everytime a user subscribes to a calendar, a new ical_token is generated+ # unlike on other token types, all previously generated ical_tokens are kept+ # in order to keep all previously generated ical urls valid and usable+ has_many :ical_tokens, class_name: '::Token::ICal', dependent: :destroy+belongs_to :auth_source# Authorized OAuth grantsdiff --git a/app/views/my/access_token.html.erb b/app/views/my/access_token.html.erbindex ac7198b593f..77a91b702a6 100644--- a/app/views/my/access_token.html.erb+++ b/app/views/my/access_token.html.erb@@ -109,12 +109,12 @@ See COPYRIGHT and LICENSE files for more details.</tr><% end %><% end %>- <% if Token::Ical.where(user: current_user).any? %>+ <% if @ical_tokens.any? %><tr><td><%= t(:label_ical_access_key_type) %></td><td>- <span title="<%= format_time(Token::Ical.where(user: current_user).last.created_at) %>">- <%= format_time(Token::Ical.where(user: current_user).last.created_at.to_s) %> (<%= t(:label_ical_access_key_latest) %>)+ <span title="<%= format_time(@ical_tokens.last.created_at) %>">+ <%= format_time(@ical_tokens.last.created_at.to_s) %> (<%= t(:label_ical_access_key_latest) %>)</span></td><td><%= I18n.t('my_account.access_tokens.indefinite_expiration') %></td>@@ -125,6 +125,16 @@ See COPYRIGHT and LICENSE files for more details.class: 'icon icon-delete' %></td></tr>+ <% else %>+ <tr>+ <td><%= t(:label_ical_access_key_type) %></td>+ <td>+ <%= t(:label_ical_access_key_not_present) %>+ <%= t(:label_ical_access_key_generation_hint) %>+ </td>+ <td></td>+ <td></td>+ </tr><% end %><% if Setting.rest_api_enabled? %><% if @user.api_token %>diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rbindex 46c21bbd14f..20f468c39d9 100644--- a/config/initializers/zeitwerk.rb+++ b/config/initializers/zeitwerk.rb@@ -20,6 +20,8 @@ OpenProject::Inflector.rule do |basename, abspath|"#{default_inflect($1, abspath)}API"when 'api''API'+ when 'ical'+ 'ICal'when /\Aar_(.*)\z/"AR#{default_inflect($1, abspath)}"when /\Aoauth_(.*)\z/diff --git a/config/locales/en.yml b/config/locales/en.ymlindex af73dad4fad..92e6b7922a5 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -300,7 +300,7 @@ en: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.- notice_ical_tokens_reverted: All iCalendar tokens have been revoked. All previously used iCalendar URLs are now invaild.+ notice_ical_tokens_reverted: All iCalendar tokens have been revoked. All previously used iCalendar URLs are now invalid.news:index:@@ -1589,6 +1589,8 @@ en:label_api_access_key_created_on: "API access key created %{value} ago"label_api_access_key_type: "API"label_ical_access_key_type: "iCalendar"+ label_ical_access_key_not_present: "iCalendar access key(s) not present."+ label_ical_access_key_generation_hint: "Automatically generated when subscribing to a calendar."label_ical_access_key_latest: "latest"label_ical_access_key_revoke_all: "Revoke all"label_applied_status: "Applied status"diff --git a/modules/calendar/app/services/calendar/generate_ical_url.rb b/modules/calendar/app/services/calendar/generate_ical_url.rbindex 8e4d7e86adf..9d6f878bb1f 100644--- a/modules/calendar/app/services/calendar/generate_ical_url.rb+++ b/modules/calendar/app/services/calendar/generate_ical_url.rb@@ -40,7 +40,7 @@ module Calendarprotecteddef create_ical_token(user)- Token::Ical.create_and_return_value(user)+ Token::ICal.create_and_return_value(user)enddef create_ical_url(query_id, project_id, ical_token)diff --git a/modules/calendar/app/services/calendar/resolve_ical_user_service.rb b/modules/calendar/app/services/calendar/resolve_ical_user_service.rbindex 154fc905b0d..0d977b8713a 100644--- a/modules/calendar/app/services/calendar/resolve_ical_user_service.rb+++ b/modules/calendar/app/services/calendar/resolve_ical_user_service.rb@@ -34,7 +34,7 @@ module Calendarend# rubocop:disable Rails/DynamicFindBy- token = Token::Ical.find_by_plaintext_value(ical_token)+ token = Token::ICal.find_by_plaintext_value(ical_token)# rubocop:enable Rails/DynamicFindByif token.present?diff --git a/modules/calendar/spec/controllers/ical_controller_spec.rb b/modules/calendar/spec/controllers/ical_controller_spec.rbindex 807dfcbbc3d..fc76fda8d93 100644--- a/modules/calendar/spec/controllers/ical_controller_spec.rb+++ b/modules/calendar/spec/controllers/ical_controller_spec.rb@@ -36,7 +36,7 @@ describe Calendar::IcalController domember_with_permissions: sufficient_permissions)endlet(:sufficient_permissions) { %i[view_work_packages share_calendars] }- let(:valid_ical_token_value) { Token::Ical.create_and_return_value user }+ let(:valid_ical_token_value) { Token::ICal.create_and_return_value user }let(:query) docreate(:query,project:,diff --git a/modules/calendar/spec/services/resolve_ical_user_service_spec.rb b/modules/calendar/spec/services/resolve_ical_user_service_spec.rbindex 9fd070e2368..d5d2b92b38f 100644--- a/modules/calendar/spec/services/resolve_ical_user_service_spec.rb+++ b/modules/calendar/spec/services/resolve_ical_user_service_spec.rb@@ -30,7 +30,7 @@ require 'spec_helper'describe Calendar::ResolveIcalUserService, type: :model dolet(:user) { create(:user) }- let(:valid_ical_token_value) { Token::Ical.create_and_return_value user }+ let(:valid_ical_token_value) { Token::ICal.create_and_return_value user }let(:invalid_ical_token_value) { valid_ical_token_value[0..-2] }let(:instance) dodiff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rbindex 378f03262c2..6160d696900 100644--- a/spec/controllers/my_controller_spec.rb+++ b/spec/controllers/my_controller_spec.rb@@ -315,15 +315,15 @@ describe MyController do# in this context all ical tokens of a user should be reverted at once# this invalidates all previously generated ical urls, which is the intentioncontext 'with existing keys' do- let!(:key1) { Token::Ical.create user: }- let!(:key2) { Token::Ical.create user: }+ let!(:key1) { Token::ICal.create user: }+ let!(:key2) { Token::ICal.create user: }it 'revokes all existing keys' do- expect(Token::Ical.where(user_id: user.id)).to contain_exactly(key1, key2)+ expect(user.ical_tokens).to contain_exactly(key1, key2)post :revoke_all_ical_tokens- expect(Token::Ical.where(user_id: user.id).count).to eq(0)+ expect(user.ical_tokens.count).to eq(0)expect(flash[:info]).to be_presentexpect(flash[:error]).not_to be_presentdiff --git a/spec/features/users/my_spec.rb b/spec/features/users/my_spec.rbindex 188ce0c5d88..7d24a26c8de 100644--- a/spec/features/users/my_spec.rb+++ b/spec/features/users/my_spec.rb@@ -153,22 +153,27 @@ describe 'my',it 'in Access Tokens they can see if Ical tokens exists' dovisit my_access_token_path- expect(page).not_to have_content 'iCalendar'+ within(:xpath, "//tr[contains(.,'iCalendar')]") do+ expect(page).to have_content 'iCalendar access key(s) not present'+ end- new_ical_token = Token::Ical.create(user:)+ new_ical_token = Token::ICal.create(user:)visit my_access_token_path- expect(page).to have_content 'iCalendar'++ expect(page).not_to have_content 'iCalendar access key(s) not present'within(:xpath, "//tr[contains(.,'iCalendar')]") doexpect(page).to have_content "#{I18n.l(new_ical_token.created_at, format: :time)} (latest)"+ expect(page).to have_content "#{I18n.l(new_ical_token.created_at, format: :time)} (latest)"endTimecop.travel(1.minute.from_now) do- another_new_ical_token = Token::Ical.create(user:)+ another_new_ical_token = Token::ICal.create(user:)visit my_access_token_path- expect(page).to have_content 'iCalendar'++ expect(page).not_to have_content 'iCalendar access key(s) not present'within(:xpath, "//tr[contains(.,'iCalendar')]") doexpect(page).not_to have_content "#{I18n.l(new_ical_token.created_at, format: :time)} (latest)"@@ -179,7 +184,7 @@ describe 'my',it 'in Access Tokens they can revoke all existing Ical tokens' do2.times do- Token::Ical.create(user:)+ Token::ICal.create(user:)endvisit my_access_token_path@@ -191,9 +196,9 @@ describe 'my',expect(page).to have_content 'All iCalendar tokens have been revoked.'- expect(page).not_to have_xpath "//tr[contains(.,'iCalendar')]"+ expect(page).to have_content 'iCalendar access key(s) not present'- expect(Token::Ical.where(user_id: user.id).count).to eq 0+ expect(user.ical_tokens.count).to eq 0endendenddiff --git a/spec/models/token/ical_token_spec.rb b/spec/models/token/ical_token_spec.rbindex a6b47a1814d..ed30d475971 100644--- a/spec/models/token/ical_token_spec.rb+++ b/spec/models/token/ical_token_spec.rb@@ -28,7 +28,7 @@require 'spec_helper'-describe Token::Ical do+describe Token::ICal dolet(:user) { build(:user) }subject { described_class.new user: }diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex a8ea76c7918..7f6403e9aa1 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -681,6 +681,34 @@ describe User do.to eq 64endend++ describe '#ical_tokens' do+ let(:user) { create(:user) }++ it 'are not present by default' do+ expect(user.ical_tokens)+ .to be_empty+ end++ it 'returns all existing ical tokens from this user' do+ ical_token1 = Token::ICal.create(user:)+ ical_token2 = Token::ICal.create(user:)++ expect(user.ical_tokens).to contain_exactly(+ ical_token1, ical_token2+ )+ end++ it 'are destroyed when the user is destroyed' do+ Token::ICal.create(user:)+ Token::ICal.create(user:)++ user.destroy++ expect(Token::ICal.all).to be_empty+ end++ enddescribe '.newest' dolet!(:anonymous) { described_class.anonymous }