ソースを参照

Move up api key auth, reuse for activities

Maarten van den Berg 6 年 前
コミット
438ab9bae6
共有3 個のファイルを変更した36 個の追加23 個の削除を含む
  1. 24 11
      app/controllers/api/activities_controller.rb
  2. 1 12
      app/controllers/api/groups_controller.rb
  3. 11 0
      app/controllers/api_controller.rb

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

1
+# Provides read-only access to Activities.
1
 class Api::ActivitiesController < ApiController
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
   # GET /api/activities
15
   # GET /api/activities
7
-  # GET /api/activities.json
8
   def index
16
   def index
9
     @activities = Activity.all
17
     @activities = Activity.all
10
   end
18
   end
11
 
19
 
12
   # GET /api/activities/1
20
   # GET /api/activities/1
13
-  # GET /api/activities/1.json
14
   def show
21
   def show
15
   end
22
   end
16
 
23
 
17
   # GET /api/activities/1/response_summary
24
   # GET /api/activities/1/response_summary
18
-  # GET /api/activities/1/response_summary.json
19
   def response_summary
25
   def response_summary
20
     as = @activity
26
     as = @activity
21
       .participants
27
       .participants
78
   end
84
   end
79
 
85
 
80
   private
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
 end
99
 end

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

14
   skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
14
   skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
15
 
15
 
16
   # API key based filter (both authenticates and authorizes)
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
   # GET /api/groups
19
   # GET /api/groups
20
   def index
20
   def index
48
   def set_group
48
   def set_group
49
     @group = Group.find(params[:id])
49
     @group = Group.find(params[:id])
50
   end
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
 end
51
 end

+ 11 - 0
app/controllers/api_controller.rb

22
     end
22
     end
23
   end
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
   # Require user to be a member of group OR admin, requires @group set
36
   # Require user to be a member of group OR admin, requires @group set
26
   def require_membership!
37
   def require_membership!
27
     if !current_person.groups.include?(@group) && !current_person.is_admin?
38
     if !current_person.groups.include?(@group) && !current_person.is_admin?