| 1 | package OAuth::Lite::Token; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | use OAuth::Lite::Util qw( | |||||||||||||||||||||||||||
| 9 | encode_param | ||||||||||||||||||||||||||||
| 10 | decode_param | ||||||||||||||||||||||||||||
| 11 | gen_random_key | ||||||||||||||||||||||||||||
| 12 | 6 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | __PACKAGE__->mk_accessors(qw/token secret callback_confirmed/); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | OAuth::Lite::Token - token class | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $token = OAuth::Lite::Token->new( | ||||||||||||||||||||||||||||
| 23 | token => 'foo', | ||||||||||||||||||||||||||||
| 24 | secret => 'bar', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # or | ||||||||||||||||||||||||||||
| 28 | my $token = OAuth::Lite::Token->new; | ||||||||||||||||||||||||||||
| 29 | $tokne->token('foo'); | ||||||||||||||||||||||||||||
| 30 | $secret->secret('bar'); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # and you also can make token which two params are filled in with random values. | ||||||||||||||||||||||||||||
| 33 | my $token = OAuth::Lite::Token->new_random; | ||||||||||||||||||||||||||||
| 34 | say $token->token; | ||||||||||||||||||||||||||||
| 35 | say $token->secret; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $encoded = $token->as_encoded; | ||||||||||||||||||||||||||||
| 38 | say $encoded; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | my $new_token = OAuth::Lite::Token->from_encoded($encoded); | ||||||||||||||||||||||||||||
| 41 | say $new_token->token; | ||||||||||||||||||||||||||||
| 42 | say $new_token->secret; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Token class. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 METHODS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head3 parameters | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item token | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item secret | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =back | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub new { | ||||||||||||||||||||||||||||
| 65 | 16 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 16 | bless { | |||||||||||||||||||||||||||
| 67 | token => undef, | ||||||||||||||||||||||||||||
| 68 | secret => undef, | ||||||||||||||||||||||||||||
| 69 | callback_confirmed => 0, | ||||||||||||||||||||||||||||
| 70 | %args | ||||||||||||||||||||||||||||
| 71 | }, $class; | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 new_random | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Generate new object. and automatically filled token and secret value with random key. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | my $t = OAuth::Lite::Token->new_random; | ||||||||||||||||||||||||||||
| 79 | say $t->token; | ||||||||||||||||||||||||||||
| 80 | say $t->secret; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub new_random { | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_random: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 1 | my $token = $class->new;; | |||||||||||||||||||||||||||
| 87 | 1 | $token->token(gen_random_key()); | |||||||||||||||||||||||||||
| 88 | 1 | $token->secret(gen_random_key()); | |||||||||||||||||||||||||||
| 89 | 1 | $token; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 token($token_value) | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Getter/Setter for token value. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | $token->token('foo'); | ||||||||||||||||||||||||||||
| 97 | say $token->token; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 secret($token_secret) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Getter/Setter for secret value. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | $token->secret('bar'); | ||||||||||||||||||||||||||||
| 104 | say $token->secret; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 from_encoded($encoded) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Generate token from encoded line (that service provider provides as response of request token.). | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $line = "oauth_token=foo&oauth_token_secret=bar"; | ||||||||||||||||||||||||||||
| 111 | my $token = OAuth::Lite::Token->from_encoded($encoded); | ||||||||||||||||||||||||||||
| 112 | say $token->token; | ||||||||||||||||||||||||||||
| 113 | say $token->secret; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub from_encoded { | ||||||||||||||||||||||||||||
| 118 | 5 | ▶ | my ($class, $encoded) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_encoded: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | $encoded =~ s/\r\n$//; | |||||||||||||||||||||||||||
| 121 | 5 | $encoded =~ s/\n$//; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | my $token = $class->new; | |||||||||||||||||||||||||||
| 124 | 5 | for my $pair (split /&/, $encoded) { | |||||||||||||||||||||||||||
| 125 | 12 | my ($key, $val) = split /=/, $pair; | |||||||||||||||||||||||||||
| 126 | 12 | ▶ | if ($key eq 'oauth_token') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 127 | 5 | $token->token(decode_param($val)); | |||||||||||||||||||||||||||
| 128 | } elsif ($key eq 'oauth_token_secret') { | ||||||||||||||||||||||||||||
| 129 | 5 | $token->secret(decode_param($val)); | |||||||||||||||||||||||||||
| 130 | } elsif ($key eq 'oauth_callback_confirmed') { | ||||||||||||||||||||||||||||
| 131 | 2 | my $p = decode_param($val); | |||||||||||||||||||||||||||
| 132 | 2 | ▶ | if ($p && $p eq 'true') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p and $p eq "true"
MC/DC50%1/2
$p and $p eq "true"
$p
$p eq "true"
Truth table66%2/3
A and B
A= $pB= $p eq "true"
| |||||||||||||||||||||||||||||
| 133 | 1 | $token->callback_confirmed(1); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 5 | return $token; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 as_encoded | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Returns encoded line from token object. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $token = OAuth::Lite::Token->new( | ||||||||||||||||||||||||||||
| 145 | token => 'foo', | ||||||||||||||||||||||||||||
| 146 | secret => 'bar', | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | say $token->as_encoded; #oauth_token=foo&oauth_token_secret=bar | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub as_encoded { | ||||||||||||||||||||||||||||
| 153 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
as_encoded: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | my $token = $self->token || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->token || ''
MC/DC0%0/1
$self->token || ''
$self->token
Truth table50%1/2
A
A= $self->token
| |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | my $secret = $self->secret || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->secret || ''
MC/DC0%0/1
$self->secret || ''
$self->secret
Truth table50%1/2
A
A= $self->secret
| |||||||||||||||||||||||||||||
| 156 | 2 | my $encoded = sprintf(q{oauth_token=%s&oauth_token_secret=%s}, | |||||||||||||||||||||||||||
| 157 | encode_param($token), | ||||||||||||||||||||||||||||
| 158 | encode_param($secret)); | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | ▶ | $encoded .= q{&oauth_callback_confirmed=true} if $self->callback_confirmed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 2 | return $encoded; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Lyo Kato, C<lyo.kato _at_ gmail.com> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 171 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||||||||||||||||||||||||||||
| 172 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 6 | 1; | |||||||||||||||||||||||||||