8079: Added support get using uuid and list using uuid or api_token and added tests.
[arvados.git] / services / api / app / controllers / arvados / v1 / api_client_authorizations_controller.rb
1 class Arvados::V1::ApiClientAuthorizationsController < ApplicationController
2   accept_attribute_as_json :scopes, Array
3   before_filter :current_api_client_is_trusted
4   before_filter :admin_required, :only => :create_system_auth
5   skip_before_filter :render_404_if_no_object, :only => :create_system_auth
6
7   def self._create_system_auth_requires_parameters
8     {
9       api_client_id: {type: 'integer', required: false},
10       scopes: {type: 'array', required: false}
11     }
12   end
13   def create_system_auth
14     @object = ApiClientAuthorization.
15       new(user_id: system_user.id,
16           api_client_id: params[:api_client_id] || current_api_client.andand.id,
17           created_by_ip_address: remote_ip,
18           scopes: Oj.load(params[:scopes] || '["all"]'))
19     @object.save!
20     show
21   end
22
23   def create
24     # Note: the user could specify a owner_uuid for a different user, which on
25     # the surface appears to be a security hole.  However, the record will be
26     # rejected before being saved to the database by the ApiClientAuthorization
27     # model which enforces that user_id == current user or the user is an
28     # admin.
29
30     if resource_attrs[:owner_uuid]
31       # The model has an owner_id attribute instead of owner_uuid, but
32       # we can't expect the client to know the local numeric ID. We
33       # translate UUID to numeric ID here.
34       resource_attrs[:user_id] =
35         User.where(uuid: resource_attrs.delete(:owner_uuid)).first.andand.id
36     elsif not resource_attrs[:user_id]
37       resource_attrs[:user_id] = current_user.id
38     end
39     resource_attrs[:api_client_id] = Thread.current[:api_client].id
40     super
41   end
42
43   protected
44
45   def default_orders
46     ["#{table_name}.created_at desc"]
47   end
48
49   def find_objects_for_index
50     # Here we are deliberately less helpful about searching for client
51     # authorizations.  We look up tokens belonging to the current user
52     # and filter by exact matches on api_token and scopes.
53     wanted_scopes = []
54     if @filters
55       wanted_scopes.concat(@filters.map { |attr, operator, operand|
56         ((attr == 'scopes') and (operator == '=')) ? operand : nil
57       })
58       @filters.select! { |attr, operator, operand|
59         ((attr == 'uuid') and (operator == '=')) || ((attr == 'api_token') and (operator == '='))
60       }
61     end
62     if @where
63       wanted_scopes << @where['scopes']
64       @where.select! { |attr, val| attr == 'uuid' }
65     end
66     @objects = model_class.
67       includes(:user, :api_client).
68       where('user_id=?', current_user.id)
69     super
70     wanted_scopes.compact.each do |scope_list|
71       sorted_scopes = scope_list.sort
72       @objects = @objects.select { |auth| auth.scopes.sort == sorted_scopes }
73     end
74   end
75
76   def find_object_by_uuid
77     @object = model_class.where(uuid: (params[:uuid] || params[:id])).first
78   end
79
80   def current_api_client_is_trusted
81     unless Thread.current[:api_client].andand.is_trusted
82       if params["action"] == "show"
83         if @object and @object['api_token'] == current_api_client_authorization.andand.api_token
84           return true
85         end
86       elsif params["action"] == "index" and @objects.andand.size == 1
87         filters = @filters.map{|f|f.first}.uniq
88         if ['uuid'] == filters
89           return true if @objects.first['api_token'] == current_api_client_authorization.andand.api_token
90         elsif ['api_token'] == filters
91           return true if @objects.first[:user_id] = current_user.id
92         end
93       end
94       send_error('Forbidden: this API client cannot manipulate other clients\' access tokens.',
95                  status: 403)
96     end
97   end
98 end