FEATURE: Revoke and connect for Yahoo logins

This commit is contained in:
David Taylor 2018-07-27 16:20:47 +01:00
parent a9c959e3e2
commit 6296f63804
3 changed files with 58 additions and 2 deletions

View File

@ -127,7 +127,7 @@ export function findAll(siteSettings, capabilities, isMobileDevice) {
params.displayPopup = true;
}
if (["facebook", "google_oauth2", "twitter"].includes(name)) {
if (["facebook", "google_oauth2", "twitter", "yahoo"].includes(name)) {
params.canConnect = true;
}

View File

@ -18,7 +18,23 @@ class Auth::OpenIdAuthenticator < Auth::Authenticator
info&.email || ""
end
def after_authenticate(auth_token)
def can_revoke?
true
end
def revoke(user, skip_remote: false)
info = UserOpenId.where("url LIKE ?", "#{@identifier}%").find_by(user_id: user.id)
raise Discourse::NotFound if info.nil?
info.destroy!
true
end
def can_connect_existing_user?
true
end
def after_authenticate(auth_token, existing_account: nil)
result = Auth::Result.new
data = auth_token[:info]
@ -33,6 +49,11 @@ class Auth::OpenIdAuthenticator < Auth::Authenticator
user_open_id = UserOpenId.find_by_url(identity_url)
if existing_account && (user_open_id.nil? || existing_account.id != user_open_id.user_id)
user_open_id.destroy! if user_open_id
user_open_id = UserOpenId.create!(url: identity_url , user_id: existing_account.id, email: email, active: true)
end
if !user_open_id && @opts[:trusted] && user = User.find_by_email(email)
user_open_id = UserOpenId.create(url: identity_url , user_id: user.id, email: email, active: true)
end

View File

@ -22,4 +22,39 @@ describe Auth::OpenIdAuthenticator do
response = OpenStruct.new(identity_url: 'abc')
expect { auth.after_authenticate(info: {}, extra: { response: response }) }.to raise_error(Discourse::InvalidParameters)
end
it 'can connect to a different existing user account' do
authenticator = Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true)
user1 = Fabricate(:user)
user2 = Fabricate(:user)
UserOpenId.create!(url: "id/123" , user_id: user1.id, email: "bob@example.com", active: true)
hash = {
info: { email: user1.email }, extra: { response: OpenStruct.new(identity_url: 'id/123') }
}
result = authenticator.after_authenticate(hash, existing_account: user2)
expect(result.user.id).to eq(user2.id)
expect(UserOpenId.exists?(user_id: user1.id)).to eq(false)
expect(UserOpenId.exists?(user_id: user2.id)).to eq(true)
end
context 'revoke' do
let(:user) { Fabricate(:user) }
let(:authenticator) { Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true) }
it 'raises exception if no entry for user' do
expect { authenticator.revoke(user) }.to raise_error(Discourse::NotFound)
end
it 'revokes correctly' do
UserOpenId.create!(url: "id/123" , user_id: user.id, email: "bob@example.com", active: true)
expect(authenticator.can_revoke?).to eq(true)
expect(authenticator.revoke(user)).to eq(true)
expect(authenticator.description_for_user(user)).to eq("")
end
end
end