MishkaCmsCore.Runtime.Plugs.ExtensionGatePlug (Mishka CMS Core v0.0.5-alpha.13)

Copy Markdown View Source

Lets a request through to an extension's API route only while that extension is switched on for the request's site, the same switch that takes the extension's entries out of the admin sidebar (MishkaCmsCore.Install.Extensions.active?/2).

MishkaCmsCore.ExtensionRouter.extension_routes/1 puts every route of routes(:api) behind it, one scope per extension, so an extension writes nothing for it and a host cannot forget it:

scope "/", alias: false do
  pipe_through {MishkaCmsCore.Runtime.Plugs.ExtensionGatePlug, extension: MishkaBlogWeb}
  get "/feed", MishkaBlogWeb.FeedController, :show
end

The site is conn.assigns.current_site, which the host's pipeline resolved before this runs. A request with no site, or one for a site that switched the extension off, is the 404 an address no route has, raised as MishkaCmsCore.ErrorsExceptions.NotFoundError in a Plug.Conn.WrapperError so the host's error page is drawn for the site it resolved, with cache-control: no-store.

An extension's API route keeps no session: a session the pipeline fetched is read but never written back, so the answer carries no cookie and a cache in front of the site may keep it. A GET, HEAD or OPTIONS reads it as it is; any other method reads an empty one, so get_session/2 answers nil. The pipeline has no CSRF check, and a form on another site posting here would otherwise act as the visitor signed in to this one. A route taking such a method authenticates the request itself, as a webhook checks its signature.

The extension's OTP app is read from its Routes module once and kept in :persistent_term.

Summary

Functions

The OTP app extension (MishkaBlogWeb) belongs to, read from its Routes module.

Functions

app(extension)

@spec app(module()) :: atom() | nil

The OTP app extension (MishkaBlogWeb) belongs to, read from its Routes module.