keycloak_oauth 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c4e4bd1db90c3f3a2b6b16c41d339d50210d01052d1de2473a6c7dd99736e2f
4
- data.tar.gz: 43cd08c9c3a233a307e1b87f8b8e842e0949d1c8cfe29a6f48ba4741d22dd6f2
3
+ metadata.gz: d5372c486b89d09a51766f5ae6420c926f23d26d3f83ed30a9cd8b62c0c100a9
4
+ data.tar.gz: fcc3d36bbcdcd9d3fda6d51ebfb74e227cbb1f3eb234dfabd98531362c0be8e2
5
5
  SHA512:
6
- metadata.gz: 8a3f24340b2967be6a3e0a3a7db140f74bf8a3b68ff73e933c1fa48b7975d18bf266c3b3663033270c3fd7ea6ab28dec88ae8b4806b218fe030dd4b9efb45a13
7
- data.tar.gz: 508d3abe88044f5adcfdbbe47f08be2a7328d26d8a33acfbe31f8aa42213c54ec8042881a929401493d9ecc88ad6ac0549e150515deeed2c3004d751573f19fe
6
+ metadata.gz: ea374a1c1d6fa04d4473cc645be6af6bfe0d9bb3468924a238cae97ee990c339bbd16d99c8c0173c736b48f0cb5d3c8758cae059dd3d12713f872d6a12ca2dfd
7
+ data.tar.gz: 95eb399aac689d154fb942521da92a557a0b5fc7f035c7798fb48b3c2e3e1c1a44a2548724cfcb5c96d689d8e6b491f75ca028fb6ad3e356174ae76a8b8562da
data/README.md CHANGED
@@ -88,6 +88,22 @@ def map_authenticatable(_request)
88
88
  end
