omniauth_oidc 0.2.7 → 1.1.0
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/.rubocop.yml +2 -1
- data/CHANGELOG.md +115 -0
- data/LICENSE.txt +1 -1
- data/README.md +157 -69
- data/lib/omniauth/oidc/client.rb +86 -0
- data/lib/omniauth/oidc/config_fetcher.rb +114 -0
- data/lib/omniauth/oidc/errors.rb +20 -1
- data/lib/omniauth/oidc/http_client.rb +103 -0
- data/lib/omniauth/oidc/jwk_handler.rb +48 -0
- data/lib/omniauth/oidc/jwks_cache.rb +30 -0
- data/lib/omniauth/oidc/logging.rb +76 -0
- data/lib/omniauth/oidc/response_objects.rb +176 -0
- data/lib/omniauth/oidc/ttl_cache.rb +82 -0
- data/lib/omniauth/oidc/version.rb +1 -1
- data/lib/omniauth/strategies/oidc/callback.rb +137 -90
- data/lib/omniauth/strategies/oidc/request.rb +22 -18
- data/lib/omniauth/strategies/oidc/verify.rb +172 -68
- data/lib/omniauth/strategies/oidc.rb +93 -34
- data/lib/omniauth_oidc.rb +8 -0
- data/omniauth_oidc.gemspec +8 -5
- data/sig/omniauth_oidc.rbs +214 -1
- metadata +45 -34
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OmniauthOidc
|
|
4
|
+
# Thread-safe cache with a per-entry TTL.
|
|
5
|
+
#
|
|
6
|
+
# Reads take a short global lock only. Fetches run under a per-key lock, so a
|
|
7
|
+
# slow or hanging upstream for one key never blocks cache hits for other keys,
|
|
8
|
+
# and concurrent misses for the same key trigger a single fetch.
|
|
9
|
+
class TtlCache
|
|
10
|
+
Entry = Struct.new(:value, :fetched_at, keyword_init: true)
|
|
11
|
+
|
|
12
|
+
attr_reader :ttl
|
|
13
|
+
|
|
14
|
+
def initialize(ttl:, name: "TTL")
|
|
15
|
+
@ttl = ttl
|
|
16
|
+
@name = name
|
|
17
|
+
@entries = {}
|
|
18
|
+
@locks = {}
|
|
19
|
+
@mutex = Mutex.new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Returns the cached value for key, calling the block to fetch it on a miss.
|
|
23
|
+
#
|
|
24
|
+
# ttl: seconds an entry stays valid (defaults to the cache TTL)
|
|
25
|
+
# force_refresh: fetch even if the entry is still valid
|
|
26
|
+
# min_age: with force_refresh, reuse an entry fetched less than this many
|
|
27
|
+
# seconds ago. Rate limits refreshes that callers can trigger.
|
|
28
|
+
def fetch(key, force_refresh: false, ttl: nil, min_age: 0, &block)
|
|
29
|
+
ttl ||= @ttl
|
|
30
|
+
entry = read(key)
|
|
31
|
+
return hit(key, entry) if !force_refresh && younger_than?(entry, ttl)
|
|
32
|
+
|
|
33
|
+
lock_for(key).synchronize do
|
|
34
|
+
# Another thread may have fetched while this one waited for the lock
|
|
35
|
+
entry = read(key)
|
|
36
|
+
return hit(key, entry) if younger_than?(entry, force_refresh ? min_age : ttl)
|
|
37
|
+
|
|
38
|
+
Logging.info("#{@name} cache miss, fetching", key: key, force_refresh: force_refresh)
|
|
39
|
+
value = block.call
|
|
40
|
+
@mutex.synchronize { @entries[key] = Entry.new(value: value, fetched_at: Time.now) }
|
|
41
|
+
value
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def valid?(key)
|
|
46
|
+
younger_than?(read(key), @ttl)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def invalidate(key)
|
|
50
|
+
@mutex.synchronize { @entries.delete(key) }
|
|
51
|
+
Logging.debug("#{@name} cache invalidated", key: key)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def clear!
|
|
55
|
+
@mutex.synchronize { @entries.clear }
|
|
56
|
+
Logging.debug("#{@name} cache cleared")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def ttl=(value)
|
|
60
|
+
@mutex.synchronize { @ttl = value }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def read(key)
|
|
66
|
+
@mutex.synchronize { @entries[key] }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def hit(key, entry)
|
|
70
|
+
Logging.debug("#{@name} cache hit", key: key)
|
|
71
|
+
entry.value
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def younger_than?(entry, seconds)
|
|
75
|
+
!entry.nil? && Time.now - entry.fetched_at < seconds
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def lock_for(key)
|
|
79
|
+
@mutex.synchronize { @locks[key] ||= Mutex.new }
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -3,120 +3,157 @@
|
|
|
3
3
|
module OmniAuth
|
|
4
4
|
module Strategies
|
|
5
5
|
class Oidc
|
|
6
|
-
# Callback phase
|
|
7
|
-
module Callback
|
|
8
|
-
def callback_phase
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
6
|
+
# Callback phase - handles OIDC provider response
|
|
7
|
+
module Callback # rubocop:disable Metrics/ModuleLength
|
|
8
|
+
def callback_phase
|
|
9
|
+
OmniauthOidc::Logging.instrument("callback_phase.start", provider: name) do
|
|
10
|
+
handle_callback_errors do
|
|
11
|
+
validate_callback_params!
|
|
12
|
+
next missing_response_type_failure unless params.key?(configured_response_type)
|
|
13
|
+
|
|
14
|
+
options.issuer = issuer if options.issuer.nil? || options.issuer.empty?
|
|
15
|
+
|
|
16
|
+
client.redirect_uri = redirect_uri
|
|
17
|
+
|
|
18
|
+
if configured_response_type == "id_token"
|
|
19
|
+
handle_id_token_response
|
|
20
|
+
else
|
|
21
|
+
handle_code_response
|
|
22
|
+
end
|
|
23
|
+
verify_subject!
|
|
24
|
+
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
private
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
def handle_callback_errors
|
|
33
|
+
yield
|
|
29
34
|
rescue CallbackError => e
|
|
35
|
+
OmniauthOidc::Logging.error("Callback error", error: e.error, reason: e.error_reason)
|
|
30
36
|
fail!(e.error, e)
|
|
31
|
-
rescue ::
|
|
32
|
-
|
|
37
|
+
rescue OmniauthOidc::ConfigurationError, OmniauthOidc::JwksError => e
|
|
38
|
+
OmniauthOidc::Logging.error("Provider setup error", error: e.class.name, message: e.message)
|
|
39
|
+
fail!(e.is_a?(OmniauthOidc::JwksError) ? :jwks_error : :configuration_error, e)
|
|
40
|
+
rescue OmniauthOidc::TokenError => e
|
|
41
|
+
OmniauthOidc::Logging.error("Token error", error: e.class.name, message: e.message)
|
|
42
|
+
fail!(:token_error, e)
|
|
43
|
+
rescue OmniauthOidc::HttpClient::HttpError => e
|
|
44
|
+
OmniauthOidc::Logging.error("HTTP error", message: e.message)
|
|
45
|
+
fail!(:http_error, e)
|
|
33
46
|
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
|
|
47
|
+
OmniauthOidc::Logging.error("Timeout error", message: e.message)
|
|
34
48
|
fail!(:timeout, e)
|
|
35
49
|
rescue ::SocketError => e
|
|
50
|
+
OmniauthOidc::Logging.error("Connection error", message: e.message)
|
|
36
51
|
fail!(:failed_to_connect, e)
|
|
37
52
|
end
|
|
38
53
|
|
|
39
|
-
|
|
54
|
+
def validate_callback_params!
|
|
55
|
+
error = params["error_reason"] || params["error"]
|
|
56
|
+
error_description = params["error_description"] || params["error_reason"]
|
|
57
|
+
invalid_state = (options.require_state && params["state"].to_s.empty?) || params["state"] != stored_state
|
|
40
58
|
|
|
41
|
-
|
|
42
|
-
|
|
59
|
+
if error
|
|
60
|
+
raise CallbackError, error: params["error"], error_reason: error_description,
|
|
61
|
+
error_uri: params["error_uri"]
|
|
62
|
+
end
|
|
63
|
+
raise CallbackError, error: :csrf_detected, error_reason: "Invalid 'state' parameter" if invalid_state
|
|
64
|
+
end
|
|
43
65
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
66
|
+
def missing_response_type_failure
|
|
67
|
+
error_attrs = RESPONSE_TYPE_EXCEPTIONS[configured_response_type]
|
|
68
|
+
fail!(error_attrs[:key], error_attrs[:exception_class].new(params["error"]))
|
|
69
|
+
end
|
|
48
70
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|
|
71
|
+
def handle_code_response
|
|
72
|
+
# Get access token via token exchange
|
|
73
|
+
@access_token = fetch_access_token
|
|
53
74
|
|
|
54
|
-
|
|
75
|
+
# Verify the ID token from the token response once, and reuse the result below
|
|
76
|
+
@verified_id_token = verify_id_token!(@access_token.id_token)
|
|
55
77
|
|
|
56
|
-
|
|
78
|
+
# Fetch and set user info
|
|
79
|
+
@user_info = fetch_user_info
|
|
80
|
+
end
|
|
57
81
|
|
|
58
|
-
|
|
82
|
+
def handle_id_token_response
|
|
83
|
+
# For id_token response type, extract user data directly from the verified token
|
|
84
|
+
# to_s: a bare "?id_token" param is nil and must fail verification, not skip it
|
|
85
|
+
@verified_id_token = verify_id_token!(params["id_token"].to_s)
|
|
86
|
+
@user_info = OmniauthOidc::ResponseObjects::UserInfo.new(@verified_id_token.raw_attributes)
|
|
87
|
+
|
|
88
|
+
# Create a minimal access token structure for credentials
|
|
89
|
+
@access_token = OmniauthOidc::ResponseObjects::AccessToken.new(
|
|
90
|
+
id_token: params["id_token"],
|
|
91
|
+
access_token: nil,
|
|
92
|
+
refresh_token: nil,
|
|
93
|
+
expires_in: nil,
|
|
94
|
+
scope: nil
|
|
95
|
+
)
|
|
96
|
+
end
|
|
59
97
|
|
|
60
|
-
|
|
98
|
+
def fetch_access_token
|
|
99
|
+
OmniauthOidc::Logging.instrument("token.exchange", provider: name) do
|
|
100
|
+
token_request_params = {
|
|
101
|
+
code: authorization_code,
|
|
102
|
+
redirect_uri: redirect_uri
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
# The verifier comes only from the session. Request params are attacker controlled.
|
|
106
|
+
token_request_params[:code_verifier] = stored_pkce_verifier if options.pkce
|
|
107
|
+
|
|
108
|
+
set_client_options_for_callback_phase
|
|
109
|
+
|
|
110
|
+
client.access_token!(token_request_params)
|
|
111
|
+
end
|
|
61
112
|
end
|
|
62
113
|
|
|
63
|
-
def
|
|
64
|
-
|
|
114
|
+
def stored_pkce_verifier
|
|
115
|
+
verifier = session.delete(session_key("pkce.verifier"))
|
|
116
|
+
return verifier unless verifier.to_s.empty?
|
|
65
117
|
|
|
66
|
-
|
|
118
|
+
raise OmniauthOidc::TokenError, "No PKCE code verifier stored in the session for this login"
|
|
67
119
|
end
|
|
68
120
|
|
|
69
|
-
def
|
|
70
|
-
return
|
|
121
|
+
def fetch_user_info
|
|
122
|
+
return minimal_user_info_from_token unless options.fetch_user_info
|
|
71
123
|
|
|
72
|
-
|
|
73
|
-
|
|
124
|
+
userinfo_data = OmniauthOidc::Logging.instrument("userinfo.fetch", provider: name) do
|
|
125
|
+
client.userinfo!(@access_token.access_token).raw_attributes
|
|
126
|
+
end
|
|
127
|
+
return OmniauthOidc::ResponseObjects::UserInfo.new(userinfo_data) unless @verified_id_token
|
|
128
|
+
|
|
129
|
+
id_token_claims = @verified_id_token.raw_attributes
|
|
130
|
+
# OIDC Core 5.3.4: UserInfo values must not be used unless its sub matches the ID token
|
|
131
|
+
unless userinfo_data["sub"] == id_token_claims["sub"]
|
|
132
|
+
OmniauthOidc::Logging.error("UserInfo sub does not match ID token sub, using ID token claims only")
|
|
133
|
+
return minimal_user_info_from_token
|
|
134
|
+
end
|
|
74
135
|
|
|
75
|
-
|
|
136
|
+
OmniauthOidc::ResponseObjects::UserInfo.new(id_token_claims.merge(userinfo_data))
|
|
137
|
+
rescue StandardError => e
|
|
138
|
+
OmniauthOidc::Logging.warn("Failed to fetch userinfo, falling back to ID token", error: e.message)
|
|
139
|
+
minimal_user_info_from_token
|
|
76
140
|
end
|
|
77
141
|
|
|
78
|
-
def
|
|
79
|
-
|
|
80
|
-
config.userinfo_endpoint, {
|
|
81
|
-
headers: {
|
|
82
|
-
"Authorization" => "Bearer #{@access_token}",
|
|
83
|
-
"Content-Type" => "application/json"
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
)
|
|
142
|
+
def minimal_user_info_from_token
|
|
143
|
+
return empty_user_info unless @verified_id_token
|
|
87
144
|
|
|
88
|
-
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
refresh_token: @access_token.refresh_token,
|
|
101
|
-
expires_in: @access_token.expires_in,
|
|
102
|
-
scope: @access_token.scope
|
|
103
|
-
}
|
|
104
|
-
)
|
|
105
|
-
call_app!
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def define_access_token
|
|
109
|
-
env["omniauth.auth"] = AuthHash.new(
|
|
110
|
-
provider: name,
|
|
111
|
-
credentials: {
|
|
112
|
-
id_token: @access_token.id_token,
|
|
113
|
-
token: @access_token.access_token,
|
|
114
|
-
refresh_token: @access_token.refresh_token,
|
|
115
|
-
expires_in: @access_token.expires_in,
|
|
116
|
-
scope: @access_token.scope
|
|
117
|
-
}
|
|
118
|
-
)
|
|
119
|
-
call_app!
|
|
145
|
+
OmniauthOidc::ResponseObjects::UserInfo.new(@verified_id_token.raw_attributes)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# A login without a subject must never complete: a nil uid can match the wrong local account
|
|
149
|
+
def verify_subject!
|
|
150
|
+
return unless uid.to_s.empty?
|
|
151
|
+
|
|
152
|
+
raise OmniauthOidc::TokenVerificationError, "Neither the ID token nor UserInfo provided a subject (sub)"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def empty_user_info
|
|
156
|
+
OmniauthOidc::ResponseObjects::UserInfo.new({})
|
|
120
157
|
end
|
|
121
158
|
|
|
122
159
|
def configured_response_type
|
|
@@ -127,9 +164,19 @@ module OmniAuth
|
|
|
127
164
|
def set_client_options_for_callback_phase
|
|
128
165
|
client.host = host
|
|
129
166
|
client.redirect_uri = redirect_uri
|
|
130
|
-
client.authorization_endpoint =
|
|
131
|
-
client.token_endpoint =
|
|
132
|
-
client.userinfo_endpoint =
|
|
167
|
+
client.authorization_endpoint = config.authorization_endpoint
|
|
168
|
+
client.token_endpoint = config.token_endpoint
|
|
169
|
+
client.userinfo_endpoint = config.userinfo_endpoint
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Accessor for OmniAuth DSL blocks
|
|
173
|
+
def user_info
|
|
174
|
+
@user_info
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Accessor for OmniAuth DSL blocks
|
|
178
|
+
def access_token
|
|
179
|
+
@access_token
|
|
133
180
|
end
|
|
134
181
|
end
|
|
135
182
|
end
|
|
@@ -6,15 +6,16 @@ module OmniAuth
|
|
|
6
6
|
# Code request phase
|
|
7
7
|
module Request
|
|
8
8
|
def request_phase
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
OmniauthOidc::Logging.instrument("request_phase.start", provider: name) do
|
|
10
|
+
set_client_options_for_request_phase
|
|
11
|
+
redirect authorize_uri
|
|
12
|
+
end
|
|
13
|
+
rescue OmniauthOidc::ConfigurationError => e
|
|
14
|
+
OmniauthOidc::Logging.error("Configuration error", error: e.class.name, message: e.message)
|
|
15
|
+
fail!(:configuration_error, e)
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
def authorize_uri # rubocop:disable Metrics/AbcSize
|
|
17
|
-
client.redirect_uri = redirect_uri
|
|
18
19
|
opts = request_options
|
|
19
20
|
|
|
20
21
|
opts.merge!(options.extra_authorize_params) unless options.extra_authorize_params.empty?
|
|
@@ -27,10 +28,14 @@ module OmniAuth
|
|
|
27
28
|
verifier = options.pkce_verifier ? options.pkce_verifier.call : SecureRandom.hex(64)
|
|
28
29
|
|
|
29
30
|
opts.merge!(pkce_authorize_params(verifier))
|
|
30
|
-
session["
|
|
31
|
+
session[session_key("pkce.verifier")] = verifier
|
|
31
32
|
end
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
# Add redirect_uri and extra_params to opts
|
|
35
|
+
opts[:redirect_uri] = redirect_uri
|
|
36
|
+
opts[:extra_params] = opts.compact
|
|
37
|
+
|
|
38
|
+
client.authorization_uri(opts)
|
|
34
39
|
end
|
|
35
40
|
|
|
36
41
|
private
|
|
@@ -45,7 +50,7 @@ module OmniAuth
|
|
|
45
50
|
ui_locales: params["ui_locales"],
|
|
46
51
|
claims_locales: params["claims_locales"],
|
|
47
52
|
prompt: options.prompt,
|
|
48
|
-
nonce: (new_nonce if
|
|
53
|
+
nonce: (new_nonce if nonce_required?),
|
|
49
54
|
hd: options.hd,
|
|
50
55
|
acr_values: options.acr_values
|
|
51
56
|
}
|
|
@@ -59,21 +64,20 @@ module OmniAuth
|
|
|
59
64
|
options.state.call
|
|
60
65
|
end
|
|
61
66
|
end
|
|
62
|
-
session["
|
|
67
|
+
session[session_key("state")] = state || SecureRandom.hex(16)
|
|
63
68
|
end
|
|
64
69
|
|
|
65
70
|
# Parse response from OIDC endpoint and set client options for request phase
|
|
66
|
-
def set_client_options_for_request_phase
|
|
71
|
+
def set_client_options_for_request_phase
|
|
67
72
|
client_options.host = host
|
|
68
|
-
client_options.authorization_endpoint =
|
|
69
|
-
client_options.token_endpoint =
|
|
70
|
-
client_options.userinfo_endpoint =
|
|
71
|
-
client_options.jwks_uri =
|
|
73
|
+
client_options.authorization_endpoint = config.authorization_endpoint
|
|
74
|
+
client_options.token_endpoint = config.token_endpoint
|
|
75
|
+
client_options.userinfo_endpoint = config.userinfo_endpoint
|
|
76
|
+
client_options.jwks_uri = config.jwks_uri
|
|
72
77
|
|
|
73
|
-
return unless config.
|
|
78
|
+
return unless config.end_session_endpoint
|
|
74
79
|
|
|
75
|
-
client_options.end_session_endpoint =
|
|
76
|
-
config.end_session_endpoint)
|
|
80
|
+
client_options.end_session_endpoint = config.end_session_endpoint
|
|
77
81
|
end
|
|
78
82
|
end
|
|
79
83
|
end
|