Skip to content

Commit cf11903

Browse files
hananasenspuun
andauthored
[oauth] Default to "sub" and "client_id" when preferred_username_claims is not configured (#1779)
### WHAT is this pull request doing? Unlike RabbitMQs oauth2.0 implementation, LavinMQ does not have a built-in default for `preferred_username_claims`. Without it set, LavinMQ won't know which JWT claim to use as the username. `DEBUG lmq.oauth2 authentication failed for user "": Could not decode token - No username found in JWT claims (tried: )` Hence, it needs to be set in the config. I think it would be a good idea to fall back to 'sub', 'client_id' in those cases, as that is the behavior for RabbitMQ . ### HOW can this pull request be tested? Specs. --------- Co-authored-by: Jon Börjesson <jon@84codes.com>
1 parent efce866 commit cf11903

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

spec/oauth_config_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ describe LavinMQ::Config do
1212
config.oauth_jwks_cache_ttl.should eq(1.hours)
1313
end
1414

15-
it "sets default oauth_preferred_username_claims to empty array" do
15+
it "sets default oauth_preferred_username_claims to sub and client_id" do
1616
config = LavinMQ::Config.new
17-
config.oauth_preferred_username_claims.should be_empty
17+
config.oauth_preferred_username_claims.should eq(["sub", "client_id"])
1818
end
1919

2020
it "sets default oauth_additional_scopes_key to nil" do

spec/token_parser_spec.cr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module TokenParserTestHelper
55
extend self
66

77
def create_token_parser(
8-
preferred_username_claims = ["preferred_username"],
8+
preferred_username_claims = ["sub", "client_id"],
99
resource_server_id : String? = nil,
1010
scope_prefix : String? = nil,
1111
additional_scopes_key : String? = nil,
@@ -79,6 +79,23 @@ describe LavinMQ::Auth::JWT::TokenParser do
7979
claims.username.should eq("sub-user")
8080
end
8181

82+
it "defaults to 'sub' claim when preferred_username_claims is not configured" do
83+
parser = TokenParserTestHelper.create_token_parser
84+
payload = LavinMQ::Auth::JWT::Payload.new(exp: RoughTime.utc.to_unix + 3600, sub: "sub-user")
85+
token = TokenParserTestHelper.create_mock_token(payload)
86+
claims = parser.parse(token)
87+
claims.username.should eq("sub-user")
88+
end
89+
90+
it "falls back to 'client_id' when 'sub' is missing and no claims configured" do
91+
parser = TokenParserTestHelper.create_token_parser
92+
payload = LavinMQ::Auth::JWT::Payload.new(exp: RoughTime.utc.to_unix + 3600)
93+
payload["client_id"] = JSON::Any.new("my-service-account")
94+
token = TokenParserTestHelper.create_mock_token(payload)
95+
claims = parser.parse(token)
96+
claims.username.should eq("my-service-account")
97+
end
98+
8299
it "raises when no username claim is found" do
83100
parser = TokenParserTestHelper.create_token_parser(["email", "preferred_username"])
84101
payload = LavinMQ::Auth::JWT::Payload.new(exp: RoughTime.utc.to_unix + 3600, sub: "sub-user")

src/lavinmq/config/options.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ module LavinMQ
356356
@[IniOpt(section: "oauth", ini_name: resource_server_id)]
357357
property oauth_resource_server_id : String? = nil
358358
@[IniOpt(section: "oauth", ini_name: preferred_username_claims)]
359-
property oauth_preferred_username_claims = Array(String).new
359+
property oauth_preferred_username_claims : Array(String) = ["sub", "client_id"]
360360
@[IniOpt(section: "oauth", ini_name: additional_scopes_key)]
361361
property oauth_additional_scopes_key : String? = nil
362362
@[IniOpt(section: "oauth", ini_name: scope_prefix)]

0 commit comments

Comments
 (0)