89
89
  ```
90
90
 
91
+ **Logging out**
92
+ In order to log out, you can use the following API call:
93
+ `KeycloakOauth.connection.logout(session: session)`
94
+
95
+ Note that you need to pass in the session, as the gem needs to remove the Keycloak tokens from there.
96
+
97
+ e.g.
98
+ ```ruby
99
+ class SessionsController < ApplicationController
100
+ def destroy
101
+ KeycloakOauth.connection.logout(session: session)
102
+ redirect_to new_session_path
103
+ end
104
+ end
105
+ ```
106
+
91
107
  ## Development
92
108
 
93
109
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,24 @@
1
+ require 'net/http'
2
+
3
+ module KeycloakOauth
4
+ class AuthorizableError < StandardError; end
5
+
6
+ class AuthorizableService
7
+ HTTP_SUCCESS_CODES = [Net::HTTPOK, Net::HTTPNoContent]
8
+ DEFAULT_CONTENT_TYPE = 'application/x-www-form-urlencoded'.freeze
9
+ AUTHORIZATION_HEADER = 'Authorization'.freeze
10
+
11
+ private
12
+
13
+ def parsed_response(http_response)
14
+ response = http_response.body.present? ? JSON.parse(http_response.body) : http_response.body
15
+
16
+ return response if HTTP_SUCCESS_CODES.include?(http_response.code_type)
17
+
18
+ # TODO: For now, we assume that the access token is always valid.
19
+ # We do not yet handle the case where a refresh token is passed in and
20
+ # used if the access token has expired.
21
+ raise KeycloakOauth::AuthorizableError.new(response)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ require 'net/http'
2
+
3
+ module KeycloakOauth
4
+ class LogoutService < KeycloakOauth::AuthorizableService
5
+ def initialize(session)
6
+ @session = session
7
+ end
8
+
9
+ def logout
10
+ parsed_response(post_logout)
11
+ end
12
+
13
+ private
14
+
15
+ attr_accessor :session
16
+
17
+ def post_logout
18
+ uri = URI.parse(KeycloakOauth.connection.logout_endpoint)
19
+ Net::HTTP.start(uri.host, uri.port) do |http|
20
+ request = Net::HTTP::Post.new(uri)
21
+ request.set_content_type(DEFAULT_CONTENT_TYPE)
22
+ request.set_form_data(logout_request_params)
23
+ request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
24
+ http.request(request)
25
+ end
26
+ end
27
+
28
+ def logout_request_params
29
+ {
30
+ client_id: KeycloakOauth.connection.client_id,
31
+ client_secret: KeycloakOauth.connection.client_secret,
32
+ refresh_token: refresh_token
33
+ }
34
+ end
35
+
36
+ def access_token
37
+ session[:access_token]
38
+ end
39
+
40
+ def refresh_token
41
+ session[:refresh_token]
42
+ end
43
+ end
44
+ end
@@ -1,45 +1,30 @@
1
1
  require 'net/http'
2
2
 
3
3
  module KeycloakOauth
4
- class UserInfoRetrievalError < StandardError; end
5
-
6
- class UserInfoRetrievalService
7
- AUTHORIZATION_HEADER = 'Authorization'.freeze
8
- CONTENT_TYPE = 'application/x-www-form-urlencoded'.freeze
9
-
4
+ class UserInfoRetrievalService < KeycloakOauth::AuthorizableService
10
5
  attr_reader :user_information
11
6
 
12
- def initialize(access_token:)
7
+ def initialize(access_token:, refresh_token:)
13
8
  @access_token = access_token
9
+ @refresh_token = refresh_token
14
10
  end
15
11
 
16
12
  def retrieve
17
- @user_information = parsed_user_information(get_user)
13
+ @user_information = parsed_response(get_user)
18
14
  end
19
15
 
20
16
  private
21
17
 
22
- attr_accessor :access_token
18
+ attr_accessor :access_token, :refresh_token
23
19
 
24
20
  def get_user
25
21
  uri = URI.parse(KeycloakOauth.connection.user_info_endpoint)
26
22
  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
27
23
  request = Net::HTTP::Get.new(uri)
28
- request.set_content_type(CONTENT_TYPE)
24
+ request.set_content_type(DEFAULT_CONTENT_TYPE)
29
25
  request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
30
26
  http.request(request)
31
27
  end
32
28
  end
33
-
34
- def parsed_user_information(http_response)
35
- response_hash = JSON.parse(http_response.body)
36
-
37
- return response_hash if http_response.code_type == Net::HTTPOK
38
-
39
- # TODO: For now, we assume that the access token is always valid.
40
- # We do not yet handle the case where a refresh token is passed in and
41
- # used if the access token has expired.
42
- raise KeycloakOauth::UserInfoRetrievalError.new(response_hash)
43
- end
44
29
  end
45
30
  end
@@ -14,10 +14,18 @@ module KeycloakOauth
14
14
  @callback_module = callback_module
15
15
  end
16
16
 
17
- def get_user_information(access_token:)
18
- service = KeycloakOauth::UserInfoRetrievalService.new(access_token: access_token)
17
+ def get_user_information(access_token:, refresh_token:)
18
+ service = KeycloakOauth::UserInfoRetrievalService.new(
19
+ access_token: access_token,
20
+ refresh_token: refresh_token
21
+ )
19
22
  service.retrieve
20
23
  service.user_information
21
24
  end
25
+
26
+ def logout(session:)
27
+ service = KeycloakOauth::LogoutService.new(session)
28
+ service.logout
29
+ end
22
30
  end
23
31
  end
@@ -16,5 +16,9 @@ module KeycloakOauth
16
16
  def user_info_endpoint
17
17
  "#{auth_url}/realms/#{realm}/protocol/openid-connect/userinfo"
18
18
  end
19
+
20
+ def logout_endpoint
21
+ "#{auth_url}/realms/#{realm}/protocol/openid-connect/logout"
22
+ end
19
23
  end
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module KeycloakOauth
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - simplificator
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-03 00:00:00.000000000 Z
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -58,7 +58,7 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
- description:
61
+ description:
62
62
  email:
63
63
  - dev@simplificator.com
64
64
  executables: []
@@ -69,6 +69,8 @@ files:
69
69
  - Rakefile
70
70
  - app/controllers/keycloak_oauth/callbacks_controller.rb
71
71
  - app/services/keycloak_oauth/authentication_service.rb
72
+ - app/services/keycloak_oauth/authorizable_service.rb
73
+ - app/services/keycloak_oauth/logout_service.rb
72
74
  - app/services/keycloak_oauth/user_info_retrieval_service.rb
73
75
  - config/routes.rb
74
76
  - lib/keycloak_oauth.rb
@@ -84,7 +86,7 @@ metadata:
84
86
  homepage_uri: https://rubygems.org/gems/keycloak_oauth
85
87
  source_code_uri: https://github.com/simplificator/keycloak_oauth
86
88
  changelog_uri: https://github.com/simplificator/keycloak_oauth
87
- post_install_message:
89
+ post_install_message:
88
90
  rdoc_options: []
89
91
  require_paths:
90
92
  - lib
@@ -99,8 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
101
  - !ruby/object:Gem::Version
100
102
  version: '0'
101
103
  requirements: []
102
- rubygems_version: 3.0.3
103
- signing_key:
104
+ rubygems_version: 3.0.8
105
+ signing_key:
104
106
  specification_version: 4
105
107
  summary: Implementing OAuth with Keycloak in Ruby
106
108
  test_files: []