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

Handles origin_user_id on OneDrive

Marcello Rochac5de1f2577db
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index e7377b80c12..4842b08121a 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -122,7 +122,7 @@ module OAuthClients
OAuthClientToken.create(
user: @user,
oauth_client: @oauth_client,
- origin_user_id: rack_access_token.raw_attributes[:user_id], # ID of user at OAuth2 Authorization Server
+ origin_user_id: @config.extract_origin_user_id(rack_access_token), # ID of user at OAuth2 Authorization Server
access_token: rack_access_token.access_token,
token_type: rack_access_token.token_type, # :bearer
refresh_token: rack_access_token.refresh_token,
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
index 7b54125acb7..047e028946e 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
@@ -37,7 +37,6 @@ module Storages
def initialize(storage)
@uri = storage.uri
@oauth_client = storage.oauth_client.freeze
- super()
end
def authorization_state_check(token)
@@ -55,6 +54,10 @@ module Storages
end
end
+ def extract_origin_user_id(rack_access_token)
+ rack_access_token.raw_attributes[:user_id]
+ end
+
def scope
[]
end
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
index d22346c04af..847f5e67e21 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
@@ -41,7 +41,6 @@ module Storages
@uri = storage.uri
@oauth_client = storage.oauth_client
@oauth_uri = URI('https://login.microsoftonline.com/').normalize
- super()
end
def authorization_state_check(access_token)
@@ -55,6 +54,20 @@ module Storages
end
end
+ def extract_origin_user_id(rack_access_token)
+ Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ response = http.get('/v1.0/me',
+ { Authorization: "Bearer #{rack_access_token.access_token}", Accept: 'application/json' })
+
+ # if response == Net::HTTPSuccess
+ parsed = JSON.parse(response.body)
+ parsed['userPrincipalName']
+ # else
+ # nil
+ # end
+ end
+ end
+
def scope
DEFAULT_SCOPES
end
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index 867561a7505..7c0263a59a0 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -173,6 +173,13 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
expect(subject.success).to be_truthy
expect(subject.result).to be_a OAuthClientToken
end
+
+ it 'fills in the origin_user_id' do
+ expect { subject }.to change(OAuthClientToken, :count).by(1)
+ last_token = OAuthClientToken.where(access_token: 'yjTDZ...RYvRH').last
+
+ expect(last_token.origin_user_id).to eq('admin')
+ end
end
context 'with known error' do
diff --git a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
index 679dab96e2c..c6dfa2a3d3c 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -39,6 +39,55 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
described_class.new(user:, configuration: storage.oauth_configuration)
end
+ describe '#code_to_token' do
+ let(:code) { 'wow.such.code.much.token' }
+ let(:code_to_token_response) do
+ {
+ access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q...",
+ token_type: "Bearer",
+ expires_in: 3599,
+ scope: "Mail.Read User.Read",
+ refresh_token: "AwABAAAAvPM1KaPlrEqdFSBzjqfTGAMxZGUTdM0t4B4..."
+ }.to_json
+ end
+ let(:me_response) do
+ {
+ businessPhones: [
+ "+45 123 4567 8901"
+ ],
+ displayName: "Sheev Palpatine ",
+ givenName: "Sheev",
+ jobTitle: "Galatic Senator",
+ mobilePhone: "+45 123 4567 8901",
+ officeLocation: "500 Republica",
+ preferredLanguage: "en-US",
+ surname: "Palpatine",
+ userPrincipalName: "[email protected]",
+ id: "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
+ }.to_json
+ end
+
+ before do
+ stub_request(:post, 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token')
+ .to_return(status: 200, body: code_to_token_response, headers: { 'Content-Type' => 'application/json' })
+
+ stub_request(:get, 'https://graph.microsoft.com/v1.0/me')
+ .with(headers: { Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q..." })
+ .to_return(status: 200, body: me_response, headers: { 'Content-Type' => 'application/json' })
+ end
+
+ it 'fills in the origin_user_id' do
+ expect { subject.code_to_token(code) }.to change(OAuthClientToken, :count).by(1)
+
+ last_token = OAuthClientToken
+ .where(access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q...")
+ .last
+
+ expect(last_token.origin_user_id).to eq('[email protected]')
+ end
+ end
+
describe '#get_authorization_uri' do
it 'always add the necessary scopes' do
uri = connection_manager.get_authorization_uri(state: nil)