# Do NOT use ApplicationController here.
class OktaController < ActionController::Base
  include ExternalSitesHelper

  protect_from_forgery with: :exception

  def callback
    id_info = auth_hash["extra"]["id_info"]
    session["okta_net_id"] = id_info["netid"] || id_info["sub"].split("@").first
    Rails.logger.info "Authenticated #{session["okta_net_id"]}, returning " \
                      "user to #{request_origin}."
    redirect_to request_origin
  end

  private

  def request_origin
    request.env["omniauth.origin"] || "/"
  end

  def auth_hash
    request.env["omniauth.auth"]
  end
end
