Skip to content

Commit 3f9e975

Browse files
committed
Create cookie for scratch auth when loading projects
This feels like a sensible place as it should always be set before a scratch project is loaded We may need to extend this when creating/remixing projects if it uses another path. Alternatively we could make a new endpoint that creates a cookie.
1 parent 339c014 commit 3f9e975

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

app/controllers/api/projects_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
module Api
66
class ProjectsController < ApiController
7+
include ActionController::Cookies
8+
79
before_action :authorize_user, only: %i[create update index destroy]
810
before_action :load_project, only: %i[show update destroy show_context]
911
before_action :load_projects, only: %i[index]
1012
load_and_authorize_resource
1113
before_action :verify_lesson_belongs_to_school, only: :create
1214
after_action :pagination_link_header, only: %i[index]
15+
before_action :set_auth_cookie_for_scratch, only: %i[show]
1316

1417
def index
1518
@paginated_projects = @projects.page(params[:page])
@@ -59,6 +62,18 @@ def show_context
5962

6063
private
6164

65+
def set_auth_cookie_for_scratch
66+
return unless @project.project_type == Project::Types::CODE_EDITOR_SCRATCH
67+
return unless Flipper.enabled?(:cat_mode, school)
68+
69+
cookies[:scratch_auth] = {
70+
value: request.headers['Authorization'],
71+
secure: Rails.env.production?,
72+
same_site: :strict,
73+
http_only: true
74+
}
75+
end
76+
6277
def verify_lesson_belongs_to_school
6378
return if base_params[:lesson_id].blank?
6479
return if school&.lessons&.pluck(:id)&.include?(base_params[:lesson_id])

app/models/project.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Types
55
PYTHON = 'python'
66
HTML = 'html'
77
SCRATCH = 'scratch'
8+
CODE_EDITOR_SCRATCH = 'code_editor_scratch'
89
end
910

1011
belongs_to :school, optional: true

config/initializers/cors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
end
2424

2525
def standard_cors_options
26+
resource '/api/projects/*', headers: :any, methods: %i[get post patch put delete], credentials: true, expose: ['Link']
2627
resource '*', headers: :any, methods: %i[get post patch put delete], expose: ['Link']
2728
end

spec/requests/projects/show_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,43 @@
5454
end
5555
end
5656

57+
context 'when setting scratch auth cookie' do
58+
let(:project_type) { Project::Types::PYTHON }
59+
let!(:project) { create(:project, school:, user_id: teacher.id, locale: nil, project_type:) }
60+
61+
before do
62+
Flipper.disable :cat_mode
63+
Flipper.disable_actor :cat_mode, school
64+
end
65+
66+
it 'does not set auth cookie when project is not scratch' do
67+
get("/api/projects/#{project.identifier}", headers:)
68+
69+
expect(response).to have_http_status(:ok)
70+
expect(response.cookies['scratch_auth']).to be_nil
71+
end
72+
73+
context 'when project is code editor scratch' do
74+
let(:project_type) { Project::Types::CODE_EDITOR_SCRATCH }
75+
76+
it 'does not set auth cookie when cat_mode is not enabled' do
77+
get("/api/projects/#{project.identifier}", headers:)
78+
79+
expect(response).to have_http_status(:ok)
80+
expect(response.cookies['scratch_auth']).to be_nil
81+
end
82+
83+
it 'sets auth cookie to auth header' do
84+
Flipper.enable_actor :cat_mode, school
85+
86+
get("/api/projects/#{project.identifier}", headers:)
87+
88+
expect(response).to have_http_status(:ok)
89+
expect(cookies['scratch_auth']).to eq(UserProfileMock::TOKEN)
90+
end
91+
end
92+
end
93+
5794
context 'when loading a student\'s project' do
5895
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id]) }
5996
let(:lesson) { create(:lesson, school:, school_class:, user_id: teacher.id, visibility: 'students') }

0 commit comments

Comments
 (0)