Skip to content

Commit

Permalink
app: Redirect to cockpit
Browse files Browse the repository at this point in the history
Redirects the user to cockpit including a valid API
token as a url fragment so that cockpit can validate the
current user.
  • Loading branch information
petervo committed Jul 5, 2017
1 parent cca20e1 commit ff78ac6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/controllers/dashboard_controller.rb
Expand Up @@ -18,6 +18,37 @@ def index
redirect_to :action => 'show'
end

def current_hostname
return URI.parse(request.env['HTTP_X_FORWARDED_FOR']).hostname if request.env['HTTP_X_FORWARDED_FOR']
URI.parse(request.original_url).hostname
end

def known_redirect_host?(hostname)
MiqServer.active_miq_servers.where(:has_active_cockpit_ws => true).each do |server|
return true if hostname == server.hostname
return true if hostname == server.ipaddress
settings = MiqCockpitWsWorker.fetch_worker_settings_from_server(server)
settings_host = URI.parse(settings[:external_url]).hostname if settings[:external_url]
return true if hostname == settings_host
end
false
end

# Redirect to cockpit with an api auth token
def cockpit_redirect
return head(:forbidden) unless params[:redirect_uri]

# We require that redirect hostname matches current host
# or is known as a miq_server
url = URI.parse(params[:redirect_uri])
if current_hostname != url.hostname && !url.hostname.nil?
return head(:forbidden) unless known_redirect_host?(url.hostname)
end

url.fragment = "access_token=#{generate_ui_api_token(current_user[:userid])}"
redirect_to url.to_s
end

def saml_protected_page
request.base_url + '/saml_login'
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -1072,6 +1072,7 @@
:dashboard => {
:get => %w(
auth_error
cockpit_redirect
iframe
change_tab
index
Expand Down

0 comments on commit ff78ac6

Please sign in to comment.