2019-03-04 11:04:19 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2019-03-04 11:04:19 -05:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
|
|
|
import * as dispatch from "./dispatch";
|
|
|
|
import { assert } from "./util";
|
|
|
|
|
|
|
|
/** Permissions as granted by the caller */
|
2019-03-09 12:30:38 -05:00
|
|
|
export interface Permissions {
|
2019-03-04 11:04:19 -05:00
|
|
|
read: boolean;
|
|
|
|
write: boolean;
|
|
|
|
net: boolean;
|
|
|
|
env: boolean;
|
|
|
|
run: boolean;
|
2019-04-08 16:22:40 -04:00
|
|
|
highPrecision: boolean;
|
2019-03-04 11:04:19 -05:00
|
|
|
// NOTE: Keep in sync with src/permissions.rs
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
|
|
|
|
export type Permission = keyof Permissions;
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function getReq(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Permissions.createPermissions(builder);
|
2019-03-09 12:30:38 -05:00
|
|
|
return [builder, msg.Any.Permissions, inner];
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPermissions(inner: msg.PermissionsRes): Permissions {
|
|
|
|
return {
|
|
|
|
read: inner.read(),
|
|
|
|
write: inner.write(),
|
|
|
|
net: inner.net(),
|
|
|
|
env: inner.env(),
|
2019-04-08 16:22:40 -04:00
|
|
|
run: inner.run(),
|
|
|
|
highPrecision: inner.highPrecision()
|
2019-03-09 12:30:38 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-04 11:04:19 -05:00
|
|
|
/** Inspect granted permissions for the current program.
|
|
|
|
*
|
|
|
|
* if (Deno.permissions().read) {
|
|
|
|
* const file = await Deno.readFile("example.test");
|
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
export function permissions(): Permissions {
|
|
|
|
const baseRes = dispatch.sendSync(...getReq())!;
|
|
|
|
assert(msg.Any.PermissionsRes === baseRes.innerType());
|
|
|
|
const res = new msg.PermissionsRes();
|
|
|
|
assert(baseRes.inner(res) != null);
|
|
|
|
// TypeScript cannot track assertion above, therefore not null assertion
|
|
|
|
return createPermissions(res);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function revokeReq(
|
|
|
|
permission: string
|
|
|
|
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const permission_ = builder.createString(permission);
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.PermissionRevoke.createPermissionRevoke(
|
|
|
|
builder,
|
|
|
|
permission_
|
|
|
|
);
|
2019-03-09 12:30:38 -05:00
|
|
|
return [builder, msg.Any.PermissionRevoke, inner];
|
|
|
|
}
|
|
|
|
|
2019-03-04 11:04:19 -05:00
|
|
|
/** Revoke a permission. When the permission was already revoked nothing changes
|
|
|
|
*
|
|
|
|
* if (Deno.permissions().read) {
|
|
|
|
* const file = await Deno.readFile("example.test");
|
|
|
|
* Deno.revokePermission('read');
|
|
|
|
* }
|
|
|
|
* Deno.readFile("example.test"); // -> error or permission prompt
|
|
|
|
*/
|
|
|
|
export function revokePermission(permission: Permission): void {
|
|
|
|
dispatch.sendSync(...revokeReq(permission));
|
|
|
|
}
|