| 1 | package Net::Twitch::Oauth2; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use JSON::Any; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | use constant ACCESS_TOKEN_URL => 'https://api.twitch.tv/kraken/oauth2/token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use constant AUTHORIZE_URL => 'https://api.twitch.tv/kraken/oauth2/authorize'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 6 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||
| 17 | 6 | my ($class,%options) = @_; | |||||||||||||||||||
| 18 | 6 | my $self = {}; | |||||||||||||||||||
| 19 | 6 | $self->{options} = \%options; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 6 | ▶ | if (!$options{access_token}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 3 | ▶ | croak "You must provide your application id when construct new method\n EPI::Twitch::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 2 | ▶ | croak "You must provide your application secret when construct new method\n EPI::Twitch::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 4 | ▶ | $self->{browser} = $options{browser} || LWP::UserAgent->new; | ||||||||||||||||||
|
Condition66%2/3
$options{'browser'} || 'LWP::UserAgent'->new
MC/DC0%0/2
$options{'browser'} || 'LWP::UserAgent'->new
$options{'browser'}
'LWP::UserAgent'->new
Truth table66%2/3
A || B
A= $options{'browser'}B= 'LWP::UserAgent'->new
| |||||||||||||||||||||
| 27 | 4 | ▶ | $self->{access_token_url} = $options{access_token_url} || ACCESS_TOKEN_URL; | ||||||||||||||||||
|
Condition50%1/2
$options{'access_token_url'} || 'https://api.twitch.tv/kraken/oauth2/token'
MC/DC0%0/1
$options{'access_token_url'} || 'https://api.twitch.tv/kraken/oauth2/token'
$options{'access_token_url'}
Truth table50%1/2
A
A= $options{'access_token_url'}
| |||||||||||||||||||||
| 28 | 4 | ▶ | $self->{authorize_url} = $options{authorize_url} || AUTHORIZE_URL; | ||||||||||||||||||
|
Condition50%1/2
$options{'authorize_url'} || 'https://api.twitch.tv/kraken/oauth2/authorize'
MC/DC0%0/1
$options{'authorize_url'} || 'https://api.twitch.tv/kraken/oauth2/authorize'
$options{'authorize_url'}
Truth table50%1/2
A
A= $options{'authorize_url'}
| |||||||||||||||||||||
| 29 | 4 | $self->{access_token} = $options{access_token}; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 4 | return bless($self, $class); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | ▶ | sub get_authorization_url { | ||||||||||||||||||
|
Subroutine
get_authorization_url: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | my ($self,%params) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | ▶ | $params{callback} ||= $self->{options}->{callback}; | ||||||||||||||||||
|
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"}
$self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
| |||||||||||||||||||||
| 38 | 0 | ▶ | croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | my $scope = join(",", @{$params{scope}}) if defined($params{scope}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | my $url = $self->{authorize_url} | ||||||||||||||||||||
| 43 | .'?response_type=code' | ||||||||||||||||||||
| 44 | .'&client_id=' | ||||||||||||||||||||
| 45 | .uri_escape($self->{options}->{application_id}) | ||||||||||||||||||||
| 46 | .'&redirect_uri=' | ||||||||||||||||||||
| 47 | 0 | .uri_escape($params{callback}); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | $url .= "&scope=$scope" if $scope; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | return $url; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub post_access_token { | ||||||||||||||||||
|
Subroutine
post_access_token: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 55 | 0 | my ($self,%params) = @_; | |||||||||||||||||||
| 56 | 0 | ▶ | $params{callback} ||= $self->{options}->{callback}; | ||||||||||||||||||
|
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"}
$self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
| |||||||||||||||||||||
| 57 | 0 | ▶ | $params{code} ||= $self->{options}->{code}; | ||||||||||||||||||
|
Condition0%0/3
$params{"code"} ||= $self->{"options"}{"code"}
MC/DC0%0/2
$params{"code"} ||= $self->{"options"}{"code"}
$params{"code"}
$self->{"options"}{"code"}
Truth table0%0/3
A ||= B
A= $params{"code"}B= $self->{"options"}{"code"}
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 0 | ▶ | croak "You must pass callback URL" unless defined $params{callback}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | $self->{options}->{code} = $params{code}; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | ###generating access token URL | ||||||||||||||||||||
| 64 | my $getURL = $self->{access_token_url} | ||||||||||||||||||||
| 65 | .'?client_id=' | ||||||||||||||||||||
| 66 | .uri_escape($self->{options}->{application_id}) | ||||||||||||||||||||
| 67 | .'&client_secret=' | ||||||||||||||||||||
| 68 | .uri_escape($self->{options}->{application_secret}) | ||||||||||||||||||||
| 69 | .'&grant_type=authorization_code' | ||||||||||||||||||||
| 70 | .'&redirect_uri=' | ||||||||||||||||||||
| 71 | .uri_escape($params{callback}) | ||||||||||||||||||||
| 72 | .'&code=' | ||||||||||||||||||||
| 73 | 0 | .uri_escape($params{code}); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | my $response = $self->{browser}->post($getURL); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | ##got an error response from twitch | ||||||||||||||||||||
| 78 | ##die and display error message | ||||||||||||||||||||
| 79 | 0 | my $j = JSON::Any->new; | |||||||||||||||||||
| 80 | 0 | ▶ | if (!$response->is_success){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | my $error = $j->jsonToObj($response->content()); | |||||||||||||||||||
| 82 | 0 | croak "'" .$error->{error}->{type}. "'" . " " .$error->{error}->{message}; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | ##everything is ok proccess response and extract access token | ||||||||||||||||||||
| 86 | 0 | my $reply = $j->jsonToObj($response->content()); | |||||||||||||||||||
| 87 | 0 | my $token = $reply->{access_token}; | |||||||||||||||||||
| 88 | 0 | my $expires = $reply->{refresh_token}; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | ###save access token | ||||||||||||||||||||
| 91 | 0 | ▶ | if ($token){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | $self->{access_token} = $token; | |||||||||||||||||||
| 93 | 0 | return $token; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | croak "can't get access token"; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=6 SCAR=19.2
Pod
undocumented
| |||||||||||||||||||||
| 100 | 2 | my ($self,$url,$params) = @_; | |||||||||||||||||||
| 101 | 2 | ▶ | unless ($self->_has_access_token($url)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 2 | ▶ | croak "You must pass access_token" unless defined $self->{access_token}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 2 | ▶ | $url .= $self->{_has_query} ? '&' : '?'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 2 | $url .= "oauth_token=" . $self->{access_token}; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | ##construct the new url | ||||||||||||||||||||
| 108 | 2 | my @array; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 2 | ▶ | while ( my ($key, $value) = each(%{$params})){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | $value = uri_escape($value); | |||||||||||||||||||
| 112 | 0 | push(@array, "$key=$value"); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 2 | my $string = join('&', @array); | |||||||||||||||||||
| 116 | 2 | ▶ | $url .= "&".$string if $string; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 2 | my $response = $self->{browser}->get($url); | |||||||||||||||||||
| 119 | 2 | my $content = $response->content(); | |||||||||||||||||||
| 120 | 2 | return $self->_content($content); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 0 | ▶ | sub post { | ||||||||||||||||||
|
Subroutine
post: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 124 | 0 | my ($self,$url,$params) = @_; | |||||||||||||||||||
| 125 | 0 | ▶ | unless ($self->_has_access_token($url)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | ▶ | croak "You must pass access_token" unless defined $self->{access_token}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | $params->{oauth_token} = $self->{access_token}; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | 0 | my $response = $self->{browser}->post($url,$params); | |||||||||||||||||||
| 130 | 0 | my $content = $response->content(); | |||||||||||||||||||
| 131 | 0 | return $self->_content($content); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | ▶ | sub delete { | ||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 135 | 1 | my ($self,$url,$params) = @_; | |||||||||||||||||||
| 136 | 1 | ▶ | unless ($self->_has_access_token($url)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 1 | ▶ | croak "You must pass access_token" unless defined $self->{access_token}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 1 | $params->{oauth_token} = $self->{access_token}; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 1 | my $response = $self->{browser}->delete($url,$params); | |||||||||||||||||||
| 141 | 1 | my $content = $response->content(); | |||||||||||||||||||
| 142 | 1 | return $self->_content($content); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | ▶ | sub as_hash { | ||||||||||||||||||
|
Subroutine
as_hash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 146 | 0 | my ($self) = @_; | |||||||||||||||||||
| 147 | 0 | my $j = JSON::Any->new; | |||||||||||||||||||
| 148 | 0 | return $j->jsonToObj($self->{content}); | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 3 | ▶ | sub as_json { | ||||||||||||||||||
|
Subroutine
as_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 152 | 3 | my ($self) = @_; | |||||||||||||||||||
| 153 | 3 | return $self->{content}; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 3 | ▶ | sub _content { | ||||||||||||||||||
|
Subroutine
_content: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 157 | 3 | my ($self,$content) = @_; | |||||||||||||||||||
| 158 | 3 | $self->{content} = $content; | |||||||||||||||||||
| 159 | 3 | return $self; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 3 | ▶ | sub _has_access_token { | ||||||||||||||||||
|
Subroutine
_has_access_token: called CC=2 SCAR=7.5
| |||||||||||||||||||||
| 163 | 3 | my ($self, $url) = @_; | |||||||||||||||||||
| 164 | 3 | my $uri = URI->new($url); | |||||||||||||||||||
| 165 | 3 | my %q = $uri->query_form; | |||||||||||||||||||
| 166 | #also check if we have a query and save result | ||||||||||||||||||||
| 167 | 3 | $self->{_has_query} = $uri->query(); | |||||||||||||||||||
| 168 | 0 | ▶ | if (grep { $_ eq 'oauth_token' } keys %q) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | 0 | return 1; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | 3 | return; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 2 | 1; | |||||||||||||||||||