]> git.openstreetmap.org Git - rails.git/commitdiff
Make /api/0.6/permissions work for OAuth 2 clients
authorTom Hughes <tom@compton.nu>
Sat, 26 Jun 2021 23:38:16 +0000 (00:38 +0100)
committerTom Hughes <tom@compton.nu>
Sat, 26 Jun 2021 23:38:16 +0000 (00:38 +0100)
app/controllers/api/permissions_controller.rb
test/controllers/api/permissions_controller_test.rb
test/factories/oauth_access_token.rb

index 9b168e04b568e5c1f880f0f2a035a11f1dc74c44..73b84f8edf341933483a9ee68341f9a2ec94b984 100644 (file)
@@ -12,7 +12,9 @@ module Api
     # * if authenticated via basic auth all permissions are granted, so the list will contain all permissions.
     # * unauthenticated users have no permissions, so the list will be empty.
     def show
-      @permissions = if current_token.present?
+      @permissions = if doorkeeper_token.present?
+                       doorkeeper_token.scopes.map { |s| :"allow_#{s}" }
+                     elsif current_token.present?
                        ClientApplication.all_permissions.select { |p| current_token.read_attribute(p) }
                      elsif current_user
                        ClientApplication.all_permissions
index b927ca703af30e9c10907dd9b3ca27b0254caf15..3101abee2bfcab3a427924597852971c8b815059 100644 (file)
@@ -31,7 +31,7 @@ module Api
       end
     end
 
-    def test_permissions_oauth
+    def test_permissions_oauth1
       token = create(:access_token,
                      :allow_read_prefs => true,
                      :allow_write_api => true,
@@ -45,5 +45,20 @@ module Api
         assert_select "permission[name='allow_read_gpx']", :count => 0
       end
     end
+
+    def test_permissions_oauth2
+      user = create(:user)
+      token = create(:oauth_access_token,
+                     :resource_owner_id => user.id,
+                     :scopes => %w[read_prefs write_api])
+      get permissions_path, :headers => bearer_authorization_header(token.token)
+      assert_response :success
+      assert_select "osm > permissions", :count => 1 do
+        assert_select "permission", :count => 2
+        assert_select "permission[name='allow_read_prefs']", :count => 1
+        assert_select "permission[name='allow_write_api']", :count => 1
+        assert_select "permission[name='allow_read_gpx']", :count => 0
+      end
+    end
   end
 end
index c0f6245305c57b9018507cf08a8d3a1e19a33ca4..5b39cb400cf62267a046bc16787ca95e58173c20 100644 (file)
@@ -1,6 +1,5 @@
 FactoryBot.define do
   factory :oauth_access_token, :class => "Doorkeeper::AccessToken" do
-    association :resource_owner_id, :factory => :user
     association :application, :factory => :oauth_application
   end
 end