jhthorsen / openapi-client

A client for talking to an Open API powered server
11 stars 17 forks source link

pre_processor enhancement. #10

Closed aero closed 6 years ago

aero commented 6 years ago

HI,

Nowadays, there are many services that want the header encrypted with api key like https://metacpan.org/pod/WebService::Cryptopia

    my $signature = $self->api_key . "POST" . lc( url_encode( $url ) ) . $nonce . $request_content_base64_string;
    chomp( $signature );
    $self->log->trace( "Signature: $signature" ) if $self->log->is_trace;
    my $hmac_signature = encode_base64( hmac_sha256( $signature, decode_base64( $self->api_secret ) ) );
    chomp( $hmac_signature );
    $self->log->trace( "HMAC signature: $hmac_signature" ) if $self->log->is_trace;
    my $header_value = "amx " . $self->api_key . ':' . $hmac_signature . ':' . $nonce;
    $self->log->trace( "Authorization: $header_value" ) if $self->log->is_trace;
    my $request = HTTP::Request->new( 'POST' => $url );
    $request->header( 'Authorization', $header_value );

Making signature with hamc_sha256(key+method+url+nonce+content) and adding it to request headers.

But OpenApi::Client's pre_process function can't see method and url, so I can't build signature and add it to headers.

return $self->ua->build_tx($http_method, $url, $self->pre_processor->(\%headers, \%req));

How can I make this possible?

jhthorsen commented 6 years ago

I'm not sure if you really want to put this logic into pre_processor. How about doing this instead:

my $client = OpenAPI::Client->new(...);
$client->ua->on(start => sub {
  my ($ua, $tx) = @_;
  $tx->req->headers->header(Authorization => ...);
});
aero commented 6 years ago

Oh I didn't know such hooking mechanism. I succeeded in that way.

Thank you.