Skip to content

Commit

Permalink
SECURITY: Ensure that user has been authenticated.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxworld committed Feb 24, 2017
1 parent 3754b03 commit fbe51d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/services/user_authenticator.rb
Expand Up @@ -21,7 +21,10 @@ def has_authenticator?
end

def finish
authenticator.after_create_account(@user, @session) if authenticator
if authenticator && authenticated?
authenticator.after_create_account(@user, @session)
end

@session = nil
end

Expand Down
36 changes: 36 additions & 0 deletions spec/components/auth/user_authenticator_spec.rb
@@ -0,0 +1,36 @@
require 'rails_helper'

RSpec.describe UserAuthenticator do
let(:user) { Fabricate(:user, email: 'test@discourse.org') }

describe "#finish" do
before do
SiteSetting.enable_google_oauth2_logins = true
end

it "should execute provider's callback" do
user.update!(email: 'test@gmail.com')

authenticator = UserAuthenticator.new(user, { authentication: {
authenticator_name: Auth::GoogleOAuth2Authenticator.new.name,
email: user.email,
email_valid: true,
extra_data: { google_user_id: 1 }
}})

expect { authenticator.finish }.to change { GoogleUserInfo.count }.by(1)
end

describe "when session's email is different from user's email" do
it "should not execute provider's callback" do
authenticator = UserAuthenticator.new(user, { authentication: {
authenticator_name: Auth::GoogleOAuth2Authenticator.new.name,
email: 'test@gmail.com',
email_valid: true
}})

expect { authenticator.finish }.to_not change { GoogleUserInfo.count }
end
end
end
end
2 changes: 2 additions & 0 deletions spec/controllers/users_controller_spec.rb
Expand Up @@ -611,6 +611,8 @@ def post_user
auth = session[:authentication] = {}
auth[:authenticator_name] = 'twitter'
auth[:extra_data] = twitter_auth
auth[:email_valid] = true
auth[:email] = @user.email

post_user

Expand Down

0 comments on commit fbe51d6

Please sign in to comment.