Skip to content

Commit

Permalink
Allow websocket connections with external auth (MarkUsProject#6912)
Browse files Browse the repository at this point in the history
  • Loading branch information
pretendWhale authored and Donny Wong committed Jan 31, 2024
1 parent 993a7e3 commit 4992a2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
if request.session[:auth_type] == 'remote'
self.current_user = User.find_by user_name: request.env['HTTP_X_FORWARDED_USER']
end
unless request.session[:user_name].nil?
self.current_user = User.find_by user_name: request.session[:user_name]
end
Expand Down
25 changes: 25 additions & 0 deletions spec/channels/application_cable/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,29 @@
end
end
end
context 'when connecting with external auth' do
context 'as an instructor' do
let(:instructor) { create :instructor }
it 'should connect' do
connect '/cable', session: { auth_type: 'remote' }, headers: { HTTP_X_FORWARDED_USER: instructor.user_name }
expect(connection.current_user.user_name).to eq(instructor.user_name)
end
context 'when role switched' do
let(:ta) { create :ta }
it 'should connect as the TA' do
connect '/cable', session: { auth_type: 'remote', user_name: ta.user_name },
headers: { HTTP_X_FORWARDED_USER: instructor.user_name }
expect(connection.current_user.user_name).to eq(ta.user_name)
end
end
end
context 'as a student' do
let(:student) { create :student }
let(:user_name) { student.user_name }
it 'should connect' do
connect '/cable', session: { auth_type: 'remote' }, headers: { HTTP_X_FORWARDED_USER: user_name }
expect(connection.current_user.user_name).to eq(user_name)
end
end
end
end

0 comments on commit 4992a2d

Please sign in to comment.