Skip to content

Commit

Permalink
FIX: user api key for webcal generation
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Feb 17, 2025
1 parent 682edd2 commit 7f0de09
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
44 changes: 19 additions & 25 deletions app/controllers/discourse_events/api_keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,26 @@ class DiscourseEvents::ApiKeysController < ApplicationController

before_action :ensure_logged_in

=begin
As soon as a new client_id is passed for the same API key, the key record
will be updated to contain the new client_id automatically.
See Auth::DefaultCurrentUserProvider#lookup_user_api_user_and_update_key
This means that rate limits could be exceeded in some cases.
TODO: Instead we should allow a unique key to be created for each client.
=end
def index
key =
UserApiKey.create! attributes.reverse_merge(
scopes: [
UserApiKeyScope.new(
name: "#{APPLICATION_NAME}:#{DiscourseEvents::USER_API_KEY_SCOPE}",
),
],
# client_id has a unique constraint
client_id: SecureRandom.uuid,
)

render json: [{ key: key.key, client_id: key.client_id }]
end

private
client = nil
key = nil
ActiveRecord::Base.transaction do
client = UserApiKeyClient.find_by(application_name: APPLICATION_NAME)
client =
UserApiKeyClient.create!(
application_name: APPLICATION_NAME,
client_id: SecureRandom.uuid,
) if client.blank?
key =
UserApiKey.create!(
user_api_key_client_id: client.id,
user_id: current_user.id,
scopes: [
UserApiKeyScope.new(name: "#{APPLICATION_NAME}:#{DiscourseEvents::USER_API_KEY_SCOPE}"),
],
)
end

def attributes
{ application_name: APPLICATION_NAME, user_id: current_user.id, revoked_at: nil }
render json: [{ key: key.key, client_id: client.client_id }]
end
end
6 changes: 2 additions & 4 deletions assets/javascripts/discourse/lib/date-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,9 @@ function eventLabel(event, args = {}) {
if (siteSettings.events_deadlines && event.deadline) {
deadline = true;
const countdownIconPending =
siteSettings.events_deadlines_countdown_icon_pending ||
"hourglass-half";
siteSettings.events_deadlines_countdown_icon_pending || "hourglass";
const countdownIconpastDue =
siteSettings.events_deadlines_countdown_icon_passed_due ||
"hourglass-end";
siteSettings.events_deadlines_countdown_icon_passed_due || "hourglass";
const countdownIcon = pastDue
? countdownIconpastDue
: countdownIconPending;
Expand Down
4 changes: 2 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ plugins:
default: false
client: true
events_deadlines_countdown_icon_pending:
default: "hourglass-half"
default: "hourglass"
client: true
events_deadlines_countdown_icon_passed_due:
default: "hourglass-end"
default: "hourglass"
client: true
events_verbose_auth_logs:
default: false
Expand Down
4 changes: 1 addition & 3 deletions plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-events
# about: Allows you to manage events in Discourse
# version: 0.9.4
# version: 0.9.5
# authors: Angus McLeod
# contact_emails: angus@angus.blog
# url: https://github.com/angusmcleod/discourse-events
Expand Down Expand Up @@ -43,8 +43,6 @@
register_svg_icon "rss"
register_svg_icon "fingerprint"
register_svg_icon "floppy-disk"
register_svg_icon "hourglass-half"
register_svg_icon "hourglass-end"
register_svg_icon "video"

require_relative "lib/discourse_events_timezone_default_site_setting.rb"
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/discourse_events/api_keys_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

describe DiscourseEvents::ApiKeysController do
fab!(:user) { Fabricate(:user, admin: true) }

before { sign_in(user) }

it "returns an api key" do
get "/discourse-events/api-keys.json"
expect(response.status).to eq(200)
expect(response.parsed_body["api_keys"].first["key"]).to be_present
expect(response.parsed_body["api_keys"].first["client_id"]).to be_present
end
end

0 comments on commit 7f0de09

Please sign in to comment.