Skip to content

Commit

Permalink
add new controller and add autoclick (#741)
Browse files Browse the repository at this point in the history
* add new controller and add autoclick

* add dynamic gem install path

* override keycloak strategie

* select omniauth strategie based on envionment
  • Loading branch information
kcinay055679 authored Jul 30, 2024
1 parent e15ddf9 commit 8d5f990
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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

3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions app/javascript/controllers/instant_click_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="instant-click"
export default class extends Controller {
connect() {
this.element.click();
}
}
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
1 change: 1 addition & 0 deletions config/docker/keycloak/rails.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/auth_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/skills_keycloak_omniauth_strategie.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8d5f990

Please sign in to comment.