]> git.openstreetmap.org Git - rails.git/blob - lib/tasks/register_oauth_apps.rake
Merge remote-tracking branch 'upstream/pull/5851'
[rails.git] / lib / tasks / register_oauth_apps.rake
1 namespace :oauth do
2   desc "Register the built-in apps with specified user as owner; append necessary changes to settings file"
3   task :register_apps, [:display_name] => :environment do |task, args|
4     already_defined_keys = [:id_application, :oauth_application, :oauth_key] & Settings.keys
5     settings_filename = Rails.root.join("config/settings.local.yml")
6     settings_file_exists = File.file?(settings_filename)
7
8     unless already_defined_keys.empty?
9       if settings_file_exists
10         puts "The following settings are already defined (likely in #{settings_filename}):"
11       else
12         puts "The following settings are already defined:"
13       end
14       already_defined_keys.each do |key|
15         puts "- #{key}"
16       end
17       puts ""
18       puts "Delete or comment them out to proceed."
19
20       raise "App settings already defined"
21     end
22
23     user = User.find_by! :display_name => args.display_name
24
25     model = Doorkeeper.config.application_model
26
27     id_application = model.create :name => "Local iD",
28                                   :redirect_uri => "http://localhost:3000",
29                                   :scopes => %w[read_prefs write_prefs write_api read_gpx write_gpx write_notes],
30                                   :confidential => false,
31                                   :owner => user
32
33     web_application = model.create :name => "OpenStreetMap Web Site",
34                                    :redirect_uri => "http://localhost:3000",
35                                    :scopes => %w[write_api write_notes],
36                                    :owner => user
37
38     File.open settings_filename, "a" do |file|
39       file << "\n" if settings_file_exists
40       file << <<~CUT
41         # generated by #{task} rake task
42         # OAuth 2 Client ID for iD
43         id_application: "#{id_application.uid}"
44         # OAuth 2 Client ID for the web site
45         oauth_application: "#{web_application.uid}"
46         # OAuth 2 Client Secret for the web site
47         oauth_key: "#{web_application.plaintext_secret}"
48       CUT
49     end
50
51     puts "Updated settings in #{settings_filename}"
52   end
53 end