keycloak_ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "omniauth"
4
+ require "omniauth_openid_connect"
5
+ require "httparty"
6
+ require "jwt"
7
+
8
+ # lib/keycloak_ruby.rb
9
+ require "generators/keycloak_ruby/install_generator" if defined?(Rails)
10
+ require "keycloak_ruby/authentication"
11
+ require "keycloak_ruby/client"
12
+ require "keycloak_ruby/config"
13
+ require "keycloak_ruby/errors"
14
+ require "keycloak_ruby/request_params"
15
+ require "keycloak_ruby/request_performer"
16
+ require "keycloak_ruby/response_validator"
17
+ require "keycloak_ruby/token_refresher"
18
+ require "keycloak_ruby/token_service"
19
+ require "keycloak_ruby/user"
20
+ require "keycloak_ruby/version"
21
+
22
+ # Module for interacting with Keycloak
23
+ module KeycloakRuby
24
+ class << self
25
+ # Logger used throughout the gem
26
+ #
27
+ # Defaults to Rails.logger if available, or a standard Logger.
28
+ #
29
+ # @return [Logger]
30
+ attr_writer :logger
31
+
32
+ def logger
33
+ @logger ||= if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
34
+ Rails.logger
35
+ else
36
+ require "logger"
37
+ Logger.new($stdout).tap { |log| log.level = Logger::INFO }
38
+ end
39
+ end
40
+
41
+ # Returns the singleton configuration object. The configuration is
42
+ # initialized on first access and validated immediately.
43
+ #
44
+ # @return [KeycloakRuby::Config] the configuration object
45
+ def config
46
+ @config ||= Config.new.tap(&:validate!)
47
+ end
48
+
49
+ # Yields the configuration object for block-based configuration.
50
+ # Validates the configuration after the block executes.
51
+ #
52
+ # @yield [KeycloakRuby::Config] the configuration object
53
+ # @raise [ConfigurationError] if configuration is invalid
54
+ def configure
55
+ yield config
56
+ config.validate!
57
+ end
58
+ end
59
+ VERSION = Version::VERSION
60
+ # Only load test helpers when in test environment
61
+ # Load test helpers only in test environment
62
+ if ENV["RACK_ENV"] == "test" || ENV["RAILS_ENV"] == "test" || defined?(RSpec) || defined?(Minitest)
63
+ require "keycloak_ruby/testing"
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/templates/omniauth.rb
4
+ require "keycloak_ruby"
5
+
6
+ realm_url = KeycloakRuby.config.realm_url
7
+ keycloak_url = URI.parse(KeycloakRuby.config.keycloak_url)
8
+ keycloak_config = KeycloakRuby.config
9
+ Rails.application.config.middleware.use OmniAuth::Builder do
10
+ provider :openid_connect, {
11
+ name: :keycloak,
12
+ issuer: realm_url,
13
+ scope: %i[openid email profile],
14
+ response_type: :code,
15
+ uid_field: "sub",
16
+ client_options: {
17
+ scheme: "http",
18
+ host: keycloak_url.host,
19
+ port: keycloak_url.port,
20
+ identifier: keycloak_config.oauth_client_id,
21
+ secret: keycloak_config.oauth_client_secret,
22
+ redirect_uri: keycloak_config.redirect_url,
23
+ authorization_endpoint: "#{realm_url}/protocol/openid-connect/auth",
24
+ token_endpoint: "#{realm_url}/protocol/openid-connect/token",
25
+ userinfo_endpoint: "#{realm_url}/protocol/openid-connect/userinfo",
26
+ jwks_uri: "#{realm_url}/protocol/openid-connect/certs"
27
+ }
28
+ }
29
+ end