2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_json::Value;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_query_permission", op_query_permission);
|
|
|
|
super::reg_json_sync(rt, "op_revoke_permission", op_revoke_permission);
|
|
|
|
super::reg_json_sync(rt, "op_request_permission", op_request_permission);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PermissionArgs {
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
path: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_query_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-10-27 11:22:53 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-10-27 11:22:53 -04:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
let permissions = cli_state.permissions.borrow();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 16:29:32 -04: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(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({ "state": perm.to_string() }))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_revoke_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-10-27 11:22:53 -04:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
let mut permissions = cli_state.permissions.borrow_mut();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 16:29:32 -04: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(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({ "state": perm.to_string() }))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
|
|
pub fn op_request_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-11-11 10:33:29 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-11-11 10:33:29 -05:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
let permissions = &mut cli_state.permissions.borrow_mut();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2019-11-11 10:33:29 -05:00
|
|
|
let perm = match args.name.as_ref() {
|
2020-08-18 16:29:32 -04: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(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({ "state": perm.to_string() }))
|
2019-11-11 10:33:29 -05:00
|
|
|
}
|