2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 14:50:21 +02:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 20:34:31 +01:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-01-20 14:45:44 +00:00
|
|
|
use std::path::Path;
|
2019-08-14 17:03:02 +02:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2019-10-11 11:41:54 -07:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_query_permission",
|
|
|
|
s.stateful_json_op(op_query_permission),
|
2019-10-11 11:41:54 -07:00
|
|
|
);
|
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_revoke_permission",
|
|
|
|
s.stateful_json_op(op_revoke_permission),
|
2019-10-11 11:41:54 -07:00
|
|
|
);
|
2019-11-12 00:33:29 +09:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_request_permission",
|
|
|
|
s.stateful_json_op(op_request_permission),
|
2019-11-12 00:33:29 +09:00
|
|
|
);
|
2019-10-11 11:41:54 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 00:22:53 +09:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PermissionArgs {
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
path: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_query_permission(
|
2020-02-08 20:34:31 +01:00
|
|
|
state: &State,
|
2019-10-28 00:22:53 +09:00
|
|
|
args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-10-28 00:22:53 +09:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 20:34:31 +01:00
|
|
|
let state = state.borrow();
|
2020-08-18 21:29:32 +01:00
|
|
|
let permissions = &state.permissions;
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 21:29:32 +01:00
|
|
|
let perm = match args.name.as_ref() {
|
|
|
|
"read" => permissions.query_read(&path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.query_write(&path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.query_net_url(&args.url.as_deref())?,
|
|
|
|
"env" => permissions.query_env(),
|
|
|
|
"run" => permissions.query_run(),
|
|
|
|
"plugin" => permissions.query_plugin(),
|
|
|
|
"hrtime" => permissions.query_hrtime(),
|
|
|
|
n => return Err(OpError::other(format!("No such permission name: {}", n))),
|
|
|
|
};
|
2019-10-28 00:22:53 +09:00
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_revoke_permission(
|
2020-02-08 20:34:31 +01:00
|
|
|
state: &State,
|
2019-08-26 14:50:21 +02:00
|
|
|
args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-10-28 00:22:53 +09:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 20:34:31 +01:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = &mut state.permissions;
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 21:29:32 +01:00
|
|
|
let perm = match args.name.as_ref() {
|
|
|
|
"read" => permissions.revoke_read(&path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.revoke_write(&path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.revoke_net(&args.url.as_deref())?,
|
|
|
|
"env" => permissions.revoke_env(),
|
|
|
|
"run" => permissions.revoke_run(),
|
|
|
|
"plugin" => permissions.revoke_plugin(),
|
|
|
|
"hrtime" => permissions.revoke_hrtime(),
|
|
|
|
n => return Err(OpError::other(format!("No such permission name: {}", n))),
|
|
|
|
};
|
2019-10-28 00:22:53 +09:00
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
2019-11-12 00:33:29 +09:00
|
|
|
|
|
|
|
pub fn op_request_permission(
|
2020-02-08 20:34:31 +01:00
|
|
|
state: &State,
|
2019-11-12 00:33:29 +09:00
|
|
|
args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-11-12 00:33:29 +09:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 20:34:31 +01:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = &mut state.permissions;
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2019-11-12 00:33:29 +09:00
|
|
|
let perm = match args.name.as_ref() {
|
2020-08-18 21:29:32 +01:00
|
|
|
"read" => permissions.request_read(&path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.request_write(&path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.request_net(&args.url.as_deref())?,
|
|
|
|
"env" => permissions.request_env(),
|
|
|
|
"run" => permissions.request_run(),
|
|
|
|
"plugin" => permissions.request_plugin(),
|
|
|
|
"hrtime" => permissions.request_hrtime(),
|
|
|
|
n => return Err(OpError::other(format!("No such permission name: {}", n))),
|
|
|
|
};
|
2019-11-12 00:33:29 +09:00
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
|
|
|
}
|