MCPcopy Create free account
hub / github.com/Botloader/botloader / plugin_middleware

Function plugin_middleware

components/webapi/src/middlewares/plugins.rs:21–49  ·  view source on GitHub ↗
(
    State(state): State<AppState>,
    request: Request,
    next: Next,
)

Source from the content-addressed store, hash-verified

19}
20
21pub async fn plugin_middleware(
22 State(state): State<AppState>,
23 request: Request,
24 next: Next,
25) -> Result<Response, ApiErrorResponse> {
26 // running extractors requires a `axum::http::request::Parts`
27 let (mut parts, body) = request.into_parts();
28
29 let path: Path<PluginPath> = parts.extract().await.unwrap();
30 let mut request = Request::from_parts(parts, body);
31
32 let session: Option<&LoggedInSession> = request.extensions().get();
33 let plugin = fetch_plugin(&state.db, path.plugin_id).await?;
34
35 if !plugin.is_public {
36 if let Some(session) = session {
37 if plugin.author_id != session.session.user.id {
38 return Err(ApiErrorResponse::NoAccessToPlugin);
39 }
40
41 // checks passed, we have access
42 } else {
43 return Err(ApiErrorResponse::NoAccessToPlugin);
44 }
45 }
46
47 request.extensions_mut().insert(plugin);
48 Ok(next.run(request).await)
49}
50
51pub async fn fetch_plugin(config_store: &Db, plugin_id: u64) -> ApiResult<Plugin> {
52 config_store.get_plugin(plugin_id).await.map_err(|err| {

Callers

nothing calls this directly

Calls 3

fetch_pluginFunction · 0.85
getMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected