keycloak_oauth 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/app/controllers/keycloak_oauth/callbacks_controller.rb +13 -2
- data/app/services/keycloak_oauth/authentication_service.rb +7 -3
- data/lib/keycloak_oauth/endpoints.rb +2 -0
- data/lib/keycloak_oauth/version.rb +1 -1
- metadata +2 -3
- data/app/controllers/keycloak_oauth/application_controller.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1700a23b09d7b728852b88e10c29de14be07c00c34a04f9342ec922c16b375b8
|
4
|
+
data.tar.gz: ae133f07b837f475631206d6dd3e3fbaa1c0807d00adf073e20e178c042e64bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3528f127017bc5b92e01327483d7d455007dea1282620eb2b7e769550d63127c3f823104fc0bcf2f10482222ddc45816f6a84488e2dadcc2a864ea0ce1f256ea
|
7
|
+
data.tar.gz: 7e41f37021b9663a1c3f50e3dc151b5891a23400ac90252c36b91a2869ea1754ee1ae72581d43199b0c217378cbdab39bfca503b88a68044a0e9db7e7a5dd0bb
|
data/README.md
CHANGED
@@ -44,6 +44,16 @@ e.g.
|
|
44
44
|
|
45
45
|
Once authentication is performed, the access and refresh tokens are stored in the session and can be used in your app as wished.
|
46
46
|
|
47
|
+
***Customising redirect URIs***
|
48
|
+
There are situations where you would want to customise the oauth2 route (e.g. to use a localised version of the callback URL).
|
49
|
+
In this case, you can do the following:
|
50
|
+
- add a controller to your app: e.g. `CallbackOverrides`
|
51
|
+
- add the following to your routes.rb file: `get 'oauth2', to: 'callback_overrides#oauth2'`
|
52
|
+
- add whatever logic you need in the controller, e.g. a `skip_before_action`; it can also be blank
|
53
|
+
- add redirect URI to the authorization link:
|
54
|
+
e.g.
|
55
|
+
`<%= link_to 'Login with Keycloak', KeycloakOauth.connection.authorization_endpoint(options: {redirect_uri: 'http://myapp.com/en/oauth2'}) %>`
|
56
|
+
|
47
57
|
**Keycloak callback URL**
|
48
58
|
Keycloak needs a callback URL to send the authorization code to once a user logs in.
|
49
59
|
By default, once authentication is performed, we redirect to the `/` path (i.e. whatever the root path is set to in the host app).
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module KeycloakOauth
|
2
|
-
class CallbacksController < ApplicationController
|
2
|
+
class CallbacksController < ::ApplicationController
|
3
3
|
if KeycloakOauth.connection.callback_module.present?
|
4
4
|
include KeycloakOauth.connection.callback_module
|
5
5
|
end
|
@@ -7,7 +7,8 @@ module KeycloakOauth
|
|
7
7
|
def oauth2
|
8
8
|
authentication_service = KeycloakOauth::AuthenticationService.new(
|
9
9
|
authentication_params: authentication_params,
|
10
|
-
session: session
|
10
|
+
session: session,
|
11
|
+
redirect_uri: current_uri_without_params
|
11
12
|
)
|
12
13
|
authentication_service.authenticate
|
13
14
|
map_authenticatable_if_implemented(session)
|
@@ -28,5 +29,15 @@ module KeycloakOauth
|
|
28
29
|
raise NotImplementedError.new('User mapping must be handled by the host app. See README for more information.')
|
29
30
|
end
|
30
31
|
end
|
32
|
+
|
33
|
+
def current_uri_without_params
|
34
|
+
# If the host app has overwritten the route (e.g. to enable localised
|
35
|
+
# callbacks), this ensures we are using the path coming from the host app
|
36
|
+
# instead of the one coming from the engine.
|
37
|
+
main_app.url_for(only_path: false, overwrite_params: nil)
|
38
|
+
rescue ActionController::UrlGenerationError
|
39
|
+
# If the host app does not override the oauth2 path, use the engine's path.
|
40
|
+
oauth2_path
|
41
|
+
end
|
31
42
|
end
|
32
43
|
end
|
@@ -9,11 +9,12 @@ module KeycloakOauth
|
|
9
9
|
ACCESS_TOKEN_KEY = 'access_token'.freeze
|
10
10
|
REFRESH_TOKEN_KEY = 'refresh_token'.freeze
|
11
11
|
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :session
|
13
13
|
|
14
|
-
def initialize(authentication_params:, session:)
|
14
|
+
def initialize(authentication_params:, session:, redirect_uri:)
|
15
15
|
@code = authentication_params[:code]
|
16
16
|
@session = session
|
17
|
+
@redirect_uri = redirect_uri
|
17
18
|
end
|
18
19
|
|
19
20
|
def authenticate
|
@@ -22,6 +23,8 @@ module KeycloakOauth
|
|
22
23
|
|
23
24
|
private
|
24
25
|
|
26
|
+
attr_reader :code, :redirect_uri
|
27
|
+
|
25
28
|
def get_tokens
|
26
29
|
uri = URI.parse(KeycloakOauth.connection.authentication_endpoint)
|
27
30
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
@@ -37,7 +40,8 @@ module KeycloakOauth
|
|
37
40
|
client_id: KeycloakOauth.connection.client_id,
|
38
41
|
client_secret: KeycloakOauth.connection.client_secret,
|
39
42
|
grant_type: GRANT_TYPE,
|
40
|
-
code: code
|
43
|
+
code: code,
|
44
|
+
redirect_uri: redirect_uri
|
41
45
|
}
|
42
46
|
end
|
43
47
|
|
@@ -5,6 +5,8 @@ module KeycloakOauth
|
|
5
5
|
def authorization_endpoint(options: {})
|
6
6
|
endpoint = "#{auth_url}/realms/#{realm}/protocol/openid-connect/auth?client_id=#{client_id}"
|
7
7
|
endpoint += "&response_type=#{options[:response_type] || DEFAULT_RESPONSE_TYPE}"
|
8
|
+
endpoint += "&redirect_uri=#{options[:redirect_uri]}" if options[:redirect_uri].present?
|
9
|
+
endpoint
|
8
10
|
end
|
9
11
|
|
10
12
|
def authentication_endpoint
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keycloak_oauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- simplificator
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -67,7 +67,6 @@ extra_rdoc_files: []
|
|
67
67
|
files:
|
68
68
|
- README.md
|
69
69
|
- Rakefile
|
70
|
-
- app/controllers/keycloak_oauth/application_controller.rb
|
71
70
|
- app/controllers/keycloak_oauth/callbacks_controller.rb
|
72
71
|
- app/services/keycloak_oauth/authentication_service.rb
|
73
72
|
- app/services/keycloak_oauth/user_info_retrieval_service.rb
|