Skip to content

Scratch project saving and loading outline#695

Open
zetter-rpf wants to merge 3 commits intomainfrom
scratch-auth-and-loading-spike
Open

Scratch project saving and loading outline#695
zetter-rpf wants to merge 3 commits intomainfrom
scratch-auth-and-loading-spike

Conversation

@zetter-rpf
Copy link
Contributor

@zetter-rpf zetter-rpf commented Feb 26, 2026

Status

What's changed?

This is an outline/scaffold for the routes needed to show & update scratch projects and show and create scratch assets. There is a static project and asset that is loaded whatever ID is provided, and updates are authorised but not performed.

The main reason for this work is to demonstrate a route to authenticating asset creation and project updates. It can also be a base that we can add in the future to load and save real data.

See the documentation for the endpoints that scratch expects.

The authentication mechanism

We use header-based auth elsewhere in the API but Scratch does not allow us to add headers to requests, but it does send cookies with the asset post and project put requests.

The flow is:

  • When loading a code editor project, also set a cookie with the authentication header on editor-api
  • When updating scratch projects or creating assets, verify this cookie.

I've limited where this cookie can be used to just the scratch controllers (and CORS rules block sending cookies for other routes). As the auth token was already accessible to Javascript, I don't think this adds additional risk.

Not considered in this PR

  • How we want to store Scratch projects and assets
  • What ids we want to use for Scratch projects
  • If we want to make scratch projects harder to access (e.g. by using hard to guess Ids or a projectToken.).

After deploy

  • we already have *.raspberrypi.org as an allowed origin on editor api so we do not need to add any more.

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.
This shows a static project for now, later we will likely pull project data from the database or object store.

The updating always succeeds and returns an ok but doesn't perform any updates.
@cla-bot cla-bot bot added the cla-signed label Feb 26, 2026
@zetter-rpf zetter-rpf marked this pull request as ready for review February 26, 2026 09:22
Copilot AI review requested due to automatic review settings February 26, 2026 09:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a scaffold for Scratch project integration by adding routes and authentication mechanisms for Scratch to save and load projects and assets through the editor API. The implementation demonstrates cookie-based authentication as a workaround for Scratch's inability to send custom headers, while the actual project/asset storage is stubbed with static responses.

Changes:

  • Added cookie-based authentication mechanism for endpoints that can't send Authorization headers
  • Created Scratch-specific API routes for projects and assets with feature flag gating (cat_mode)
  • Added CODE_EDITOR_SCRATCH as a new project type
  • Configured CORS to allow credentials for Scratch and projects routes

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
app/models/project.rb Adds CODE_EDITOR_SCRATCH as a new project type constant
app/controllers/concerns/identifiable_by_cookie.rb New concern for cookie-based authentication extracting token from scratch_auth cookie
app/controllers/api/scratch/scratch_controller.rb Base controller for Scratch endpoints with cookie auth and cat_mode feature flag checks
app/controllers/api/scratch/projects_controller.rb Stub implementation for showing/updating Scratch projects (returns static JSON)
app/controllers/api/scratch/assets_controller.rb Stub implementation for showing/creating Scratch assets (returns static SVG/JSON)
app/controllers/api/projects_controller.rb Sets scratch_auth cookie when loading CODE_EDITOR_SCRATCH projects with cat_mode enabled
config/routes.rb Adds Scratch namespace routes for projects and assets
config/initializers/cors.rb Enables credentials for Scratch and projects routes to allow cookie transmission
app/views/api/scratch/projects/show.json Static Scratch project JSON with example blocks and sprites
app/views/api/scratch/assets/show.svg Static SVG asset (teapot image)
spec/requests/projects/show_spec.rb Tests cookie setting behavior for CODE_EDITOR_SCRATCH projects
spec/features/scratch/showing_a_scratch_project_spec.rb Tests GET endpoint returns Scratch project JSON
spec/features/scratch/updating_a_scratch_project_spec.rb Tests PUT endpoint with cookie authentication and feature flag
spec/features/scratch/showing_a_scratch_asset_spec.rb Tests GET asset endpoint returns SVG
spec/features/scratch/creating_a_scratch_asset_spec.rb Tests POST asset endpoint with cookie authentication and feature flag

This comment was marked as outdated.

This comment was marked as outdated.

For now, the show always returns a static asset and the create always succeeds but doesn't do any saving.

Note that the /internalapi/asset/.../get/ route is dictated by Scratch.
@zetter-rpf zetter-rpf force-pushed the scratch-auth-and-loading-spike branch from a54f922 to 0c6cef7 Compare February 26, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants