AnyEvent-MCP

 view release on metacpan or  search on metacpan

lib/AnyEvent/MCP/Permissions/Ask.pm  view on Meta::CPAN

            type    => "string",
            enum    => \@decisions,
            default => "deny-once",
            title   => "Decision",
         },
      },
   }
}

sub message {
   my ($request, $stage) = @_;

   my $json = JSON::XS->new;
   my %label = (
      name                   => "Server",
      multibroker_alias      => "Multibroker alias",
      tool                   => "Tool",
      action                 => "Action",
      action_description     => "Action description",
      sandbox_implementation => "Sandbox",
      paths                  => "Paths",
   );

   my $reason = $request->{reason};
   $reason =~ s/([\x00-\x08\x0b-\x1f\x7f-\x9f\x{202a}-\x{202e}\x{2066}-\x{2069}])/sprintf "\\u{%X}", ord $1/ge
      if defined $reason;

   join "\n",
      "Permission request",
      "Requested sandbox grants: " . join (", ", @{$request->{sandbox_grants}}),
      (map { "$label{$_}: " . $json->encode ($request->{$_}) }
         grep defined $request->{$_},
            qw(name multibroker_alias tool action action_description sandbox_implementation paths)),
      (defined $reason ? "Reason: $reason" : ()),
      (map {
         my $dim = $request->{dimensions}{$_};
         "$dim->{label}: " . $json->encode ($dim->{value})
      } dimension_names ($request)),
      (map { detail_text ($_, $request->{details}{$_}) } sort keys %{$request->{details} || {}}),
      ($stage && $stage eq "rule" ? (
         "Available sandbox grants:",
         (map {
            my $description = $request->{sandbox_grant_descriptions}{$_};
            "  $_" . (defined $description && length $description ? " - $description" : "")
         } @{$request->{sandbox_grant_choices}}),
         (map {
            my $dim = $request->{dimensions}{$_};
            map { "  " . $json->encode ($_->{label}) . ": " . $json->encode ($_->{match}) }
               @{$dim->{choices}}
         } dimension_names ($request))
      ) : ())
}

sub detail_text {
   my ($label, $text) = @_;

   defined $text && !ref $text
      or die "Invalid permission detail\n";

   my $chars = length $text;
   my $bytes = length AnyEvent::MCP::utf8enc $text;
   my $truncated = $chars > 65536;
   $text = substr $text, 0, 65536;
   $text =~ s/([\x00-\x08\x0b-\x1f\x7f-\x9f\x{202a}-\x{202e}\x{2066}-\x{2069}])/sprintf "\\u{%X}", ord $1/ge;
   $label =~ s/([\x00-\x1f\x7f-\x9f\x{202a}-\x{202e}\x{2066}-\x{2069}])/sprintf "\\u{%X}", ord $1/ge;

   "$label: $chars characters, $bytes UTF-8 bytes\n\n"
      . ($truncated ? "WARNING: INCOMPLETE PREVIEW. Additional text is not shown here.\n\n" : "")
      . $text
}

sub build {
   my ($prepared, $answer, $stage, $previous) = @_;

   $previous ||= {};
   die "Invalid permission answer\n" unless ref $answer eq "HASH"
      && defined $answer->{decision} && !ref $answer->{decision};

   my $choice = $answer->{decision};
   if ($stage && $stage eq "rule") {
      return { decision => "back" } if $choice eq "back";
      die "Invalid rule decision\n" unless $choice eq ($previous->{decision} // "")
         && $choice =~ /-always\z/;
   } else {
      die "Invalid permission decision\n"
         unless $choice =~ /\A(?:allow|deny)-(?:once|always)\z/;
   }

   my $message = $choice =~ /\Adeny-/ ? ($stage && $stage eq "rule"
      ? $previous->{message} : $answer->{message}) : undef;
   if (defined $message) {
      die "Invalid permission message\n" if ref $message || length $message > 2048
         || $message =~ /[\r\n]/;
      undef $message unless $message =~ /\S/;
   }

   # a native MCP "always" choice opens a second form for rule details.
   if ($stage && $stage eq "choice" && $choice =~ /-always\z/) {
      return { decision => $choice, (defined $message ? (message => $message) : ()) };
   }

   my $allow = $choice =~ /\Aallow-/;
   my $decision = { decision => $choice, (defined $message ? (message => $message) : ()) };
   if ($allow) {
      my $grants = $answer->{sandbox_grants};
      $grants = $prepared->{sandbox_grants} if !defined $grants && $choice eq "allow-once";
      die "Permission answer needs sandbox grants\n" unless ref $grants eq "ARRAY";
      my %available = map { $_ => 1 } @{$prepared->{sandbox_grant_choices}};
      die "Unknown selected sandbox grant\n" if grep !$available{$_}, @$grants;
      $decision->{sandbox_grants} = [@$grants];
   }

   if ($choice =~ /-always\z/) {
      my $rule = $decision->{rule} = {
         decision         => $allow ? "allow" : "deny",
         action           => $prepared->{action_matcher},
         "sandbox-grants" => join (",", @{$allow ? $decision->{sandbox_grants} : $prepared->{sandbox_grants}}),
      };
      my $lifetime = exists $answer->{lifetime} ? $answer->{lifetime} : "indefinite";
      defined $lifetime && !ref $lifetime && $lifetime =~ /\A(?:indefinite|900|57600)\z/
         or die "Invalid permission rule lifetime\n";



( run in 0.651 second using v1.01-cache-2.11-cpan-234fa220139 )