Skip to content

Commit

Permalink
Update authentication headers when following redirection via user agent
Browse files Browse the repository at this point in the history
The openQA user agent code so far preserves manually set headers including
the `X-API-…` headers used for authentication. This is probably not
required and has the problematic side-effect that those headers are not
updated when following redirections. That means authentication fails when a
redirection is in place. This is the case when querying assets of a job as
done by the cache service.

In order to make the cache service work when authentication for assets is
enabled via 4c8ed39 this change updates
the `X-API-…` headers regardless of whether they are already present. Any
custom values will be overridden but preserving custom values is not
required anyways. (The accepted header will still be preserved.)

Related ticket: https://progress.opensuse.org/issues/174154
  • Loading branch information
Martchus committed Jan 16, 2025
1 parent be8b517 commit 5dcb81d
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/OpenQA/UserAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,12 @@ sub configure_credentials ($self, $host) {

sub _add_auth_headers ($self, $ua, $tx) {
my $timestamp = time;
my %headers = (
Accept => 'application/json',
'X-API-Microtime' => $timestamp,
);
my $headers = $tx->req->headers;
$headers->accept('application/json') unless defined $headers->accept;
$headers->header('X-API-Microtime', $timestamp);
if ($self->apisecret && $self->apikey) {
$headers{'X-API-Key'} = $self->apikey;
$headers{'X-API-Hash'} = hmac_sha1_sum($self->_path_query($tx) . $timestamp, $self->apisecret);
}

my $set_headers = $tx->req->headers;
foreach my $key (keys %headers) {
# don't overwrite headers that were set manually
$set_headers->header($key, $headers{$key}) unless defined $set_headers->header($key);
$headers->header('X-API-Key', $self->apikey);
$headers->header('X-API-Hash', hmac_sha1_sum($self->_path_query($tx) . $timestamp, $self->apisecret));
}
}

Expand Down

0 comments on commit 5dcb81d

Please sign in to comment.