| 1 | package Net::Async::WebSearch::Provider::Reddit::OAuth; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Reddit search provider using the OAuth2 endpoint | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent 'Net::Async::WebSearch::Provider::Reddit'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use HTTP::Request::Common qw( GET POST ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use MIME::Base64 qw( encode_base64 ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | sub _init { | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 16 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=21 SCAR=31.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 16 | ▶ | croak "Reddit::OAuth requires 'client_id'" unless $self->{client_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | croak "Reddit::OAuth requires 'client_secret'" unless defined $self->{client_secret}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 14 | ▶ | $self->{grant_type} ||= 'client_credentials'; # client_credentials | password | installed | authorization_code | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"grant_type"} ||= "client_credentials"
MC/DC100%1/1
$self->{"grant_type"} ||= "client_credentials"
$self->{"grant_type"}
Truth table100%2/2
A
A= $self->{"grant_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 14 | ▶ | $self->{token_url} ||= 'https://www.reddit.com/api/v1/access_token'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"token_url"} ||= "https://www.reddit.com/api/v1/access_token"
MC/DC0%0/1
$self->{"token_url"} ||= "https://www.reddit.com/api/v1/access_token"
$self->{"token_url"}
Truth table50%1/2
A
A= $self->{"token_url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 14 | ▶ | $self->{authorize_url} ||= 'https://www.reddit.com/api/v1/authorize'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"authorize_url"} ||= "https://www.reddit.com/api/v1/authorize"
MC/DC0%0/1
$self->{"authorize_url"} ||= "https://www.reddit.com/api/v1/authorize"
$self->{"authorize_url"}
Truth table50%1/2
A
A= $self->{"authorize_url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 14 | ▶ | $self->{endpoint} ||= 'https://oauth.reddit.com'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://oauth.reddit.com"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://oauth.reddit.com"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 14 | ▶ | $self->{name} ||= 'reddit-oauth'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"name"} ||= "reddit-oauth"
MC/DC0%0/1
$self->{"name"} ||= "reddit-oauth"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 14 | ▶ | $self->{link_base} ||= 'https://www.reddit.com'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"link_base"} ||= "https://www.reddit.com"
MC/DC0%0/1
$self->{"link_base"} ||= "https://www.reddit.com"
$self->{"link_base"}
Truth table50%1/2
A
A= $self->{"link_base"}
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 14 | ▶ | $self->{token_margin} ||= 60; # refresh this many seconds before expiry | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"token_margin"} ||= 60
MC/DC100%1/1
$self->{"token_margin"} ||= 60
$self->{"token_margin"}
Truth table100%2/2
A
A= $self->{"token_margin"}
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 14 | ▶ | if ( $self->{grant_type} eq 'password' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | croak "grant_type=password needs 'username' and 'password'" | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | unless defined $self->{username} && defined $self->{password}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"username"} and defined $self->{"password"}
MC/DC50%1/2
defined $self->{"username"} and defined $self->{"password"}
defined $self->{"username"}
defined $self->{"password"}
Truth table66%2/3
A and B
A= defined $self->{"username"}B= defined $self->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | elsif ( $self->{grant_type} eq 'installed' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | $self->{device_id} ||= 'DO_NOT_TRACK_THIS_DEVICE'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"device_id"} ||= "DO_NOT_TRACK_THIS_DEVICE"
MC/DC0%0/1
$self->{"device_id"} ||= "DO_NOT_TRACK_THIS_DEVICE"
$self->{"device_id"}
Truth table50%1/2
A
A= $self->{"device_id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | elsif ( $self->{grant_type} eq 'authorization_code' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # For authorization_code, either the caller seeds us with | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # a refresh_token (persisted from a prior session) or with | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # an access_token + refresh_token pair, or they must drive | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # authorize_url → complete_authorization themselves before | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # the first search. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 13 | $self->SUPER::_init; | |||||||||||||||||||||||||||||||||||||||||
| 44 | # Parent sets link_base to www.reddit.com, which is what we want. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # But parent also sets endpoint to www.reddit.com — overwrite back. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 13 | ▶ | $self->{endpoint} = 'https://oauth.reddit.com' unless $self->{_endpoint_explicit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # Pre-seed token/refresh if the caller provided them (persisted state). | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | if ( defined $self->{access_token} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | $self->{_access_token} = $self->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 51 | $self->{_token_expires_at} = $self->{token_expires_at} | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | // ( time + ( $self->{expires_in} // 3600 ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition20%1/5
$self->{"token_expires_at"} // time + ($self->{"expires_in"} // 3600)
$self->{"expires_in"} // 3600
MC/DC0%0/2
$self->{"token_expires_at"} // time + ($self->{"expires_in"} // 3600)
$self->{"token_expires_at"}
time + ($self->{"expires_in"} // 3600)
MC/DC0%0/1
$self->{"expires_in"} // 3600
$self->{"expires_in"}
Truth table33%1/3
A // B
A= $self->{"token_expires_at"}B= time + ($self->{"expires_in"} // 3600)
Truth table0%0/2
A
A= $self->{"expires_in"}
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | ▶ | if ( defined $self->{refresh_token} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | $self->{_refresh_token} = $self->{refresh_token}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 16 | ▶ | my ( $class, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 16 | my $explicit_ep = exists $args{endpoint}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 16 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 13 | $self->{_endpoint_explicit} = $explicit_ep; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 13 | ▶ | $self->{endpoint} = 'https://oauth.reddit.com' unless $explicit_ep; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 12 | ▶ | sub client_id { $_[0]->{client_id} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | sub client_secret { $_[0]->{client_secret} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_secret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 28 | ▶ | sub grant_type { $_[0]->{grant_type} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
grant_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | sub token_url { $_[0]->{token_url} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub authorize_endpoint { $_[0]->{authorize_url} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
authorize_endpoint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub access_token { $_[0]->{_access_token} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
access_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | sub refresh_token { $_[0]->{_refresh_token} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
refresh_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub token_expires_at { $_[0]->{_token_expires_at} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token_expires_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # MCP / host helper: snapshot the persistable bits of the auth state | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # so a host application can stash them in its session store and feed | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # them back to a later `new()` call. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | sub token_state { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token_state: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ( defined $self->{_access_token} | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ? ( access_token => $self->{_access_token}, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | token_expires_at => $self->{_token_expires_at} ) | ||||||||||||||||||||||||||||||||||||||||||
| 87 | : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 88 | ( defined $self->{_refresh_token} | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ? ( refresh_token => $self->{_refresh_token} ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | : () ), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | sub _basic_auth_header { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_basic_auth_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | my $raw = $self->client_id . ':' . $self->client_secret; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 10 | my $b64 = encode_base64($raw, ''); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | return "Basic $b64"; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # Host-facing authorization-code helpers | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Build the URL the human should visit to authorize the app. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # Returns a plain URL string (not a Future) — no network round-trip. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | sub authorize_url { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
authorize_url: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | croak "authorize_url needs 'redirect_uri'" unless defined $args{redirect_uri}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | my $scope = $args{scope}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ( ref $scope eq 'ARRAY' ) { $scope = join ' ', @$scope } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | $scope //= 'read'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope //= "read"
MC/DC100%1/1
$scope //= "read"
$scope
Truth table100%2/2
A
A= $scope
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | my $uri = URI->new( $self->authorize_endpoint ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | $uri->query_form( | ||||||||||||||||||||||||||||||||||||||||||
| 115 | client_id => $self->client_id, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | response_type => 'code', | ||||||||||||||||||||||||||||||||||||||||||
| 117 | state => $args{state} // _random_state(), | ||||||||||||||||||||||||||||||||||||||||||
| 118 | redirect_uri => $args{redirect_uri}, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | duration => $args{duration} // 'permanent', | ||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
$args{"state"} // _random_state()
$args{"duration"} // "permanent"
MC/DC0%0/2
$args{"state"} // _random_state()
$args{"state"}
_random_state()
MC/DC0%0/1
$args{"duration"} // "permanent"
$args{"duration"}
Truth table66%2/3
A // B
A= $args{"state"}B= _random_state()
Truth table50%1/2
A
A= $args{"duration"}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | scope => $scope, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | return $uri->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # Exchange an authorization_code → access_token + refresh_token. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # Caller supplies the code pasted back from the redirect, plus the | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # same redirect_uri that was used in authorize_url. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | sub complete_authorization { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
complete_authorization: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | my $http = $args{http} || $self->{_http_for_auth}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'http'} || $self->{'_http_for_auth'}
MC/DC0%0/2
$args{'http'} || $self->{'_http_for_auth'}
$args{'http'}
$self->{'_http_for_auth'}
Truth table33%1/3
A || B
A= $args{'http'}B= $self->{'_http_for_auth'}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | ▶ | croak "complete_authorization needs 'http' (Net::Async::HTTP) " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | ."unless the provider has been added to a loop first" | ||||||||||||||||||||||||||||||||||||||||||
| 133 | unless $http; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | croak "complete_authorization needs 'code'" unless defined $args{code}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | croak "complete_authorization needs 'redirect_uri'" unless defined $args{redirect_uri}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | return $self->_post_token($http, [ | ||||||||||||||||||||||||||||||||||||||||||
| 138 | grant_type => 'authorization_code', | ||||||||||||||||||||||||||||||||||||||||||
| 139 | code => $args{code}, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | redirect_uri => $args{redirect_uri}, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | ]); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | sub _random_state { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | my @chars = ( 'a'..'z', 'A'..'Z', 0..9 ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | join '', map { $chars[ int rand @chars ] } 1 .. 24; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # Token fetch / refresh dance | ||||||||||||||||||||||||||||||||||||||||||
| 151 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | sub _post_token { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 10 | ▶ | my ( $self, $http, $body_ref ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_post_token: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 10 | my $req = POST( $self->token_url, $body_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 10 | $req->header( 'Authorization' => $self->_basic_auth_header ); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 10 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 10 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 10 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 10 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | return Future->fail( | |||||||||||||||||||||||||||||||||||||||||
| 164 | $self->name.": token HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 9 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 9 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return Future->fail( $self->name.": token invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | ▶ | if ( !$data->{access_token} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $err = $data->{error} // 'unknown'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"error"} // "unknown"
MC/DC0%0/1
$data->{"error"} // "unknown"
$data->{"error"}
Truth table0%0/2
A
A= $data->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | return Future->fail( $self->name.": token error: $err", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | $self->{_access_token} = $data->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 9 | ▶ | $self->{_token_expires_at} = time + ( $data->{expires_in} // 3600 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"expires_in"} // 3600
MC/DC0%0/1
$data->{"expires_in"} // 3600
$data->{"expires_in"}
Truth table50%1/2
A
A= $data->{"expires_in"}
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | # Reddit only returns refresh_token on duration=permanent auth_code | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # flows. Some refresh responses include a fresh refresh_token too — | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # prefer that; otherwise keep the one we used. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 9 | ▶ | if ( defined $data->{refresh_token} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | $self->{_refresh_token} = $data->{refresh_token}; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 9 | ▶ | if ( my $cb = $self->{on_token_refresh} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | $cb->( $self, $data ); | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 9 | return Future->done( $self->{_access_token} ); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 10 | }); | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | sub _get_token { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 11 | ▶ | my ( $self, $http ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_token: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # Valid cached token? | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 11 | ▶ | if ( $self->{_access_token} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->{"_access_token"} and $self->{"_token_expires_at"} and time + $self->{"token_margin"} < $self->{"_token_expires_at"}
$self->{"_access_token"} and $self->{"_token_expires_at"}
MC/DC66%2/3
$self->{"_access_token"} and $self->{"_token_expires_at"} and time + $self->{"token_margin"} < $self->{"_token_expires_at"}
$self->{"_access_token"}
$self->{"_token_expires_at"}
time + $self->{"token_margin"} < $self->{"_token_expires_at"}
Truth table75%3/4
A and B and C
A= $self->{"_access_token"}B= $self->{"_token_expires_at"}C= time + $self->{"token_margin"} < $self->{"_token_expires_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | && $self->{_token_expires_at} | ||||||||||||||||||||||||||||||||||||||||||
| 196 | && time + $self->{token_margin} < $self->{_token_expires_at} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | return Future->done( $self->{_access_token} ); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # A refresh_token beats every other grant — it's cheap and user-context | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # is already established. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 9 | ▶ | if ( $self->{_refresh_token} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | return $self->_post_token($http, [ | ||||||||||||||||||||||||||||||||||||||||||
| 204 | grant_type => 'refresh_token', | ||||||||||||||||||||||||||||||||||||||||||
| 205 | refresh_token => $self->{_refresh_token}, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | ]); | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | # For authorization_code flows without a refresh_token, we can't | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # auto-fetch — the caller must drive the authorize → complete | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # dance first. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 8 | ▶ | if ( $self->grant_type eq 'authorization_code' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | return Future->fail( | |||||||||||||||||||||||||||||||||||||||||
| 214 | $self->name.": no access_token and no refresh_token — " | ||||||||||||||||||||||||||||||||||||||||||
| 215 | ."call authorize_url / complete_authorization first", | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 7 | my @body = ( grant_type => $self->grant_type ); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 7 | ▶ | if ( $self->grant_type eq 'password' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | push @body, username => $self->{username}, password => $self->{password}; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | elsif ( $self->grant_type eq 'installed' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | @body = ( | ||||||||||||||||||||||||||||||||||||||||||
| 226 | grant_type => 'https://oauth.reddit.com/grants/installed_client', | ||||||||||||||||||||||||||||||||||||||||||
| 227 | device_id => $self->{device_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 7 | return $self->_post_token($http, \@body); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | sub search { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 11 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=3 SCAR=12.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 11 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 11 | ▶ | my $limit = $opts->{limit} || 10; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{'limit'} || 10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table50%1/2
A
A= $opts->{'limit'}
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | return $self->_get_token($http)->then(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 9 | ▶ | my ( $token ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=25.4
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 9 | ▶ | my $sub = defined $opts->{subreddit} ? $opts->{subreddit} : $self->subreddit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 9 | ▶ | my $path = defined $sub && length $sub ? "/r/$sub/search" : "/search"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sub and length $sub
MC/DC0%0/2
defined $sub and length $sub
defined $sub
length $sub
Truth table33%1/3
A and B
A= defined $subB= length $sub
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 9 | my $uri = URI->new( $self->endpoint . $path ); | |||||||||||||||||||||||||||||||||||||||||
| 246 | my %q = ( | ||||||||||||||||||||||||||||||||||||||||||
| 247 | q => $query, | ||||||||||||||||||||||||||||||||||||||||||
| 248 | limit => $limit, | ||||||||||||||||||||||||||||||||||||||||||
| 249 | sort => $opts->{sort} // $self->sort, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 9 | ▶ | t => $opts->{time} // $self->time, | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$opts->{"sort"} // $self->sort
$opts->{"time"} // $self->time
MC/DC0%0/2
$opts->{"sort"} // $self->sort
$opts->{"sort"}
$self->sort
MC/DC0%0/2
$opts->{"time"} // $self->time
$opts->{"time"}
$self->time
Truth table33%1/3
A // B
A= $opts->{"sort"}B= $self->sort
Truth table33%1/3
A // B
A= $opts->{"time"}B= $self->time
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | raw_json => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 9 | ▶ | $q{restrict_sr} = 1 if defined $sub && length $sub; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sub and length $sub
MC/DC0%0/2
defined $sub and length $sub
defined $sub
length $sub
Truth table33%1/3
A and B
A= defined $subB= length $sub
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 9 | ▶ | $q{include_over_18} = $opts->{include_nsfw} ? 'on' : 'off'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 9 | $uri->query_form(%q); | |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 9 | my $req = GET( $uri->as_string ); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 9 | $req->header( 'Authorization' => "Bearer $token" ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 9 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 9 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 9 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 9 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | return Future->fail( | |||||||||||||||||||||||||||||||||||||||||
| 266 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 9 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 9 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 9 | return Future->done( $self->_parse_listing($data, $limit) ); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 9 | }); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 11 | }); | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | sub user_agent_string { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 19 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
user_agent_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 19 | ▶ | return $self->{user_agent} if defined $self->{user_agent}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 17 | return $self->SUPER::user_agent_string; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | __END__ | ||||||||||||||||||||||||||||||||||||||||||