From 4116ebf421f853a055880c04a00310a71586b3f0 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sun, 13 Jul 2025 19:29:01 +0300 Subject: [PATCH] Add a task to register iD and web OAuth apps --- CONFIGURE.md | 13 ++++++++ lib/tasks/register_oauth_apps.rake | 53 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/tasks/register_oauth_apps.rake diff --git a/CONFIGURE.md b/CONFIGURE.md index 815cf92f6..cc4daf02f 100644 --- a/CONFIGURE.md +++ b/CONFIGURE.md @@ -57,6 +57,19 @@ There are two built-in applications which communicate via the API, and therefore * iD * The website itself (for the Notes functionality) +You can register them by running the following rake task: + +``` +bundle exec rails oauth:register_apps["My New User Name"] +``` + +This task registers the applications with the "My New User Name" user as the owner +and saves their keys to `config/settings.local.yml`. When logged in, the owner should be able to see the apps on the [OAuth 2 applications](http://127.0.0.1:3000/oauth2/applications) page. + +Alternatively you can register the applications manually, as described in the next section. + +### Manually registering the build-in OAuth applications + For iD, do the following: * Go to "[OAuth 2 applications](http://localhost:3000/oauth2/applications)" on the My settings page. diff --git a/lib/tasks/register_oauth_apps.rake b/lib/tasks/register_oauth_apps.rake new file mode 100644 index 000000000..8be6c9b4d --- /dev/null +++ b/lib/tasks/register_oauth_apps.rake @@ -0,0 +1,53 @@ +namespace :oauth do + desc "Register the built-in apps with specified user as owner; append necessary changes to settings file" + task :register_apps, [:display_name] => :environment do |task, args| + already_defined_keys = [:id_application, :oauth_application, :oauth_key] & Settings.keys + settings_filename = Rails.root.join("config/settings.local.yml") + settings_file_exists = File.file?(settings_filename) + + unless already_defined_keys.empty? + if settings_file_exists + puts "The following settings are already defined (likely in #{settings_filename}):" + else + puts "The following settings are already defined:" + end + already_defined_keys.each do |key| + puts "- #{key}" + end + puts "" + puts "Delete or comment them out to proceed." + + raise "App settings already defined" + end + + user = User.find_by! :display_name => args.display_name + + model = Doorkeeper.config.application_model + + id_application = model.create :name => "Local iD", + :redirect_uri => "http://localhost:3000", + :scopes => %w[read_prefs write_prefs write_api read_gpx write_gpx write_notes], + :confidential => false, + :owner => user + + web_application = model.create :name => "OpenStreetMap Web Site", + :redirect_uri => "http://localhost:3000", + :scopes => %w[write_api write_notes], + :owner => user + + File.open settings_filename, "a" do |file| + file << "\n" if settings_file_exists + file << <<~CUT + # generated by #{task} rake task + # OAuth 2 Client ID for iD + id_application: "#{id_application.uid}" + # OAuth 2 Client ID for the web site + oauth_application: "#{web_application.uid}" + # OAuth 2 Client Secret for the web site + oauth_key: "#{web_application.plaintext_secret}" + CUT + end + + puts "Updated settings in #{settings_filename}" + end +end -- 2.39.5