Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Discover audiences from access token

Jan Sandbrinkc4caf9851700
diff --git a/modules/openid_connect/app/services/openid_connect/associate_user_token.rb b/modules/openid_connect/app/services/openid_connect/associate_user_token.rb
index 893107e8647..3c5f7da91e6 100644
--- a/modules/openid_connect/app/services/openid_connect/associate_user_token.rb
+++ b/modules/openid_connect/app/services/openid_connect/associate_user_token.rb
@@ -30,8 +30,9 @@
module OpenIDConnect
class AssociateUserToken
- def initialize(user)
+ def initialize(user, jwt_parser: JwtParser.new(verify_audience: false, required_claims: ["aud"]))
@user = user
+ @jwt_parser = jwt_parser
end
def call(access_token:, refresh_token: nil, known_audiences: [], clear_previous: false)
@@ -47,9 +48,22 @@ module OpenIDConnect
@user.oidc_user_tokens.destroy_all if clear_previous
- token = @user.oidc_user_tokens.build(access_token:, refresh_token:, audiences: Array(known_audiences))
- # We should discover further audiences from the token in the future
+ token = @user.oidc_user_tokens.build(access_token:, refresh_token:)
+ token.audiences = merge_audiences(known_audiences, discover_audiences(access_token))
token.save! if token.audiences.any?
end
+
+ private
+
+ def discover_audiences(access_token)
+ decoded, = @jwt_parser.parse(access_token)
+ Array(decoded["aud"])
+ rescue StandardError
+ []
+ end
+
+ def merge_audiences(*args)
+ args.flatten.uniq
+ end
end
end
diff --git a/modules/openid_connect/spec/services/openid_connect/associate_user_token_spec.rb b/modules/openid_connect/spec/services/openid_connect/associate_user_token_spec.rb
index 0d80085ff57..aa7db97e443 100644
--- a/modules/openid_connect/spec/services/openid_connect/associate_user_token_spec.rb
+++ b/modules/openid_connect/spec/services/openid_connect/associate_user_token_spec.rb
@@ -30,7 +30,7 @@
require "spec_helper"
RSpec.describe OpenIDConnect::AssociateUserToken do
- subject { described_class.new(user).call(**args) }
+ subject { described_class.new(user, jwt_parser: parser).call(**args) }
let(:user) { create(:user) }
@@ -39,6 +39,9 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
let(:access_token) { "access-token-foo" }
let(:refresh_token) { "refresh-token-bar" }
+ let(:parser) { instance_double(OpenIDConnect::JwtParser, parse: [parsed_jwt, nil]) }
+ let(:parsed_jwt) { { "aud" => ["aud1", "aud2"] } }
+
before do
allow(Rails.logger).to receive(:error)
end
@@ -50,7 +53,7 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
expect(token.user_id).to eq user.id
expect(token.access_token).to eq access_token
expect(token.refresh_token).to eq refresh_token
- expect(token.audiences).to eq ["io"]
+ expect(token.audiences).to contain_exactly("io", "aud1", "aud2")
end
it "logs no error" do
@@ -58,6 +61,44 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
expect(Rails.logger).not_to have_received(:error)
end
+ context "when the JWT encodes aud as a string" do
+ let(:parsed_jwt) { { "aud" => "aud1" } }
+
+ it "creates a correct user token", :aggregate_failures do
+ expect { subject }.to change(OpenIDConnect::UserToken, :count).by(1)
+
+ token = OpenIDConnect::UserToken.last
+ expect(token.access_token).to eq access_token
+ expect(token.refresh_token).to eq refresh_token
+ expect(token.audiences).to contain_exactly("io", "aud1")
+ end
+
+ it "logs no error" do
+ subject
+ expect(Rails.logger).not_to have_received(:error)
+ end
+ end
+
+ context "when the access token is not a valid JWT" do
+ before do
+ allow(parser).to receive(:parse).and_raise("Oops, not a JWT!")
+ end
+
+ it "creates a correct user token", :aggregate_failures do
+ expect { subject }.to change(OpenIDConnect::UserToken, :count).by(1)
+
+ token = OpenIDConnect::UserToken.last
+ expect(token.access_token).to eq access_token
+ expect(token.refresh_token).to eq refresh_token
+ expect(token.audiences).to contain_exactly("io")
+ end
+
+ it "logs no error" do
+ subject
+ expect(Rails.logger).not_to have_received(:error)
+ end
+ end
+
context "when there is no refresh token" do
let(:refresh_token) { nil }
@@ -68,7 +109,7 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
expect(token.user_id).to eq user.id
expect(token.access_token).to eq access_token
expect(token.refresh_token).to be_nil
- expect(token.audiences).to eq ["io"]
+ expect(token.audiences).to contain_exactly("io", "aud1", "aud2")
end
it "logs no error" do
@@ -106,14 +147,32 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
context "when there is no audience" do
let(:args) { { access_token:, refresh_token:, known_audiences: [] } }
- it "does not create a user token" do
- expect { subject }.not_to change(OpenIDConnect::UserToken, :count)
+ it "creates a correct user token", :aggregate_failures do
+ expect { subject }.to change(OpenIDConnect::UserToken, :count).by(1)
+
+ token = OpenIDConnect::UserToken.last
+ expect(token.access_token).to eq access_token
+ expect(token.refresh_token).to eq refresh_token
+ expect(token.audiences).to contain_exactly("aud1", "aud2")
end
it "logs no error" do
subject
expect(Rails.logger).not_to have_received(:error)
end
+
+ context "and the token has no audience defined" do
+ let(:parsed_jwt) { { "sub" => "ject" } }
+
+ it "does not create a user token" do
+ expect { subject }.not_to change(OpenIDConnect::UserToken, :count)
+ end
+
+ it "logs no error" do
+ subject
+ expect(Rails.logger).not_to have_received(:error)
+ end
+ end
end
context "when another user token existed before" do
@@ -131,7 +190,7 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
expect(token.user_id).to eq user.id
expect(token.access_token).to eq access_token
expect(token.refresh_token).to eq refresh_token
- expect(token.audiences).to eq ["io"]
+ expect(token.audiences).to contain_exactly("io", "aud1", "aud2")
end
context "and when previous tokens shall be cleared" do
@@ -149,7 +208,7 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
expect(token.user_id).to eq user.id
expect(token.access_token).to eq access_token
expect(token.refresh_token).to eq refresh_token
- expect(token.audiences).to eq ["io"]
+ expect(token.audiences).to contain_exactly("io", "aud1", "aud2")
end
it "logs no error" do
@@ -158,4 +217,15 @@ RSpec.describe OpenIDConnect::AssociateUserToken do
end
end
end
+
+ context "when audiences from token and arguments overlap" do
+ let(:parsed_jwt) { { "aud" => ["io", "aud2"] } }
+
+ it "normalizes the audience array" do
+ subject
+
+ token = OpenIDConnect::UserToken.last
+ expect(token.audiences).to contain_exactly("io", "aud2")
+ end
+ end
end