From 8d5f990b88e1c86ca4a2cd968a8836d3c9c1caa7 Mon Sep 17 00:00:00 2001 From: Yanick Minder <79108296+kcinay055679@users.noreply.github.com> Date: Tue, 30 Jul 2024 12:46:08 +0200 Subject: [PATCH] add new controller and add autoclick (#741) * add new controller and add autoclick * add dynamic gem install path * override keycloak strategie * select omniauth strategie based on envionment --- .env | 11 +++++++++++ app/javascript/controllers/index.js | 3 +++ .../controllers/instant_click_controller.js | 8 ++++++++ app/views/layouts/application.html.haml | 2 +- config/docker/keycloak/rails.env | 1 + config/initializers/devise.rb | 7 +++++-- docker-compose.yml | 3 ++- lib/auth_config.rb | 4 ++++ lib/skills_keycloak_omniauth_strategie.rb | 15 +++++++++++++++ 9 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 app/javascript/controllers/instant_click_controller.js create mode 100644 lib/skills_keycloak_omniauth_strategie.rb diff --git a/.env b/.env index 5ffbdb2eb..cfbb13a90 100644 --- a/.env +++ b/.env @@ -11,3 +11,14 @@ RAILS_DB_PASSWORD=skills POSTGRES_DB=skills_development POSTGRES_USER=skills POSTGRES_PASSWORD=skills + +# CONFIG GEM PATH + +# Use local gems (development only ) +# run `bundle exec gem env gemdir` to get the value of GEM_PATH + +# GEM_PATH=/home/yminder/.asdf/installs/ruby/3.3.3/lib/ruby/gems/3.3.0 + +# Use docker volume(recommended) +GEM_PATH=bundler_cache + diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 23d64230d..a9e868934 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -22,6 +22,9 @@ application.register("highlight", HighlightController) import ImageUploadController from "./image_upload_controller" application.register("image-upload", ImageUploadController) +import InstantClickController from "./instant_click_controller" +application.register("instant-click", InstantClickController) + import LangSelectionController from "./lang_selection_controller" application.register("lang-selection", LangSelectionController) diff --git a/app/javascript/controllers/instant_click_controller.js b/app/javascript/controllers/instant_click_controller.js new file mode 100644 index 000000000..09a52f31d --- /dev/null +++ b/app/javascript/controllers/instant_click_controller.js @@ -0,0 +1,8 @@ +import { Controller } from "@hotwired/stimulus" + +// Connects to data-controller="instant-click" +export default class extends Controller { + connect() { + this.element.click(); + } +} diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index cfbe451fc..9b99c097b 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -50,7 +50,7 @@ - if auth_user_signed_in? =link_to "Logout", destroy_auth_user_session_path, data: { "turbo-method": :delete}, class: "btn btn-link" - elsif devise_mapping.omniauthable? - =button_to "Login", omniauth_authorize_path(resource_name, resource_class.omniauth_providers.first), {data: { "turbo": false }, class: "btn btn-link"} + =button_to "Login", omniauth_authorize_path(resource_name, resource_class.omniauth_providers.first), {data: { "turbo": false, controller: "instant-click"}, class: "btn btn-link"} -# Help %li.d-flex.align-items-center.cursor-pointer.ps-2.pe-2.border-start.h-100 %a.d-flex.align-items-center{:href => "https://github.com/puzzle/skills/issues"} diff --git a/config/docker/keycloak/rails.env b/config/docker/keycloak/rails.env index ca53ff47d..b638787eb 100644 --- a/config/docker/keycloak/rails.env +++ b/config/docker/keycloak/rails.env @@ -5,5 +5,6 @@ ADMIN_ROLE=ADMIN REALM=pitc HOST_URL=http://keycloak:8080 +KEYCLOAK_REDIRECT_HOST_URL=http://localhost:8080 CLIENT_ID=pitc-skills-dev SECRET=Hr3e9BSzyfRSkUksFkgLgFgIhK91aFPk diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 084468091..6c77f0ec0 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require 'auth_config' +require 'skills_keycloak_omniauth_strategie' + # Assuming you have not yet modified this file, each configuration option below # is set to its default value. Note that some are commented out while others # are not: uncommented lines are intended to protect your configuration from @@ -273,7 +275,6 @@ # Add a new OmniAuth provider. Check the wiki for more information on setting # up on your models and hooks. # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' - config.omniauth :keycloak_openid, { name: :keycloak_openid, scope: [:openid, :email], @@ -283,9 +284,11 @@ client_options: { site: AuthConfig.host_url, realm: AuthConfig.realm, + keycloak_redirect_site: AuthConfig.keycloak_redirect_host_url, }, - strategy_class: OmniAuth::Strategies::KeycloakOpenId, + strategy_class: Rails.env.development? ? SkillsKeycloakOmniauthStrategie: OmniAuth::Strategies::KeycloakOpenId, } + OmniAuth.config.logger = Rails.logger if Rails.env.development? # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/docker-compose.yml b/docker-compose.yml index cedb92fef..431233595 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,8 @@ services: - ./tmp/seed:/seed - ./config/docker/development/home/rails:/home/developer - /tmp/.X11-unix:/tmp/.X11-unix - - bundler_cache:/opt/bundle + - ${GEM_PATH}:/opt/bundle + depends_on: - postgres diff --git a/lib/auth_config.rb b/lib/auth_config.rb index 5fb312843..66f0adbb7 100644 --- a/lib/auth_config.rb +++ b/lib/auth_config.rb @@ -21,6 +21,10 @@ def host_url get_var_from_environment(:host_url) end + def keycloak_redirect_host_url + get_var_from_environment(:keycloak_redirect_host_url, default: host_url) + end + def realm get_var_from_environment(:realm) end diff --git a/lib/skills_keycloak_omniauth_strategie.rb b/lib/skills_keycloak_omniauth_strategie.rb new file mode 100644 index 000000000..6e7d4fa32 --- /dev/null +++ b/lib/skills_keycloak_omniauth_strategie.rb @@ -0,0 +1,15 @@ +require 'omniauth-keycloak' +require 'oauth2' + + +class SkillsKeycloakOmniauthStrategie < OmniAuth::Strategies::KeycloakOpenId + + + def request_phase # rubocop:disable Metrics/AbcSize + options.authorize_options.each { |key| options[key] = request.params[key.to_s] } + url = client.auth_code.authorize_url({ :redirect_uri => callback_url }.merge(authorize_params)) + url = url.gsub(options.client_options[:site], + options.client_options[:keycloak_redirect_site]) + redirect url + end +end