Skip to content

Commit

Permalink
[backend] BSHTTP: add a parseauthenticate method
Browse files Browse the repository at this point in the history
Parses the weird and complex syntax described in RFC 7235.
  • Loading branch information
mlschroe committed Dec 12, 2017
1 parent 401d4cb commit 1016cb4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/backend/BSHTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ sub makemultipart {
return $data;
}

sub authquote {
my ($q) = @_;
$q =~ s/(.)/sprintf("%%%02X", ord($1))/ge;
return $q;
}

sub parseauthenticate {
my ($auth) = @_;
$auth =~ s/%/%25/g;
$auth =~ s/\\(.)/$1 eq '%' ? '%' : sprintf("%%%02X", ord($1))/ge;
$auth =~ s/\"(.*?)\"/authquote($1)/ge;
$auth =~ s/\s*=\s*/=/g; # get rid of bad space
my @auth = split(/\s*,\s*/, $auth);
for (splice @auth) {
s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
if (/^([^=\s]+)(?:\s+(.*?))?$/s) {
push @auth, lc($1), {};
$_ = $2;
}
next unless @auth && defined($_);
if (/^(.*?)=(.*)/) {
push @{$auth[-1]->{lc($1)}}, $2;
} else {
push @{$auth[-1]->{'token'}}, $_;
}
}
return @auth;
}

sub unexpected_eof {
my ($req) = @_;
$req->{'__eof'} = 1 if $req;
Expand Down
2 changes: 2 additions & 0 deletions src/backend/BSRPC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ sub rpc {
if ($param->{'timeout'}) {
my %paramcopy = %$param;
my $timeout = delete $paramcopy{'timeout'};
$paramcopy{'running_timeout'} = $timeout;
my $ans;
local $SIG{'ALRM'} = sub {
alarm(0);
Expand Down Expand Up @@ -400,6 +401,7 @@ sub rpc {
close S;
my %myparam = %$param;
delete $myparam{'authenticator'};
$myparam{'headers'} = [ grep {!/^authorization:/i} @{$myparam{'headers'} || []} ];
push @{$myparam{'headers'}}, "Authorization: $auth";
return rpc(\%myparam, $xmlargs, @args);
}
Expand Down

0 comments on commit 1016cb4

Please sign in to comment.