AgentPushKit-Client
view release on metacpan or search on metacpan
lib/AgentPushKit/Client/ApiClient.pm view on Meta::CPAN
return unless $api_key;
my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key;
}
# update header and query param based on authentication setting
#
# @param array $headerParams header parameters (by ref)
# @param array $queryParams query parameters (by ref)
# @param array $authSettings array of authentication scheme (e.g ['api_key'])
sub update_params_for_auth {
my ($self, $header_params, $query_params, $auth_settings) = @_;
return $self->_global_auth_setup($header_params, $query_params)
unless $auth_settings && @$auth_settings;
# one endpoint can have more than 1 auth settings
foreach my $auth (@$auth_settings) {
# determine which one to use
if (!defined($auth)) {
# TODO show warning about auth setting not defined
}
elsif ($auth eq 'UserOrAgent') {
# this endpoint requires Bearer (Agent Push Kit JWT or apt_ token) authentication (access token)
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}
}
elsif ($auth eq 'AgentToken') {
# this endpoint requires Bearer (apt_) authentication (access token)
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}
}
elsif ($auth eq 'OrganizationApiKey') {
# this endpoint requires Bearer (apk_) authentication (access token)
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}
}
else {
# TODO show warning about security definition not found
}
}
}
# The endpoint API class has not found any settings for auth. This may be deliberate,
# in which case update_params_for_auth() will be a no-op. But it may also be that the
# OpenAPI Spec does not describe the intended authorization. So we check in the config for any
# auth tokens and if we find any, we use them for all endpoints;
sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_;
my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens;
# basic
if (my $uname = delete $tokens->{username}) {
my $pword = delete $tokens->{password};
$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
}
# oauth
if (my $access_token = delete $tokens->{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $access_token;
}
# other keys
foreach my $token_name (keys %$tokens) {
my $in = $tokens->{$token_name}->{in};
my $token = $self->get_api_key_with_prefix($token_name);
if ($in eq 'head') {
$header_params->{$token_name} = $token;
}
elsif ($in eq 'query') {
$query_params->{$token_name} = $token;
}
else {
die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
}
}
}
1;
( run in 1.413 second using v1.01-cache-2.11-cpan-f5f21f1076c )