Zapp/Task/GetOAuth2Token.pm

filter: Statementstmt 52.6% 10 / 19 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 3 / 5 Podpod 50.0% 1 / 2 totaltotal 40.0% 14 / 35 CC 4 SCAR 23.6
CC
4
Cov
38%
run
20.0
schema
2.0
BEGIN
1.0
CRAP 10.6
« Zapp/Trigger.pm ↑ Summary Zapp/Trigger/Webhook.pm »
1 package Zapp::Task::GetOAuth2Token;
2 # ABSTRACT: Get an OAuth2 Token
3  
4 #pod =head1 DESCRIPTION
5 #pod
6 #pod This task gets an OAuth2 token from the given provider.
7 #pod
8 #pod =head2 Output
9 #pod
10 #pod access_token - The access token
11 #pod token_type - The type of token. Only Bearer tokens are available so far.
12 #pod scope - The scopes returned
13 #pod expires_in
14 #pod refresh_token
15 #pod
16 #pod =head1 SEE ALSO
17 #pod
18 #pod =cut
19  
20 1 ▶use Mojo::Base 'Zapp::Task', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Mojo::JSON qw( false true );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Mojo::Util qw( b64_encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ▶sub schema( $class ) {
Subroutine schema: not called CC=1 SCAR=6.9
Pod undocumented
25 return {
26 0 input => {
27 type => 'object',
28 required => [qw( endpoint client_id client_secret )],
29 properties => {
30 endpoint => {
31 type => 'string',
32 },
33 client_id => {
34 type => 'string',
35 },
36 client_secret => {
37 type => 'string',
38 },
39 scope => {
40 type => 'string',
41 },
42 },
43 additionalProperties => false,
44 },
45 output => {
46 type => 'object',
47 required => [qw( access_token token_type )],
48 properties => {
49 is_success => {
50 type => 'boolean',
51 },
52 # XXX: Add more validation here? Should invalid results
53 # be accepted but flagged? Should users be able to fail
54 # invalid results?
55 access_token => {
56 type => 'string',
57 },
58 token_type => {
59 # We only understand Bearer tokens for now.
60 # https://tools.ietf.org/html/rfc6749#section-7.1
61 type => 'string',
62 enum => [qw( bearer )],
63 },
64 expires_in => {
65 type => 'integer',
66 },
67 refresh_token => {
68 type => 'string',
69 },
70 scope => {
71 type => 'string',
72 },
73 },
74 },
75 };
76 }
77  
78 ▶sub run( $self, $input ) {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
79 # An OAuth2 client credentials request is authenticated with HTTP
80 # basic auth: The client_id is the username, the client_secret is
81 # the password. https://tools.ietf.org/html/rfc6749#section-4.4
82 0 my $url = Mojo::URL->new( $input->{endpoint} );
83 0 my $auth = b64_encode( join( ':', $input->@{qw( client_id client_secret )} ), "" );
84 my $tx = $self->app->ua->post(
85 $url,
86 {
87 Authorization => 'Basic ' . $auth,
88 },
89 form => {
90 grant_type => 'client_credentials',
91 scope => $input->{ scope },
92 },
93 0 );
94  
95 # The response will be a JSON document. On success (200 OK) it will contain
96 # the token. On failure (400 Bad Request) it will describe the
97 # error.
98 0 my $json = $tx->res->json;
99 0 ▶ my %output = (
Branch: 1 branch
BranchTrueFalseTotal
$tx->res->is_success ? : 0 0 0
100 is_success => $tx->res->is_success ? true : false,
101 );
102 # Success: https://tools.ietf.org/html/rfc6749#section-5.1
103 0 ▶ if ( $output{is_success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output{"is_success"}) 0 0 0
104 return $self->finish({
105 %output,
106 $json->%{qw( access_token token_type expires_in refresh_token )},
107 # If scope is omitted, it is the same as the scope sent with the
108 # request (https://tools.ietf.org/html/rfc6749#section-5.1)
109 scope => $json->{scope} || $input->{scope},
110 0 ▶ });
Condition0%0/3
$json->{'scope'} || $input->{'scope'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$json->{'scope'} || $input->{'scope'}
$json->{'scope'} $input->{'scope'}
Truth table0%0/3
A || B
A= $json->{'scope'}B= $input->{'scope'}
ABresult
1X1
011
000
111 }
112 # Error: https://tools.ietf.org/html/rfc6749#section-5.2
113 return $self->fail({
114 %output,
115 0 $json->%{qw( error error_description error_uri )},
116 });
117 }
118  
119 1 1;
120  
121 =pod
122  
123 =head1 NAME
124  
125 Zapp::Task::GetOAuth2Token - Get an OAuth2 Token
126  
127 =head1 VERSION
128  
129 version 0.004
130  
131 =head1 DESCRIPTION
132  
133 This task gets an OAuth2 token from the given provider.
134  
135 =head2 Output
136  
137 access_token - The access token
138 token_type - The type of token. Only Bearer tokens are available so far.
139 scope - The scopes returned
140 expires_in
141 refresh_token
142  
143 =head1 SEE ALSO
144  
145 =head1 AUTHOR
146  
147 Doug Bell <preaction@cpan.org>
148  
149 =head1 COPYRIGHT AND LICENSE
150  
151 This software is copyright (c) 2021 by Doug Bell.
152  
153 This is free software; you can redistribute it and/or modify it under
154 the same terms as the Perl 5 programming language system itself.
155  
156 =cut
157  
158 __DATA__
« Zapp/Trigger.pm ↑ Summary Zapp/Trigger/Webhook.pm »