Skip to content

Commit 94bba5f

Browse files
committed
FEATURE: Option to enable verbose logging of authentication process
1 parent d394c12 commit 94bba5f

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

config/locales/server.en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ en:
77
openid_connect_authorize_scope: "The scopes sent to the authorize endpoint. This must include 'openid'."
88
openid_connect_token_scope: "The scopes sent when requesting the token endpoint. The official specification does not require this."
99
openid_connect_error_redirects: "If the callback error_reason contains the first parameter, the user will be redirected to the URL in the second parameter"
10-
openid_connect_allow_association_change: "Allow users to disconnect and reconnect their Discourse accounts from the OpenID Connect provider"
10+
openid_connect_allow_association_change: "Allow users to disconnect and reconnect their Discourse accounts from the OpenID Connect provider"
11+
openid_connect_verbose_logging: "Log detailed openid-connect authentication information to `/logs`. Keep this disabled during normal use."

config/settings.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ plugins:
1111
default: false
1212
openid_connect_authorize_scope:
1313
default: "openid"
14+
openid_connect_verbose_logging:
15+
default: false
1416
openid_connect_token_scope:
1517
default: ""
1618
openid_connect_error_redirects:

lib/omniauth_open_id_connect.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class OpenIDConnect < OmniAuth::Strategies::OAuth2
1212
option :use_userinfo, true
1313
option :cache, lambda { |key, &blk| blk.call } # Default no-op cache
1414
option :error_handler, lambda { |error, message| nil } # Default no-op handler
15+
option :verbose_logger, lambda { |message| nil } # Default no-op handler
1516
option :passthrough_authorize_options, [:p]
1617
option :passthrough_token_options, [:p]
1718

@@ -23,10 +24,16 @@ class OpenIDConnect < OmniAuth::Strategies::OAuth2
2324
userinfo_endpoint: nil,
2425
auth_scheme: :basic_auth
2526

27+
def verbose_log(message)
28+
options.verbose_logger.call(message)
29+
end
30+
2631
def discover!
32+
verbose_log("Fetching discovery document from #{options[:client_options][:discovery_document]}")
2733
discovery_document = options.cache.call("openid_discovery_#{options[:client_options][:discovery_document]}") do
2834
client.request(:get, options[:client_options][:discovery_document], parse: :json).parsed
2935
end
36+
verbose_log("Discovery document loaded\n\n#{discovery_document.to_yaml}")
3037

3138
discovery_params = {
3239
authorize_url: "authorization_endpoint",
@@ -80,6 +87,7 @@ def token_params
8087

8188
def callback_phase
8289
if request.params["error"] && request.params["error_description"] && response = options.error_handler.call(request.params["error"], request.params["error_description"])
90+
verbose_log("Error handled, redirecting\n\n#{response.to_yaml}")
8391
return redirect(response)
8492
end
8593

@@ -106,7 +114,7 @@ def id_token_info
106114
# token was acquired via a direct server-server connection to the issuer
107115
@id_token_info ||= begin
108116
decoded = JWT.decode(access_token['id_token'], nil, false).first
109-
117+
verbose_log("Loaded JWT\n\n#{decoded.to_yaml}")
110118
JWT::Verify.verify_claims(decoded,
111119
verify_iss: true,
112120
iss: options[:client_options][:site],
@@ -118,13 +126,19 @@ def id_token_info
118126
verify_iat: true,
119127
verify_jti: false
120128
)
129+
verbose_log("Verified JWT\n\n#{decoded.to_yaml}")
121130

122131
decoded
123132
end
124133
end
125134

126135
def userinfo_response
127-
@raw_info ||= access_token.get(options[:client_options][:userinfo_endpoint]).parsed
136+
@raw_info ||= begin
137+
info = access_token.get(options[:client_options][:userinfo_endpoint]).parsed
138+
verbose_log("Fetched userinfo response\n\n#{info.to_yaml}")
139+
info
140+
end
141+
128142
return fail!(:csrf_detected, CallbackError.new(:csrf_detected, "CSRF detected")) unless @raw_info['sub'] == id_token_info['sub']
129143
@raw_info
130144
end

plugin.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def register_middleware(omniauth)
3737
end
3838
nil
3939
},
40+
verbose_logger: lambda { |message|
41+
return unless SiteSetting.openid_connect_verbose_logging
42+
Rails.logger.warn("OIDC Log: #{message}")
43+
},
4044
setup: lambda { |env|
4145
opts = env['omniauth.strategy'].options
4246
opts.deep_merge!(

0 commit comments

Comments
 (0)