add ApiClient#is_trusted, show list of api tokens in Workbench
[arvados.git] / services / api / app / controllers / arvados / v1 / api_client_authorizations_controller.rb
1 class Arvados::V1::ApiClientAuthorizationsController < ApplicationController
2   before_filter :current_api_client_is_trusted
3
4   protected
5
6   def find_objects_for_index
7     # Here we are deliberately less helpful about searching for client
8     # authorizations. Rather than use the generic index/where/order
9     # featuers, we look up tokens belonging to the current user and
10     # filter by exact match on api_token (which we expect in the form
11     # of a where[uuid] parameter to make things easier for API client
12     # libraries).
13     @objects = model_class.
14       includes(:user, :api_client).
15       where('user_id=? and (? or api_token=?)', current_user.id, !@where['uuid'], @where['uuid']).
16       order('created_at desc')
17   end
18
19   def find_object_by_uuid
20     # Again, to make things easier for the client and our own routing,
21     # here we look for the api_token key in a "uuid" (POST) or "id"
22     # (GET) parameter.
23     @object = model_class.where('api_token=?', params[:uuid] || params[:id]).first
24   end
25
26   def current_api_client_is_trusted
27     unless Thread.current[:api_client].andand.is_trusted
28       render :json => { errors: ['Forbidden: this API client cannot manipulate other clients\' access tokens.'] }.to_json, status: 403
29     end
30   end
31 end