Skip to content

Commit

Permalink
Add local login strategy to develop without Keycloak
Browse files Browse the repository at this point in the history
  • Loading branch information
nymous committed Jun 23, 2024
1 parent 6a92cc4 commit f828389
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
14 changes: 14 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ def create
redirect_to user_path user
end

def create_developer # rubocop: disable Metrics/AbcSize
user = User.find_or_create_by!(

Check warning on line 12 in app/controllers/sessions_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sessions_controller.rb#L12

Added line #L12 was not covered by tests
email: params[:email]
) do |u|
u.firstname = params[:firstname]
u.lastname = params[:lastname]
u.room = params[:room]

Check warning on line 17 in app/controllers/sessions_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sessions_controller.rb#L15-L17

Added lines #L15 - L17 were not covered by tests
end
user.groups = params[:groups].split(',')
log_in user
flash[:success] = 'You are now logged in (using developer method)!'
redirect_to user

Check warning on line 22 in app/controllers/sessions_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sessions_controller.rb#L19-L22

Added lines #L19 - L22 were not covered by tests
end

def destroy
log_out
flash[:success] = 'You are now logged out!'
Expand Down
9 changes: 9 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
'aria-label': "Log in",
data: { turbo: false }
) %>
<% if Rails.env.development? %>
<%= button_to(
"Login (developer)",
"/auth/developer",
class: "login_button",
'aria-label': "Log in (developer)",
data: { turbo: false }
) %>
<% end %>
<% else %>
<%= button_to(
"Logout",
Expand Down
32 changes: 20 additions & 12 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# frozen_string_literal: true

Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
client_options = {
identifier: Rails.application.credentials.sso_id!,
secret: Rails.application.credentials.sso_secret!,
redirect_uri: "https://lea5.rezoleo.fr/#{AUTH_CALLBACK_PATH}"
}
elsif Rails.env.development?
client_options = {
identifier: Rails.application.credentials.sso_id!,
secret: Rails.application.credentials.sso_secret!,
redirect_uri: "http://127.0.0.1:3000/#{AUTH_CALLBACK_PATH}"
if Rails.env.development?
provider :developer, {
fields: [:firstname, :lastname, :email, :room, :groups]
}
end

Expand All @@ -25,6 +17,22 @@
issuer: 'https://auth.rezoleo.fr/realms/rezoleo',
discovery: true,
pkce: true,
client_options: client_options
client_options: oidc_client_options
}
end

def oidc_client_options
if Rails.env.production?
{
identifier: Rails.application.credentials.sso_id!,
secret: Rails.application.credentials.sso_secret!,
redirect_uri: "https://lea5.rezoleo.fr/#{AUTH_CALLBACK_PATH}"
}
elsif Rails.env.development?
{
identifier: Rails.application.credentials.sso_id!,
secret: Rails.application.credentials.sso_secret!,
redirect_uri: "http://127.0.0.1:3000/#{AUTH_CALLBACK_PATH}"
}
end
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
get AUTH_CALLBACK_PATH, to: 'sessions#create', as: 'auth_callback'
if Rails.env.development?
get '/auth/developer/callback', to: 'sessions#create_developer', as: 'auth_callback_developer'
end

delete '/logout', to: 'sessions#destroy', as: 'logout'
root 'static_pages#home'

Expand Down

0 comments on commit f828389

Please sign in to comment.