Browse Source

Move up api key auth, reuse for activities

Maarten van den Berg 6 years ago
parent
commit
438ab9bae6

+ 24 - 11
app/controllers/api/activities_controller.rb

@@ -1,21 +1,27 @@
1
+# Provides read-only access to Activities.
1 2
 class Api::ActivitiesController < ApiController
2
-  before_action :set_activity, only: [:show, :response_summary]
3
-  before_action :require_membership!, only: [:show, :reponse_summary]
4
-  before_action :api_require_admin!, only: [:index]
3
+  has_no_activity = [:index]
4
+
5
+  # Session-based authentication/authorization
6
+  before_action :set_activity,        except: has_no_activity
7
+  before_action :require_membership!, except: has_no_activity
8
+  before_action :api_require_admin!,  only: has_no_activity
9
+  skip_before_action :api_require_authentication!, :set_activity, :require_membership!, if: 'request.authorization'
10
+
11
+  # Group API-key-based authentication/authorization
12
+  before_action :api_auth_group_token,    if: 'request.authorization'
13
+  before_action :set_activity_from_group, if: 'request.authorization'
5 14
 
6 15
   # GET /api/activities
7
-  # GET /api/activities.json
8 16
   def index
9 17
     @activities = Activity.all
10 18
   end
11 19
 
12 20
   # GET /api/activities/1
13
-  # GET /api/activities/1.json
14 21
   def show
15 22
   end
16 23
 
17 24
   # GET /api/activities/1/response_summary
18
-  # GET /api/activities/1/response_summary.json
19 25
   def response_summary
20 26
     as = @activity
21 27
       .participants
@@ -78,9 +84,16 @@ class Api::ActivitiesController < ApiController
78 84
   end
79 85
 
80 86
   private
81
-    # Use callbacks to share common setup or constraints between actions.
82
-    def set_activity
83
-      @activity = Activity.find(params[:id])
84
-      @group = @activity.group
85
-    end
87
+
88
+  # Set activity from the :id-parameter
89
+  def set_activity
90
+    @activity = Activity.find(params[:id])
91
+    @group = @activity.group
92
+  end
93
+
94
+  # Set activity from the :id-parameter, and assert that it belongs to the set @group.
95
+  def set_activity_from_group
96
+    @activity = Activity.find(params[:id])
97
+    head :unauthorized unless @activity.group == @group
98
+  end
86 99
 end

+ 1 - 12
app/controllers/api/groups_controller.rb

@@ -14,7 +14,7 @@ class Api::GroupsController < ApiController
14 14
   skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
15 15
 
16 16
   # API key based filter (both authenticates and authorizes)
17
-  before_action :api_auth_token, if: 'request.authorization'
17
+  before_action :api_auth_group_token, if: 'request.authorization'
18 18
 
19 19
   # GET /api/groups
20 20
   def index
@@ -48,15 +48,4 @@ class Api::GroupsController < ApiController
48 48
   def set_group
49 49
     @group = Group.find(params[:id])
50 50
   end
51
-
52
-  # Authenticate a request by a 'Authorization: Group xxx'-header.
53
-  # Asserts that the client meant to pass a Group API key, and then sets the
54
-  # @group variable from the key's associated group.
55
-  def api_auth_token
56
-    words = request.authorization.split(' ')
57
-    head :unauthorized unless words[0].casecmp('group').zero?
58
-
59
-    @group = Group.find_by api_token: words[1]
60
-    head :unauthorized unless @group
61
-  end
62 51
 end

+ 11 - 0
app/controllers/api_controller.rb

@@ -22,6 +22,17 @@ class ApiController < ActionController::Base
22 22
     end
23 23
   end
24 24
 
25
+  # Authenticate a request by a 'Authorization: Group xxx'-header.
26
+  # Asserts that the client meant to pass a Group API key, and then sets the
27
+  # @group variable from the key's associated group.
28
+  def api_auth_group_token
29
+    words = request.authorization.split(' ')
30
+    head :unauthorized unless words[0].casecmp('group').zero?
31
+
32
+    @group = Group.find_by api_token: words[1]
33
+    head :unauthorized unless @group
34
+  end
35
+
25 36
   # Require user to be a member of group OR admin, requires @group set
26 37
   def require_membership!
27 38
     if !current_person.groups.include?(@group) && !current_person.is_admin?