require 'hws_settings' require 'json' require 'p4_error' require 'p4_util' require 'sinatra/base' module Auth # Authentication interface for helix web services class App < Sinatra::Base # This method allows people to generate a host unlocked p4 ticket # # One subtle behavior of p4 login -p, is that it will reuse any token # lying in a p4ticket file. What this means, is that if you sign on, say, # using the p4 command line, this method will end up reusing and returning # that ticket. # # So, make sure you run your web services instance in a clean environment # that you don't do interactive work (like maintenance stuff) under. post '/auth/v1/login' do user = params[:user] password = params[:password] # may be a p4 ticket ticket = nil if HWSSettings.system.ENABLE_HELIX_CLOUD_AUTH # TODO(HelixCloud) else env['p4'] = P4Util.open_from_env(env) p4 = env['p4'] p4.user = user p4.password = password p4.connect ticket = Auth.ticket_from_login(p4) end halt 403 if ticket.nil? if request.env['HTTP_ACCEPT'] =~ /application\/json/ content_type 'application/json' return { ticket: ticket }.to_json else content_type 'text/plain' return ticket end end end end