0

OK i'm not really a ROR person but I'm trying to modify an existing app (forked) from https://github.com/adamcooke/staytus

there's an /admin page which right now brings you to a haml login form with a username/password box.

What I'm attempting to do us change this functionally so that when /admin is hit devise and omniAuth will redirect to my IDP via OpenIDConnect ask the user to login with their creds, do the auth stuff and pending they get through then show the admin section...

here's what I've done thus far: installed these gems

gem 'devise'
gem 'omniauth-rails_csrf_protection'
gem 'omniauth-azure-activedirectory-v2'

ran the devise config/install:

added config.rb

  def omniauth_oidc?
    result = ENV['OMNIAUTH_OIDC'] == '1'
    puts "omniauth_oidc? result: #{result}"

I've tried all combinations of routes:

 devise_scope :user do
    devise_for :users, controllers: { omniauth_callbacks: 'admin/omniauth_callbacks' }
  end


  devise_scope :user do
    get '/admin/omniauth_callbacks' => 'admin/omniauth_callbacks#azure_activedirectory_v2'
  end


  namespace :admin do
    get '/omniauth_callbacks', to: 'omniauth_callbacks#azure_activedirectory_v2'
  end
    result
  end

I've also tried " to: 'sessions#create'" routes but clearly I'm missing something here...

added OmniauthCallbacksControler

class Admin::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def azure_activedirectory_v2
    puts "request.env['omniauth.auth']: #{request.env['omniauth.auth'].inspect}"
    response_params = request.env['omniauth.auth']
    if response_params.nil?
      Rails.logger.error("request.env['omniauth.auth'] is nil")
      raise "request.env['omniauth.auth'] is nil"
    else
      response_params = response_params['info']
    end
    @user = User.find_by!(email: response_params['email'])

    if @user&.persisted?
      sign_in_and_redirect @user, event: :authentication, location: admin_root_path
    else
      flash[:danger] = 'You have not yet an account!'
      redirect_back(fallback_location: admin_root_path)
    end
  end
  end

added omniauth.rb initializer

Rails.application.config.middleware.use OmniAuth::Builder do provider :developer if Rails.env.development? provider :azure_activedirectory_v2, { client_id: ENV[''], client_secret: ENV[''], tenant_id: ENV['] } end

given all of the above I still haven't gotten /admin to redirect to my IDP login ? WHY ?

Tony
  • 8,681
  • 7
  • 36
  • 55

0 Answers0