Skip to content

Commit 1130cd9

Browse files
balazsorban44Jincoco88912davidtaylorhq
authored
FEATURE: PKCE support (#86)
Introduces a new site setting (openid_connect_use_pkce) which enables PKCE support Co-authored-by: Jincoco88912 <[email protected]> Co-authored-by: David Taylor <[email protected]>
1 parent c79a5c4 commit 1130cd9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

config/locales/server.en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ en:
1515
openid_connect_overrides_email: "On every login, override the user's email using the openid-connect value. Works the same as the `auth_overrides_email` setting, but is specific to OpenID Connect logins."
1616
openid_connect_claims: "Explicitly define the claims for use with providers that don't pass data back based on scopes. (JSON)"
1717
openid_connect_match_by_email: "Use email address to match OpenID Connect authentications to existing Discourse user accounts."
18+
openid_connect_use_pkce: "Enable Proof Key for Code Exchange (PKCE) for OpenID Connect authentication."
1819
login:
1920
omniauth_error:
2021
openid_connect_discovery_error: Unable to fetch configuration from identity provider. Please try again.

config/settings.yml

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ discourse_openid_connect:
3535
textarea: true
3636
openid_connect_match_by_email:
3737
default: true
38+
openid_connect_use_pkce:
39+
default: false

lib/openid_connect_authenticator.rb

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# frozen_string_literal: true
2+
require "base64"
3+
require "openssl"
24

35
class OpenIDConnectAuthenticator < Auth::ManagedAuthenticator
46
def name
@@ -107,6 +109,14 @@ def register_middleware(omniauth)
107109
passthrough_authorize_options:
108110
SiteSetting.openid_connect_authorize_parameters.split("|"),
109111
claims: SiteSetting.openid_connect_claims,
112+
pkce: SiteSetting.openid_connect_use_pkce,
113+
pkce_options: {
114+
code_verifier: -> { generate_code_verifier },
115+
code_challenge: ->(code_verifier) do
116+
generate_code_challenge(code_verifier)
117+
end,
118+
code_challenge_method: "S256",
119+
},
110120
)
111121

112122
opts[:client_options][:connection_opts] = {
@@ -128,6 +138,14 @@ def register_middleware(omniauth)
128138
}
129139
end
130140

141+
def generate_code_verifier
142+
Base64.urlsafe_encode64(OpenSSL::Random.random_bytes(32)).tr("=", "")
143+
end
144+
145+
def generate_code_challenge(code_verifier)
146+
Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)).tr("+/", "-_").tr("=", "")
147+
end
148+
131149
def request_timeout_seconds
132150
GlobalSetting.openid_connect_request_timeout_seconds
133151
end

0 commit comments

Comments
 (0)