add authorized_keys
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
4   before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
5
6   unless Rails.application.config.consider_all_requests_local
7     rescue_from Exception,
8     :with => :render_exception
9     rescue_from ActiveRecord::RecordNotFound,
10     :with => :render_not_found
11     rescue_from ActionController::RoutingError,
12     :with => :render_not_found
13     rescue_from ActionController::UnknownController,
14     :with => :render_not_found
15     rescue_from ActionController::UnknownAction,
16     :with => :render_not_found
17   end
18
19   def unprocessable(message=nil)
20     @errors ||= []
21     @errors << message if message
22     render_error status: 422
23   end
24
25   def render_error(opts)
26     respond_to do |f|
27       f.html { render opts.merge(controller: 'application', action: 'error') }
28       f.json { render opts.merge(json: {success: false, errors: @errors}) }
29     end
30   end
31
32   def render_exception(e)
33     logger.error e.inspect
34     logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
35     if @object and @object.errors and @object.errors.full_messages
36       @errors = @object.errors.full_messages
37     else
38       @errors = [e.inspect]
39     end
40     self.render_error status: 422
41   end
42
43   def render_not_found(e=ActionController::RoutingError.new("Path not found"))
44     logger.error e.inspect
45     @errors = ["Path not found"]
46     self.render_error status: 404
47   end
48
49
50   def index
51     @objects ||= model_class.all
52     respond_to do |f|
53       f.json { render json: @objects }
54       f.html { render }
55     end
56   end
57
58   def show
59     if !@object
60       return render_not_found("object not found")
61     end
62     respond_to do |f|
63       f.json { render json: @object }
64       f.html { render }
65     end
66   end
67
68   def current_user
69     if Thread.current[:arvados_api_token]
70       @current_user ||= User.current
71     else
72       logger.error "No API token in Thread"
73       return nil
74     end
75   end
76
77   protected
78     
79   def model_class
80     controller_name.classify.constantize
81   end
82
83   def find_object_by_uuid
84     if params[:id] and params[:id].match /\D/
85       params[:uuid] = params.delete :id
86     end
87     @object = model_class.where(uuid: params[:uuid]).first
88   end
89
90   def thread_with_api_token
91     begin
92       try_redirect_to_login = true
93       if params[:api_token]
94         try_redirect_to_login = false
95         Thread.current[:arvados_api_token] = params[:api_token]
96         # Before copying the token into session[], do a simple API
97         # call to verify its authenticity.
98         if verify_api_token
99           session[:arvados_api_token] = params[:api_token]
100           if !request.format.json? and request.method == 'GET'
101             # Repeat this request with api_token in the (new) session
102             # cookie instead of the query string.  This prevents API
103             # tokens from appearing in (and being inadvisedly copied
104             # and pasted from) browser Location bars.
105             redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
106           else
107             yield
108           end
109         else
110           @errors = ['Invalid API token']
111           self.render_error status: 401
112         end
113       elsif session[:arvados_api_token]
114         # In this case, the token must have already verified at some
115         # point, but it might have been revoked since.  We'll try
116         # using it, and catch the exception if it doesn't work.
117         try_redirect_to_login = false
118         Thread.current[:arvados_api_token] = session[:arvados_api_token]
119         begin
120           yield
121         rescue ArvadosApiClient::NotLoggedInException
122           try_redirect_to_login = true
123         end
124       end
125       if try_redirect_to_login
126         respond_to do |f|
127           f.html {
128             redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
129           }
130           f.json {
131             @errors = ['No API token supplied -- can\'t really do anything.']
132             self.render_error status: 422
133           }
134         end
135       end
136     ensure
137       # Remove token in case this Thread is used for anything else.
138       Thread.current[:arvados_api_token] = nil
139     end
140   end
141
142   def verify_api_token
143     begin
144       Link.where(uuid: 'just-verifying-my-api-token')
145       true
146     rescue ArvadosApiClient::NotLoggedInException
147       false
148     end
149   end
150
151   def ensure_current_user_is_admin
152     unless current_user and current_user.is_admin
153       @errors = ['Permission denied']
154       self.render_error status: 401
155     end
156   end
157 end