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