-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the way user tokens are stored
This commit provides an alternative implementation for storing tokens compared to the parent commit. The idea is that we will not only need to store access and refresh tokens obtained via Omniauth, but also the ones to access third party services that will most likely be obtained through OAuth 2.0 Token Exchange. This structure allows to store all of these tokens in the same data model, while keeping the implementation separated from the back-channel logout logic.
- Loading branch information
1 parent
9a42699
commit 201f5cd
Showing
10 changed files
with
244 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
modules/openid_connect/app/services/openid_connect/create_open_project_token.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#-- copyright | ||
# OpenProject is a project management system. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2017 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# + | ||
|
||
module OpenIDConnect | ||
class CreateOpenProjectToken | ||
def initialize(session) | ||
@session = session | ||
end | ||
|
||
def call | ||
if access_token.blank? | ||
Rails.logger.error("Could not associate token to session: No access token") | ||
return | ||
end | ||
|
||
user_session = find_user_session | ||
if user_session.nil? | ||
Rails.logger.error("Could not associate token to session: User session not found") | ||
return | ||
end | ||
|
||
user_session.oidc_user_tokens.open_project.create!(access_token:, refresh_token:) | ||
end | ||
|
||
private | ||
|
||
def find_user_session | ||
private_session_id = @session.id.private_id | ||
::Sessions::UserSession.find_by(session_id: private_session_id) | ||
end | ||
|
||
def access_token | ||
@session["omniauth.oidc_access_token"] | ||
end | ||
|
||
def refresh_token | ||
@session["omniauth.oidc_refresh_token"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
modules/openid_connect/spec/services/openid_connect/create_open_project_token_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
require "spec_helper" | ||
|
||
RSpec.describe OpenIDConnect::CreateOpenProjectToken do | ||
subject { described_class.new(session).call } | ||
|
||
let(:session) do | ||
instance_double(ActionDispatch::Request::Session, | ||
id: instance_double(Rack::Session::SessionId, private_id: 42)) | ||
end | ||
|
||
let(:session_data) do | ||
{ | ||
"omniauth.oidc_access_token" => access_token, | ||
"omniauth.oidc_refresh_token" => refresh_token | ||
} | ||
end | ||
|
||
let(:access_token) { "access-token-foo" } | ||
let(:refresh_token) { "refresh-token-bar" } | ||
|
||
let!(:user_session) { create(:user_session, session_id: session.id.private_id) } | ||
|
||
before do | ||
allow(session).to receive(:[]) { |k| session_data[k] } | ||
allow(Rails.logger).to receive(:error) | ||
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.audience).to eq "open_project" | ||
end | ||
|
||
it "logs no error" do | ||
subject | ||
expect(Rails.logger).not_to have_received(:error) | ||
end | ||
|
||
context "when there is no refresh token" do | ||
let(:refresh_token) { nil } | ||
|
||
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 be_nil | ||
expect(token.audience).to eq "open_project" | ||
end | ||
|
||
it "logs no error" do | ||
subject | ||
expect(Rails.logger).not_to have_received(:error) | ||
end | ||
end | ||
|
||
context "when there is no access token" do | ||
let(:access_token) { nil } | ||
|
||
it "does not create a user token" do | ||
expect { subject }.not_to change(OpenIDConnect::UserToken, :count) | ||
end | ||
|
||
it "logs an error" do | ||
subject | ||
expect(Rails.logger).to have_received(:error) | ||
end | ||
end | ||
|
||
context "when the user session can't be found" do | ||
let!(:user_session) { create(:user_session, session_id: SecureRandom.uuid) } | ||
|
||
it "does not create a user token" do | ||
expect { subject }.not_to change(OpenIDConnect::UserToken, :count) | ||
end | ||
|
||
it "logs an error" do | ||
subject | ||
expect(Rails.logger).to have_received(:error) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters