keycloak-ruby-client 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/lib/keycloak.rb +1 -0
- data/lib/keycloak/api/user_resources.rb +8 -3
- data/lib/keycloak/utils.rb +1 -0
- data/lib/keycloak/utils/representation_iterator.rb +62 -0
- data/lib/keycloak/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb38b642619a3555b01fb376a6339a3d549c243
|
4
|
+
data.tar.gz: 925ca9ad46a8632213681fdc822ed0c029773df5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7741aceb16860d9931dbebc11d5e2ce10c688c362a5574232bf9a4b141020f72f20f53bc9c33ab617e5d1c24b81ba87e9d2074c5a4a653aea8eb1980b1ad9f4a
|
7
|
+
data.tar.gz: 68a48f33549ff151c059acdf768233c6246d23ac4087a14bed8084b3a4fb269cf88a81b5ac2329d8f69d6b8386af66102b3c4be033e5210ad13817139b8f7f52
|
data/README.md
CHANGED
@@ -45,7 +45,7 @@ Then you can authenticate your keycloak JWT token:
|
|
45
45
|
|
46
46
|
```ruby
|
47
47
|
token = "your_bearer_token"
|
48
|
-
keycloak_token =
|
48
|
+
keycloak_token = Keycloak::Realm.shundao_admin.parse_access_token(token) # an instance of Keycloak::AccessToken
|
49
49
|
raise CanCan::AccessDenied if keycloak_token.expired? || !keycloak_token.has_role?("admin")
|
50
50
|
|
51
51
|
# authentication succeeded
|
@@ -100,6 +100,13 @@ user_rep = Keycloak::Model::UserRepresentation.new({
|
|
100
100
|
})
|
101
101
|
mapping_roles = role_store.map { |entry| {id: entry[1].id , name: entry[1].name} }
|
102
102
|
client.create_user(user_rep, mapping_roles)
|
103
|
+
|
104
|
+
# NOTE: It's worth noting that `to_a` may be costly if you have a large dataset of users,
|
105
|
+
# which could cause out-of-memory, but using `each` instead of `to_a` could save you if
|
106
|
+
# you really want traverse all users.
|
107
|
+
client.find_users.to_a.each { |user| puts user.to_json } # possibly out of memory
|
108
|
+
client.find_users.each { |user| puts user.to_json } # no risk of out of memory
|
109
|
+
|
103
110
|
```
|
104
111
|
|
105
112
|
### Connect your rails user model with keycloak user entity:
|
data/lib/keycloak.rb
CHANGED
@@ -28,13 +28,18 @@ module Keycloak
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# see https://www.keycloak.org/docs-api/6.0/rest-api/index.html#_users_resource for params details
|
31
|
+
#
|
32
|
+
# @return [Keycloak::Utils::RepresentationIterator] iterator of users
|
31
33
|
def find_users(params = {})
|
32
|
-
|
33
|
-
|
34
|
+
Utils::RepresentationIterator.new(self, params) do
|
35
|
+
res = JSON.parse(get(user_resources_url, params: params))
|
36
|
+
res.map { |user| Model::UserRepresentation.new user }
|
37
|
+
end
|
34
38
|
end
|
35
39
|
|
40
|
+
# @return [Keycloak::Model::UserRepresentation] user representation
|
36
41
|
def find_user_by_username(username)
|
37
|
-
find_users({username: username})[0]
|
42
|
+
find_users({username: username}).to_a[0]
|
38
43
|
end
|
39
44
|
|
40
45
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'keycloak/utils/representation_iterator'
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Keycloak
|
2
|
+
module Utils
|
3
|
+
# RepresentationIterator provides ability to enable lazy loading if necessary.
|
4
|
+
#
|
5
|
+
# NOTE: It's worth noting that `to_a` may be costly if you have a large dataset of users,
|
6
|
+
# which could cause out-of-memory, but using `each` instead of `to_a` could save you if
|
7
|
+
# you really want traverse all users.
|
8
|
+
class RepresentationIterator
|
9
|
+
DEFAULT_PER_PAGE = 30
|
10
|
+
|
11
|
+
include Enumerable
|
12
|
+
|
13
|
+
attr_accessor :first, :per_page, :till
|
14
|
+
attr_reader :cursor
|
15
|
+
|
16
|
+
def initialize(client, params, per_page: DEFAULT_PER_PAGE, till: nil, &block)
|
17
|
+
@client = client
|
18
|
+
@params = params
|
19
|
+
@params[:first] ||= 0
|
20
|
+
@first = @params[:first]
|
21
|
+
@till = @params[:max] || till
|
22
|
+
@per_page = per_page
|
23
|
+
@block = block
|
24
|
+
|
25
|
+
@data = []
|
26
|
+
@cursor = 0
|
27
|
+
end
|
28
|
+
|
29
|
+
def first=(num)
|
30
|
+
@first = num
|
31
|
+
@cursor = 0
|
32
|
+
@data = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def each
|
36
|
+
while true
|
37
|
+
return if at_end?
|
38
|
+
_fetch_next if @data.empty? || @cursor >= @data.size
|
39
|
+
return if @data.empty?
|
40
|
+
|
41
|
+
yield @data[@cursor]
|
42
|
+
@cursor += 1
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def _fetch_next
|
49
|
+
@params[:first] = @first
|
50
|
+
@params[:max] = @per_page
|
51
|
+
@data = @client.instance_eval(&@block)
|
52
|
+
|
53
|
+
@first += @per_page
|
54
|
+
@cursor = 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def at_end?
|
58
|
+
@till && @till <= @params[:first] + @cursor
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/keycloak/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.6
|
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-08-
|
11
|
+
date: 2019-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -117,6 +117,8 @@ files:
|
|
117
117
|
- lib/keycloak/scalar.rb
|
118
118
|
- lib/keycloak/scalar/array_type.rb
|
119
119
|
- lib/keycloak/user_entity.rb
|
120
|
+
- lib/keycloak/utils.rb
|
121
|
+
- lib/keycloak/utils/representation_iterator.rb
|
120
122
|
- lib/keycloak/version.rb
|
121
123
|
homepage: https://github.com/FX-HAO/keycloak-ruby-client
|
122
124
|
licenses:
|