forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes zammad#4889 - Third-party authorization handling is not using t…
…he unique identifier correctly (uid).
- Loading branch information
1 parent
abf5295
commit 07e8af4
Showing
13 changed files
with
166 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider | ||
include Mixin::RequiredSubPaths | ||
|
||
attr_reader :auth_hash, :user, :info, :uid | ||
|
||
def initialize(auth_hash, user = nil) | ||
@auth_hash = auth_hash | ||
@uid = auth_hash['uid'] | ||
@info = auth_hash['info'] || {} | ||
|
||
@user = user.presence || fetch_user | ||
end | ||
|
||
def name | ||
self.class.name.demodulize.downcase | ||
end | ||
|
||
private | ||
|
||
def fetch_user | ||
if Setting.get('auth_third_party_auto_link_at_inital_login') | ||
user = find_user | ||
|
||
return user if user.present? | ||
end | ||
|
||
User.create_from_hash!(auth_hash) | ||
end | ||
|
||
def find_user | ||
return if info['email'].nil? | ||
|
||
User.find_by(email: info['email'].downcase) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::Facebook < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::GitHub < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::GitLab < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::GoogleOauth2 < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::Linkedin < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::MicrosoftOffice365 < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::Saml < Authorization::Provider | ||
private | ||
|
||
def find_user | ||
user = User.find_by(login: uid) | ||
|
||
if !user && !Setting.get('user_email_multiple_use') && info['email'].present? | ||
user = User.find_by(email: info['email'].downcase) | ||
end | ||
|
||
user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::Twitter < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Authorization::Provider::Weibo < Authorization::Provider | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,72 @@ | |
end | ||
end | ||
|
||
describe 'Account linking' do | ||
let(:auth_hash) do | ||
{ | ||
'info' => auth_info, | ||
'uid' => auth_uid, | ||
'provider' => provider, | ||
'credentials' => auth_credentials, | ||
} | ||
end | ||
let(:auth_info) { {} } | ||
let(:auth_uid) { SecureRandom.uuid } | ||
let(:auth_credentials) do | ||
{ | ||
'token' => '1234', | ||
'secret' => '1234', | ||
} | ||
end | ||
let(:provider) { 'saml' } | ||
let(:user) { create(:user, login: auth_uid) } | ||
|
||
before do | ||
Setting.set('auth_third_party_auto_link_at_inital_login', true) | ||
|
||
user | ||
end | ||
|
||
shared_examples 'links account with email address' do |type:| | ||
let(:provider) { type } | ||
|
||
it 'linked account' do | ||
authorization = described_class.create_from_hash(auth_hash) | ||
|
||
expect(authorization.user_id).to eq(user.id) | ||
end | ||
end | ||
|
||
context 'when saml is the provider' do | ||
context 'when auth provider provides no email address' do | ||
it 'linked account with uid' do | ||
authorization = described_class.create_from_hash(auth_hash) | ||
|
||
expect(authorization.user_id).to eq(user.id) | ||
end | ||
end | ||
end | ||
|
||
context 'when auth provider provides an email address' do | ||
let(:email) { '[email protected]' } | ||
let(:auth_info) do | ||
{ | ||
'email' => email, | ||
} | ||
end | ||
let(:user) { create(:user, login: auth_uid, email: email) } | ||
|
||
include_examples 'links account with email address', type: 'github' | ||
include_examples 'links account with email address', type: 'gitlab' | ||
include_examples 'links account with email address', type: 'twitter' | ||
include_examples 'links account with email address', type: 'facebook' | ||
include_examples 'links account with email address', type: 'linkedin' | ||
include_examples 'links account with email address', type: 'microsoft_office365' | ||
include_examples 'links account with email address', type: 'google_oauth2' | ||
include_examples 'links account with email address', type: 'weibo' | ||
end | ||
end | ||
|
||
describe 'Account linking notification', sends_notification_emails: true do | ||
subject(:authorization) { create(:authorization, user: agent, provider: provider) } | ||
|
||
|