keycloak-ruby-client 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5fb941053a36de82d660a51be6d041b432c9342
4
- data.tar.gz: 5c9ffc4b484fed6cdbff04f937b0a96f603e457e
3
+ metadata.gz: 64fd5654b140211d2d61816df33711498be7bdc4
4
+ data.tar.gz: d263f09209a4a4d27d282ed9c5fa673c9116a0ec
5
5
  SHA512:
6
- metadata.gz: bb526460464780a4be8667ce19e576d3ff790b169d22edf0502f20d9684bab8ca22a65c71c9aeecd7a9e6534729739420d40917208a93c4f791062062adbaf5f
7
- data.tar.gz: 50e74f7a50372209f4b011076ecb9ceeda1b4898ed718b6e4fdf01b4a54ab8ca5415ae28cc4b92d73f16a1880bdabc85343de35bf7dbac05e1a784b9337c3acc
6
+ metadata.gz: 87621811a0e707edfe779468442738f2044943666a7392e719a1c3511130423c52b91549c7a9c8130ae11fc5454037ad2dceb7faea2a3ab3f6f3076f2c5f3731
7
+ data.tar.gz: 062f6231ea0b325c6cac2d603446c93e0fc9e0a2c0176ce2933a208c82e5759a72875f74549e7db73b37fba8d1899d6411d166b7d79f0bbc0d1145bfa8a84ed2
data/README.md CHANGED
@@ -109,6 +109,8 @@ client.find_users.each { |user| puts user.to_json } # no risk of out of memory
109
109
 
110
110
  ```
111
111
 
112
+ For more examples, please take a look at [spec/api/](./spec/api/)
113
+
112
114
  ### Connect your rails user model with keycloak user entity:
113
115
 
114
116
  Firstly, we create a user model with `bundle exec rails g model user`
@@ -44,4 +44,5 @@ Gem::Specification.new do |spec|
44
44
  spec.add_development_dependency "rake", "~> 10.0"
45
45
  spec.add_development_dependency "rspec"
46
46
  spec.add_development_dependency "faker"
47
+ spec.add_development_dependency "factory_bot"
47
48
  end
@@ -4,11 +4,14 @@ module Keycloak
4
4
  extend ActiveSupport::Concern
5
5
  include Concerns::APIUtil
6
6
 
7
+ def client_resources_url
8
+ "#{admin_realm_url}/clients"
9
+ end
10
+
7
11
  # @param client_id [String] client-id (not id of client)
8
12
  # @return [Keycloak::Model::ClientRepresentation] client representation
9
13
  def find_client_by_client_id(client_id)
10
- url = admin_realm_url + "/clients"
11
- data = JSON.parse(get(url, params: { clientId: client_id }).body)
14
+ data = JSON.parse(get(client_resources_url, params: { clientId: client_id }).body)
12
15
  data[0] && Model::ClientRepresentation.new(data[0])
13
16
  rescue RestClient::NotFound
14
17
  nil
@@ -17,11 +20,25 @@ module Keycloak
17
20
  # @param id [String] id of client (not client-id)
18
21
  # @return [Keycloak::Model::ClientRepresentation] client representation
19
22
  def find_client_by_id(id)
20
- url = admin_realm_url + "/clients/#{id}"
23
+ url = client_resources_url + "/#{id}"
21
24
  Model::ClientRepresentation.new JSON.parse(get(url).body)
22
25
  rescue RestClient::NotFound
23
26
  nil
24
27
  end
28
+
29
+ # @param client_rep [Keycloak::Model::UserRepresentation] client representation
30
+ # @return [String] id of client
31
+ def create_client(client_rep)
32
+ res = post(client_resources_url, client_rep.to_json, headers: {content_type: :json})
33
+ res.headers[:location].split("/")[-1]
34
+ end
35
+
36
+ # @param id [String] id of client (not client-id)
37
+ # @param client_rep [Keycloak::Model::UserRepresentation] client representation
38
+ def update_client(id, client_rep)
39
+ url = client_resources_url + "/#{id}"
40
+ put(url, client_rep.to_json, headers: {content_type: :json})
41
+ end
25
42
  end
26
43
  end
27
44
  end
@@ -5,8 +5,10 @@ module Keycloak
5
5
  include Concerns::APIUtil
6
6
 
7
7
  # @param realm_rep [Keycloak::Model::RealmRepresentation] realm representation
8
+ # @return [String] realm id
8
9
  def create_realm(realm_rep)
9
- post("#{@auth_server_url}/admin/realms/", realm_rep.to_json, headers: {content_type: :json})
10
+ res = post("#{@auth_server_url}/admin/realms/", realm_rep.to_json, headers: {content_type: :json})
11
+ res.headers[:location].split("/")[-1]
10
12
  end
11
13
 
12
14
  # @param realm [String] realm name
@@ -43,11 +43,32 @@ module Keycloak
43
43
  end
44
44
  end
45
45
 
46
+ # @param username [String] username
46
47
  # @return [Keycloak::Model::UserRepresentation] user representation
47
48
  def find_user_by_username(username)
48
49
  find_users({username: username}).to_a[0]
49
50
  end
50
51
 
52
+ # @param role [String] role name
53
+ # @param params [Hash] extra params
54
+ # @return [Keycloak::Model::UserRepresentation] user representation
55
+ def find_user_by_role(role, params = {})
56
+ url = "#{admin_realm_url}/roles/#{role}/users"
57
+ Utils::RepresentationIterator.new(self, params) do
58
+ JSON.parse(get(url, params: params)).map { |user| Model::UserRepresentation.new(user) }
59
+ end
60
+ end
61
+
62
+ # @param id_of_client [String] id of client (not client-id)
63
+ # @param role [String] role name
64
+ # @param params [Hash] extra params
65
+ # @return [Keycloak::Model::UserRepresentation] user representation
66
+ def find_user_by_client_role(id_of_client, role, params = {})
67
+ url = "#{admin_realm_url}/clients/#{id_of_client}/roles/#{role}/users"
68
+ Utils::RepresentationIterator.new(self, params) do
69
+ JSON.parse(get(url, params: params)).map { |user| Model::UserRepresentation.new(user) }
70
+ end
71
+ end
51
72
  end
52
73
  end
53
74
  end
@@ -39,7 +39,8 @@ module Keycloak
39
39
  username: username,
40
40
  password: password,
41
41
  grant_type: grant_type,
42
- client_id: client_id
42
+ client_id: client_id,
43
+ scope: "offline_access"
43
44
  }, try_refresh_token: false).body
44
45
  @access_token = res["access_token"]
45
46
  @refresh_token = res["refresh_token"]
@@ -1,3 +1,3 @@
1
1
  module Keycloak
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keycloak-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fuxin Hao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-23 00:00:00.000000000 Z
11
+ date: 2019-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: factory_bot
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Keycloak ruby client
112
126
  email:
113
127
  - haofxpro@gmail.com