keycloak-sdk 1.4.0 → 1.4.1
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 +4 -2
- data/lib/keycloak_sdk/admin/admin_client.rb +5 -4
- data/lib/keycloak_sdk/admin/bearer_auth.rb +25 -25
- data/lib/keycloak_sdk/admin/call.rb +2 -1
- data/lib/keycloak_sdk/admin/clients.rb +1 -1
- data/lib/keycloak_sdk/admin/realms.rb +2 -2
- data/lib/keycloak_sdk/auth_client.rb +24 -29
- data/lib/keycloak_sdk/client.rb +2 -2
- data/lib/keycloak_sdk/config.rb +23 -21
- data/lib/keycloak_sdk/errors.rb +25 -19
- data/lib/keycloak_sdk/http.rb +79 -33
- data/lib/keycloak_sdk/jwks_store.rb +57 -32
- data/lib/keycloak_sdk/jwt_validator.rb +18 -16
- data/lib/keycloak_sdk/masking.rb +1 -1
- data/lib/keycloak_sdk/oidc_endpoints.rb +5 -5
- data/lib/keycloak_sdk/token_provider.rb +38 -10
- data/lib/keycloak_sdk/tokens.rb +28 -25
- data/lib/keycloak_sdk/version.rb +1 -1
- data/lib/keycloak_sdk.rb +5 -7
- metadata +1 -1
data/lib/keycloak_sdk/tokens.rb
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module KeycloakSdk
|
|
4
|
-
# OAuth
|
|
4
|
+
# Immutable value type of an OAuth token response. The access/refresh/id tokens are masked in inspect.
|
|
5
5
|
TokenSet = Data.define(:access_token, :token_type, :expires_in, :refresh_token,
|
|
6
6
|
:id_token, :scope, :expires_at) do
|
|
7
|
-
# ⚠️
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
7
|
+
# ⚠️ **The validation lives in the constructor, not the factory.** Put only in `from_response`, it would be
|
|
8
|
+
# bypassed entirely by the path where `AuthClient` calls `TokenSet.new` directly (`to_token_set` in
|
|
9
|
+
# `auth_client.rb`) — an independent leg pointed out that hole. Every path goes through the constructor, so
|
|
10
|
+
# placing it here makes a bypass impossible.
|
|
11
11
|
def initialize(access_token:, **rest)
|
|
12
12
|
unless access_token.is_a?(String) && !access_token.empty?
|
|
13
13
|
raise AuthError, "token response has no usable access_token"
|
|
@@ -17,12 +17,13 @@ module KeycloakSdk
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def self.from_response(body, received_at: Time.now.to_f)
|
|
20
|
-
#
|
|
20
|
+
# An array body broke through the boundary as a TypeError from `Array#[]` — a body that is not an object is
|
|
21
|
+
# an SDK error.
|
|
21
22
|
raise AuthError, "token response is not a JSON object" unless body.is_a?(Hash)
|
|
22
23
|
|
|
23
|
-
# ⚠️
|
|
24
|
-
# `access_token
|
|
25
|
-
# (
|
|
24
|
+
# ⚠️ **A presence check is not a type check.** The value used to be stored as is, so a number, a hash or nil
|
|
25
|
+
# became the `access_token`, and the consumer sent it as the Bearer and got a 401 every time
|
|
26
|
+
# (a silent, repeated failure). Accepting a string `expires_in` is **intended**, so it is left alone.
|
|
26
27
|
access_token = body["access_token"]
|
|
27
28
|
unless access_token.is_a?(String) && !access_token.empty?
|
|
28
29
|
raise AuthError, "token response has no usable access_token"
|
|
@@ -31,7 +32,7 @@ module KeycloakSdk
|
|
|
31
32
|
expires_in = begin
|
|
32
33
|
body["expires_in"] && Integer(body["expires_in"])
|
|
33
34
|
rescue ArgumentError, TypeError, RangeError # RangeError: JSON 1e400 = Infinity → FloatDomainError
|
|
34
|
-
# `cause: nil` — ArgumentError
|
|
35
|
+
# `cause: nil` — ArgumentError quotes the value (`invalid value for Integer(): "…"`).
|
|
35
36
|
raise AuthError, "token response has an unusable expires_in", cause: nil
|
|
36
37
|
end
|
|
37
38
|
new(
|
|
@@ -45,10 +46,10 @@ module KeycloakSdk
|
|
|
45
46
|
)
|
|
46
47
|
end
|
|
47
48
|
|
|
48
|
-
# ⚠️
|
|
49
|
-
# `false`(
|
|
50
|
-
# `expires_at`
|
|
51
|
-
#
|
|
49
|
+
# ⚠️ **An unknown expiry time means "expired"** (fail-safe — isomorphic with the eight siblings, Java's M.6).
|
|
50
|
+
# Returning `false` (= still alive) would make a token whose expiry time is unknown **valid forever**.
|
|
51
|
+
# `expires_at` is nil only when the server did not send `expires_in`, so it is not the normal path, and the
|
|
52
|
+
# safe side to take then is "reissue".
|
|
52
53
|
def expired?(skew: 0, now: Time.now.to_f)
|
|
53
54
|
return true if expires_at.nil?
|
|
54
55
|
|
|
@@ -62,20 +63,21 @@ module KeycloakSdk
|
|
|
62
63
|
end
|
|
63
64
|
alias_method :to_s, :inspect
|
|
64
65
|
|
|
65
|
-
# ⚠️ `pp`/`pretty_inspect`
|
|
66
|
-
#
|
|
67
|
-
#
|
|
66
|
+
# ⚠️ `pp`/`pretty_inspect` is a **display path** of the same rank as `inspect`, but for a `Data` type PP prints
|
|
67
|
+
# the members directly and **skips** the `inspect` above (measured: the raw access_token came out). For `Config`,
|
|
68
|
+
# an ordinary class, PP falls back to `inspect`, so it is safe — that is why only `Data` needs this hook.
|
|
68
69
|
def pretty_print(pp)
|
|
69
70
|
pp.text(inspect)
|
|
70
71
|
end
|
|
71
72
|
end
|
|
72
73
|
|
|
73
|
-
#
|
|
74
|
+
# The claims of interest of a validated access token.
|
|
74
75
|
ValidatedToken = Data.define(:subject, :audience, :issuer, :expires_at, :issued_at, :claims)
|
|
75
76
|
|
|
76
|
-
# RFC 7662 introspection
|
|
77
|
+
# RFC 7662 introspection result.
|
|
77
78
|
IntrospectionResult = Data.define(:active, :username, :client_id, :claims) do
|
|
78
|
-
#
|
|
79
|
+
# A body that is not an object is an SDK error — an array broke through the boundary as a TypeError, and a string
|
|
80
|
+
# became a result whose `claims` was a string.
|
|
79
81
|
def self.from_response(body)
|
|
80
82
|
raise AuthError, "introspection response is not a JSON object" unless body.is_a?(Hash)
|
|
81
83
|
|
|
@@ -88,8 +90,9 @@ module KeycloakSdk
|
|
|
88
90
|
end
|
|
89
91
|
end
|
|
90
92
|
|
|
91
|
-
# authorization-code
|
|
92
|
-
# nonce
|
|
93
|
+
# The values that start an authorization-code flow (PKCE code_verifier included · masked in inspect).
|
|
94
|
+
# The nonce is a replay-protection value carried in the authorization URL, so it is not a secret (on a par with
|
|
95
|
+
# state — only code_verifier is masked).
|
|
93
96
|
AuthorizationRequest = Data.define(:url, :state, :code_verifier, :nonce) do
|
|
94
97
|
def inspect
|
|
95
98
|
"#<KeycloakSdk::AuthorizationRequest url=#{url.inspect} state=#{state.inspect} " \
|
|
@@ -97,9 +100,9 @@ module KeycloakSdk
|
|
|
97
100
|
end
|
|
98
101
|
alias_method :to_s, :inspect
|
|
99
102
|
|
|
100
|
-
# ⚠️ `pp`/`pretty_inspect`
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
+
# ⚠️ `pp`/`pretty_inspect` is a **display path** of the same rank as `inspect`, but for a `Data` type PP prints
|
|
104
|
+
# the members directly and **skips** the `inspect` above (measured: the raw access_token came out). For `Config`,
|
|
105
|
+
# an ordinary class, PP falls back to `inspect`, so it is safe — that is why only `Data` needs this hook.
|
|
103
106
|
def pretty_print(pp)
|
|
104
107
|
pp.text(inspect)
|
|
105
108
|
end
|
data/lib/keycloak_sdk/version.rb
CHANGED
data/lib/keycloak_sdk.rb
CHANGED
|
@@ -26,11 +26,9 @@ require_relative "keycloak_sdk/client"
|
|
|
26
26
|
module KeycloakSdk
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
# ⚠️ rack-oauth2 HTTP
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
# ⚠️ SDK 자신의 토큰 요청은 이 전역 연결을 타지 않는다 — 그랜트는 SDK 커넥션(`Http.build`, 클라이언트마다 Config
|
|
35
|
-
# 타임아웃)으로 직접 보낸다(응답 상한 때문). rack-oauth2 의 HTTP 연결을 쓰는 SDK 경로는 이제 없다(인가 URL 조립은 네트워크가 없다).
|
|
29
|
+
# ⚠️ rack-oauth2 의 HTTP 설정(`Rack::OAuth2.http_config`)은 프로세스 전역이고 먼저 건 쪽이 이긴다 — SDK 는 그것을
|
|
30
|
+
# 걸지 않는다. require 때도(과거엔 하드코딩 10초로 전역을 변조했다), AuthClient 생성 때도(걸면 호스트 앱이 나중에 건
|
|
31
|
+
# 블록이 조용히 버려진다). SDK 의 요청은 그 연결을 타지 않는다 — 그랜트·introspection·logout·JWKS·admin 은 전부 SDK
|
|
32
|
+
# 커넥션(`Http.build`, 클라이언트마다 Config 타임아웃)으로 나가고 인가 URL 조립은 네트워크가 없다
|
|
33
|
+
# (`spec/unit/host_http_config_spec.rb`).
|
|
36
34
|
require "rack/oauth2"
|