Skip to content

Commit

Permalink
Merge 34c6ecf into c7fe7d2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkoi committed Jan 24, 2021
2 parents c7fe7d2 + 34c6ecf commit 3954134
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
16 changes: 15 additions & 1 deletion lib/WebService/Slack/WebApi/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,26 @@ sub base_url {
sub request {
my ($self, $path, $params) = @_;

my %headers;
if( $self->token && $params->{'http_auth'} ) {
my $msg = 'Illegal parameters. You have defined \'token\' but the '
. ' method you are using defines its own HTTP Authorization header.';
WebService::Slack::WebApi::Exception::IllegalParameters->throw(
message => $msg,
);
}
if( $self->token ) {
$headers{ 'Authorization' } = 'Bearer ' . $self->token;
} elsif( $params->{'http_auth'} ) {
$headers{ 'Authorization' } = $params->{'http_auth'};
}
my %options = ( headers => \%headers );
my $response = $self->ua->post_form(
$self->base_url . $path,
[
$self->token ? (token => $self->token) : (),
%{ $params },
],
\%options,
);
return decode_json $response->{content} if $response->{success};

Expand Down
21 changes: 18 additions & 3 deletions lib/WebService/Slack/WebApi/Oauth/V2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ use warnings;
use utf8;
use feature qw/ state /;

use MIME::Base64 qw/ encode_base64 /;

use parent 'WebService::Slack::WebApi::Base';

# override
sub base_name { 'oauth.v2' }

sub access {
# Attn. client_id and client_secret are optional parameters for Slack
# because Slack makes it possible to authenticate either
# by using them or by setting an HTTP Auth header.
# https://api.slack.com/methods/oauth.v2.access
# But here we make the parameters mandatory because
# they are needed to call this method in any case.
state $rule = Data::Validator->new(
code => 'Str',
client_id => { isa => 'Str', optional => 1 },
client_secret => { isa => 'Str', optional => 1 },
client_id => { isa => 'Str', optional => 0 },
client_secret => { isa => 'Str', optional => 0 },
redirect_uri => { isa => 'Str', optional => 1 },
)->with('Method');
my ($self, $args) = $rule->validate(@_);

return $self->request('access', { %$args });
my $t = encode_base64( $args->{'client_id'} . q{:} . $args->{'client_secret'}, q{} );
my $basic_auth = 'Basic ' . $t;
return $self->request('access', {
code => $args->{'code'},
redirect_uri => $args->{'redirect_uri'},
http_auth => $basic_auth,
}
);
}

1;
Expand Down
20 changes: 18 additions & 2 deletions t/03_call_methods.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use utf8;
use feature qw/state/;

use Test::More;
use Test::Exception;
use lib '.';
use t::Util qw/ any_mocked_slack /;
use t::Util qw/ any_mocked_slack mocked_slack_without_token /;

use WebService::Slack::WebApi;

my %tests = (
auth => {
Expand Down Expand Up @@ -558,12 +561,25 @@ subtest 'users.profile' => sub {
};

subtest 'oauth.v2' => sub {
isa_ok $slack->oauth->v2->access(
throws_ok {
$slack->oauth->v2->access(
code => 'ccdaa72ad',
client_id => '4b39e9-752c4',
client_secret => '33fea0113f5b1',
redirect_uri => 'http://example.com',
);
} 'WebService::Slack::WebApi::Exception::IllegalParameters',
'Illegal parameters: defined token when calling oath.v2.access';

my $sl = mocked_slack_without_token();
isa_ok $sl->oauth->v2->access(
code => 'ccdaa72ad',
client_id => '4b39e9-752c4',
client_secret => '33fea0113f5b1',
redirect_uri => 'http://example.com',
), 'HASH';

done_testing;
};

done_testing;
Expand Down
8 changes: 6 additions & 2 deletions t/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use Exporter qw/ import /;

use WebService::Slack::WebApi;

our @EXPORT_OK = qw/ mocked_slack any_mocked_slack /;
our @EXPORT_OK = qw/ mocked_slack any_mocked_slack mocked_slack_without_token /;

sub mocked_slack {
my ($content, $is_success) = @_;
my ($content, $is_success, $no_token) = @_;

# mock ua for HTTP::AnyUA
# don't use with real HTTP::Tiny
Expand All @@ -22,10 +22,14 @@ sub mocked_slack {
}, 'HTTP::Tiny';
sub HTTP::Tiny::request { shift }

if(defined $no_token && $no_token) {
return WebService::Slack::WebApi->new(ua => $ua);
}
return WebService::Slack::WebApi->new(token => 'a', ua => $ua);
}

sub any_mocked_slack { mocked_slack +{hoge => 'fuga'}, 1 }
sub mocked_slack_without_token { mocked_slack +{hoge => 'fuga'}, 1, 1 }

1;

0 comments on commit 3954134

Please sign in to comment.