keycloak_oauth 0.1.8 → 0.1.9
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/app/services/keycloak_oauth/authorizable_service.rb +25 -1
- data/app/services/keycloak_oauth/get_users_service.rb +42 -0
- data/app/services/keycloak_oauth/logout_service.rb +1 -1
- data/app/services/keycloak_oauth/post_token_service.rb +1 -1
- data/app/services/keycloak_oauth/post_users_service.rb +2 -4
- data/app/services/keycloak_oauth/put_execute_actions_email_service.rb +57 -0
- data/app/services/keycloak_oauth/user_info_retrieval_service.rb +1 -1
- data/lib/keycloak_oauth/endpoints.rb +5 -1
- data/lib/keycloak_oauth/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd57edea266446ba41b1247a566ae9919ee9f5d26c8b4f77a915f568a085542d
|
4
|
+
data.tar.gz: 6334660d691bb5aeab2a8a789b7f2d57c4b8ff643ea29fb4a711f968d08bdc62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b472474aca15f56c3c7bbddbb5c135b11220add4f2cf0e6d52cc8398b5b3b8c755fa4d9517e103a68fe6ce86e676ac12860c077793a3094632e94da4b4a067d
|
7
|
+
data.tar.gz: aa7927e0ec6759a835123ae6d3f10ba59469558fe50081d2143c0274a94bfed11543527cc71b286436242e50588a7a5596b4bdad4152c90e80c385833f8a4f84
|
@@ -2,10 +2,12 @@ require 'net/http'
|
|
2
2
|
|
3
3
|
module KeycloakOauth
|
4
4
|
class AuthorizableError < StandardError; end
|
5
|
+
class NotFoundError < StandardError; end
|
5
6
|
|
6
7
|
class AuthorizableService
|
7
8
|
HTTP_SUCCESS_CODES = [Net::HTTPOK, Net::HTTPNoContent, Net::HTTPCreated]
|
8
|
-
|
9
|
+
CONTENT_TYPE_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded'.freeze
|
10
|
+
CONTENT_TYPE_JSON = 'application/json'.freeze
|
9
11
|
AUTHORIZATION_HEADER = 'Authorization'.freeze
|
10
12
|
|
11
13
|
attr_reader :http_response, :parsed_response_body
|
@@ -15,6 +17,20 @@ module KeycloakOauth
|
|
15
17
|
@parsed_response_body ||= parse_response_body(http_response)
|
16
18
|
end
|
17
19
|
|
20
|
+
def self.uri_with_supported_query_params(url, supported_params, given_params)
|
21
|
+
uri = URI.parse(url)
|
22
|
+
|
23
|
+
query_params = supported_params.inject({}) do |acc, query_param|
|
24
|
+
acc[query_param] = given_params[query_param] if given_params[query_param].present?
|
25
|
+
acc
|
26
|
+
end
|
27
|
+
|
28
|
+
log_unsupported_params(given_params.keys - supported_params)
|
29
|
+
|
30
|
+
uri.query = URI.encode_www_form(query_params) if query_params.values.any?
|
31
|
+
uri
|
32
|
+
end
|
33
|
+
|
18
34
|
private
|
19
35
|
|
20
36
|
def parse_response_body(http_response)
|
@@ -39,6 +55,8 @@ module KeycloakOauth
|
|
39
55
|
return response['errorMessage']
|
40
56
|
elsif response.has_key?('error_description')
|
41
57
|
return response['error_description']
|
58
|
+
elsif response.has_key?('error')
|
59
|
+
return response['error']
|
42
60
|
end
|
43
61
|
when 'String'
|
44
62
|
return response
|
@@ -46,5 +64,11 @@ module KeycloakOauth
|
|
46
64
|
'Unexpected Keycloak error'
|
47
65
|
end
|
48
66
|
end
|
67
|
+
|
68
|
+
def self.log_unsupported_params(query_params)
|
69
|
+
query_params.each do |query_param|
|
70
|
+
Rails.logger.warn { "Unsupported query param was passed in: #{query_param}" }
|
71
|
+
end
|
72
|
+
end
|
49
73
|
end
|
50
74
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module KeycloakOauth
|
4
|
+
class GetUsersService < KeycloakOauth::AuthorizableService
|
5
|
+
SUPPORTED_QUERY_PARAMS = %i(briefRepresentation email first firstName lastName max search username)
|
6
|
+
|
7
|
+
attr_reader :connection, :options
|
8
|
+
|
9
|
+
def initialize(connection:, access_token:, refresh_token:, options: {})
|
10
|
+
@connection = connection
|
11
|
+
@access_token = access_token
|
12
|
+
@refresh_token = refresh_token
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def send_request
|
17
|
+
get_users
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :access_token, :refresh_token
|
23
|
+
|
24
|
+
def get_users
|
25
|
+
uri = build_uri
|
26
|
+
|
27
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
28
|
+
request = Net::HTTP::Get.new(uri)
|
29
|
+
request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
|
30
|
+
http.request(request)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_uri
|
35
|
+
self.class.uri_with_supported_query_params(
|
36
|
+
connection.users_endpoint,
|
37
|
+
SUPPORTED_QUERY_PARAMS,
|
38
|
+
options
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -18,7 +18,7 @@ module KeycloakOauth
|
|
18
18
|
uri = URI.parse(KeycloakOauth.connection.logout_endpoint)
|
19
19
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
20
20
|
request = Net::HTTP::Post.new(uri)
|
21
|
-
request.set_content_type(
|
21
|
+
request.set_content_type(CONTENT_TYPE_X_WWW_FORM_URLENCODED)
|
22
22
|
request.set_form_data(logout_request_params)
|
23
23
|
request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
|
24
24
|
http.request(request)
|
@@ -23,7 +23,7 @@ module KeycloakOauth
|
|
23
23
|
uri = URI.parse(connection.authentication_endpoint)
|
24
24
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
25
25
|
request = Net::HTTP::Post.new(uri)
|
26
|
-
request.set_content_type(
|
26
|
+
request.set_content_type(CONTENT_TYPE_X_WWW_FORM_URLENCODED)
|
27
27
|
request.set_form_data(token_request_params)
|
28
28
|
http.request(request)
|
29
29
|
end
|
@@ -4,8 +4,6 @@ module KeycloakOauth
|
|
4
4
|
class DuplicationError < StandardError; end
|
5
5
|
|
6
6
|
class PostUsersService < KeycloakOauth::AuthorizableService
|
7
|
-
CONTENT_TYPE = 'application/json'.freeze
|
8
|
-
|
9
7
|
attr_reader :request_params, :connection, :user_params
|
10
8
|
|
11
9
|
def initialize(connection:, access_token:, refresh_token:, user_params:)
|
@@ -24,10 +22,10 @@ module KeycloakOauth
|
|
24
22
|
attr_accessor :access_token, :refresh_token
|
25
23
|
|
26
24
|
def post_users
|
27
|
-
uri = URI.parse(connection.
|
25
|
+
uri = URI.parse(connection.users_endpoint)
|
28
26
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
29
27
|
request = Net::HTTP::Post.new(uri)
|
30
|
-
request.set_content_type(
|
28
|
+
request.set_content_type(CONTENT_TYPE_JSON)
|
31
29
|
request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
|
32
30
|
request.body = user_params.to_json
|
33
31
|
http.request(request)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module KeycloakOauth
|
4
|
+
class PutExecuteActionsEmailService < KeycloakOauth::AuthorizableService
|
5
|
+
SUPPORTED_QUERY_PARAMS = %i(client_id lifespan redirect_uri)
|
6
|
+
|
7
|
+
attr_reader :connection, :user_id, :actions, :options
|
8
|
+
|
9
|
+
def initialize(connection: KeycloakOauth.connection, access_token:, refresh_token:, user_id:, actions:, options: {})
|
10
|
+
@connection = connection
|
11
|
+
@access_token = access_token
|
12
|
+
@refresh_token = refresh_token
|
13
|
+
@user_id = user_id
|
14
|
+
@actions = actions
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def send_request
|
19
|
+
put_execute_actions_email
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_accessor :access_token, :refresh_token
|
25
|
+
|
26
|
+
def put_execute_actions_email
|
27
|
+
uri = build_uri
|
28
|
+
|
29
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
30
|
+
request = Net::HTTP::Put.new(uri)
|
31
|
+
request.set_content_type(CONTENT_TYPE_JSON)
|
32
|
+
request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
|
33
|
+
request.body = actions.to_json
|
34
|
+
http.request(request)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def build_uri
|
39
|
+
self.class.uri_with_supported_query_params(
|
40
|
+
connection.put_execute_actions_email_endpoint(user_id),
|
41
|
+
SUPPORTED_QUERY_PARAMS,
|
42
|
+
options
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_response_body(http_response)
|
47
|
+
super
|
48
|
+
rescue KeycloakOauth::AuthorizableError => exception
|
49
|
+
raise exception unless not_found_error?(exception)
|
50
|
+
raise KeycloakOauth::NotFoundError.new(exception)
|
51
|
+
end
|
52
|
+
|
53
|
+
def not_found_error?(exception)
|
54
|
+
exception.message == "User not found"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -21,7 +21,7 @@ module KeycloakOauth
|
|
21
21
|
uri = URI.parse(KeycloakOauth.connection.user_info_endpoint)
|
22
22
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
23
23
|
request = Net::HTTP::Get.new(uri)
|
24
|
-
request.set_content_type(
|
24
|
+
request.set_content_type(CONTENT_TYPE_X_WWW_FORM_URLENCODED)
|
25
25
|
request[AUTHORIZATION_HEADER] = "Bearer #{access_token}"
|
26
26
|
http.request(request)
|
27
27
|
end
|
@@ -21,8 +21,12 @@ module KeycloakOauth
|
|
21
21
|
"#{auth_url}/realms/#{realm}/protocol/openid-connect/logout"
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
24
|
+
def users_endpoint
|
25
25
|
"#{auth_url}/admin/realms/#{realm}/users"
|
26
26
|
end
|
27
|
+
|
28
|
+
def put_execute_actions_email_endpoint(user_id)
|
29
|
+
"#{auth_url}/admin/realms/#{realm}/users/#{user_id}/execute-actions-email"
|
30
|
+
end
|
27
31
|
end
|
28
32
|
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.
|
4
|
+
version: 0.1.9
|
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-
|
11
|
+
date: 2020-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -70,9 +70,11 @@ files:
|
|
70
70
|
- app/controllers/keycloak_oauth/callbacks_controller.rb
|
71
71
|
- app/services/keycloak_oauth/authentication_service.rb
|
72
72
|
- app/services/keycloak_oauth/authorizable_service.rb
|
73
|
+
- app/services/keycloak_oauth/get_users_service.rb
|
73
74
|
- app/services/keycloak_oauth/logout_service.rb
|
74
75
|
- app/services/keycloak_oauth/post_token_service.rb
|
75
76
|
- app/services/keycloak_oauth/post_users_service.rb
|
77
|
+
- app/services/keycloak_oauth/put_execute_actions_email_service.rb
|
76
78
|
- app/services/keycloak_oauth/user_info_retrieval_service.rb
|
77
79
|
- config/routes.rb
|
78
80
|
- lib/keycloak_oauth.rb
|