| 1 | package Crypt::OpenToken::Token; | ||||||||||||
| 2 | |||||||||||||
| 3 | 7 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use Date::Parse qw(str2time); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 7 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | # XXX: this could be a *lot* smarter; right now its just a glorified hashref | ||||||||||||
| 9 | 7 | has 'version' => ( | |||||||||||
| 10 | is => 'rw', | ||||||||||||
| 11 | default => 1, | ||||||||||||
| 12 | ); | ||||||||||||
| 13 | 7 | has 'cipher' => (is => 'rw'); | |||||||||||
| 14 | 7 | has 'hmac' => (is => 'rw'); | |||||||||||
| 15 | 7 | has 'iv_length' => (is => 'rw'); | |||||||||||
| 16 | 7 | has 'iv' => (is => 'rw'); | |||||||||||
| 17 | 7 | has 'key_length' => (is => 'rw'); | |||||||||||
| 18 | 7 | has 'key' => (is => 'rw'); | |||||||||||
| 19 | 7 | has 'payload_length' => (is => 'rw'); | |||||||||||
| 20 | 7 | has 'payload' => (is => 'rw'); | |||||||||||
| 21 | 1 | ▶ | has 'data' => (is => 'rw', default => sub { {} }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | sub subject { | ||||||||||
|
Subroutine
subject: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||
| 25 | 0 | return $self->data->{subject}; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 5 | ▶ | sub is_valid { | ||||||||||
|
Subroutine
is_valid: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 29 | 5 | my $self = shift; | |||||||||||
| 30 | 5 | my %args = @_; | |||||||||||
| 31 | 5 | ▶ | my $skew = $args{clock_skew} || 5; | ||||||||||
|
Condition100%2/2
$args{'clock_skew'} || 5
MC/DC100%1/1
$args{'clock_skew'} || 5
$args{'clock_skew'}
Truth table100%2/2
A
A= $args{'clock_skew'}
| |||||||||||||
| 32 | 5 | my $now = DateTime->now(time_zone => 'UTC'); | |||||||||||
| 33 | |||||||||||||
| 34 | 5 | my $not_before = $self->not_before; | |||||||||||
| 35 | 5 | ▶ | if ($not_before) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 4 | $not_before->subtract(seconds => $skew); | |||||||||||
| 37 | 4 | ▶ | return 0 if ($now < $not_before); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | 3 | my $not_on_or_after = $self->not_on_or_after; | |||||||||||
| 41 | 3 | ▶ | if ($not_on_or_after) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 3 | $not_on_or_after->add(seconds => $skew); | |||||||||||
| 43 | 3 | ▶ | return 0 if ($now >= $not_on_or_after); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 2 | return 1; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 3 | ▶ | sub requires_renewal { | ||||||||||
|
Subroutine
requires_renewal: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 50 | 3 | my $self = shift; | |||||||||||
| 51 | 3 | my %args = @_; | |||||||||||
| 52 | 3 | ▶ | my $skew = $args{clock_skew} || 5; | ||||||||||
|
Condition50%1/2
$args{'clock_skew'} || 5
MC/DC0%0/1
$args{'clock_skew'} || 5
$args{'clock_skew'}
Truth table50%1/2
A
A= $args{'clock_skew'}
| |||||||||||||
| 53 | 3 | my $now = DateTime->now(time_zone => 'UTC'); | |||||||||||
| 54 | |||||||||||||
| 55 | 3 | my $renew_until = $self->renew_until; | |||||||||||
| 56 | 3 | ▶ | if ($renew_until) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 2 | $renew_until->add(seconds => $skew); | |||||||||||
| 58 | 2 | ▶ | return 1 if ($now > $renew_until); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 2 | return 0; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 3 | ▶ | sub renew_until { | ||||||||||
|
Subroutine
renew_until: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 65 | 3 | my $self = shift; | |||||||||||
| 66 | 3 | my $when = $self->data->{'renew-until'}; | |||||||||||
| 67 | 3 | ▶ | return $when ? $self->_parse_iso8601_to_datetime($when) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 5 | ▶ | sub not_before { | ||||||||||
|
Subroutine
not_before: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 71 | 5 | my $self = shift; | |||||||||||
| 72 | 5 | my $when = $self->data->{'not-before'}; | |||||||||||
| 73 | 5 | ▶ | return $when ? $self->_parse_iso8601_to_datetime($when) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 3 | ▶ | sub not_on_or_after { | ||||||||||
|
Subroutine
not_on_or_after: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 77 | 3 | my $self = shift; | |||||||||||
| 78 | 3 | my $when = $self->data->{'not-on-or-after'}; | |||||||||||
| 79 | 3 | ▶ | return $when ? $self->_parse_iso8601_to_datetime($when) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 9 | ▶ | sub _parse_iso8601_to_datetime { | ||||||||||
|
Subroutine
_parse_iso8601_to_datetime: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 9 | my ($self, $gmt_str) = @_; | |||||||||||
| 84 | 9 | my $time_t = str2time($gmt_str); | |||||||||||
| 85 | 9 | my $when = DateTime->from_epoch(epoch => $time_t, time_zone => 'UTC'); | |||||||||||
| 86 | 9 | return $when; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 7 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 90 | |||||||||||||
| 91 | 7 | 1; | |||||||||||
| 92 | |||||||||||||
| 93 | =head1 NAME | ||||||||||||
| 94 | |||||||||||||
| 95 | Crypt::OpenToken::Token - OpenToken data object | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 SYNOPSIS | ||||||||||||
| 98 | |||||||||||||
| 99 | use Crypt::OpenToken; | ||||||||||||
| 100 | |||||||||||||
| 101 | # the OpenToken that you're looking to validate | ||||||||||||
| 102 | my $token_string = '.....'; | ||||||||||||
| 103 | |||||||||||||
| 104 | # create factory based on shared password, and parse the token | ||||||||||||
| 105 | my $factory = Crypt::OpenToken->new(password => 'abc123'); | ||||||||||||
| 106 | my $token = $factory->parse($token_string); | ||||||||||||
| 107 | |||||||||||||
| 108 | # check if the token is still valid or requires renewal, based on | ||||||||||||
| 109 | # an allowable time skew (in seconds) | ||||||||||||
| 110 | my $skew = 5; | ||||||||||||
| 111 | |||||||||||||
| 112 | if ($token->is_valid(clock_skew => $skew)) { | ||||||||||||
| 113 | # token is valid, do something with the data | ||||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | if ($token->requires_renewal(clock_skew => $skew)) { | ||||||||||||
| 117 | # token should be renewed by authenticating the User again | ||||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 DESCRIPTION | ||||||||||||
| 121 | |||||||||||||
| 122 | This module implements the data representation of an OpenToken. | ||||||||||||
| 123 | |||||||||||||
| 124 | =head1 METHODS | ||||||||||||
| 125 | |||||||||||||
| 126 | =over | ||||||||||||
| 127 | |||||||||||||
| 128 | =item subject() | ||||||||||||
| 129 | |||||||||||||
| 130 | Returns the "subject" field as specified in the token data. | ||||||||||||
| 131 | |||||||||||||
| 132 | =item is_valid(clock_skew => $allowable_skew) | ||||||||||||
| 133 | |||||||||||||
| 134 | Checks to see if the OpenToken is valid, based on the standard fields | ||||||||||||
| 135 | specified in the IETF draft specification. | ||||||||||||
| 136 | |||||||||||||
| 137 | Can accept an optional C<clock_skew> parameter, which specifies the amount of | ||||||||||||
| 138 | allowable clock skew (in seconds). Defaults to "5 seconds". | ||||||||||||
| 139 | |||||||||||||
| 140 | =item requires_renewal(clock_skew => $allowable_skew) | ||||||||||||
| 141 | |||||||||||||
| 142 | Checks to see if the OpenToken is past its "renew-until" timestamp, and | ||||||||||||
| 143 | requires that it be renewed by re-authenticating the User. B<Not> | ||||||||||||
| 144 | automatically renewed/reissued, but by B<re-authenticating> the User. | ||||||||||||
| 145 | |||||||||||||
| 146 | Can accept an optional C<clock_skew> parameter, which specifies the amount of | ||||||||||||
| 147 | allowable clock skew (in seconds). Defaults to "5 seconds". | ||||||||||||
| 148 | |||||||||||||
| 149 | =item renew_until() | ||||||||||||
| 150 | |||||||||||||
| 151 | Returns a C<DateTime> object representing the "renew-until" field specified in | ||||||||||||
| 152 | the token data; the date/time at which the token B<must not> automatically be | ||||||||||||
| 153 | re-issued without further authentication. | ||||||||||||
| 154 | |||||||||||||
| 155 | If no "renew-until" field was specified, this method returns C<undef>. | ||||||||||||
| 156 | |||||||||||||
| 157 | =item not_before() | ||||||||||||
| 158 | |||||||||||||
| 159 | Returns a C<DateTime> object representing the "not-before" field specified in | ||||||||||||
| 160 | the token data; the date/time when the token was created. A token received | ||||||||||||
| 161 | before this date/time B<must> be rejected as invalid. | ||||||||||||
| 162 | |||||||||||||
| 163 | If no "not-before" field was specified, this method returns C<undef>. | ||||||||||||
| 164 | |||||||||||||
| 165 | =item not_on_or_after() | ||||||||||||
| 166 | |||||||||||||
| 167 | Returns a C<DateTime> object representing the "not-on-or-after" field | ||||||||||||
| 168 | specified in the token data; the time/time at which the token will expire. A | ||||||||||||
| 169 | token received on or after this date/time B<must> be rejected as invalid. | ||||||||||||
| 170 | |||||||||||||
| 171 | If no "not-on-or-after" field was specified, this method returns C<undef>. | ||||||||||||
| 172 | |||||||||||||
| 173 | =back | ||||||||||||
| 174 | |||||||||||||
| 175 | =head1 AUTHOR | ||||||||||||
| 176 | |||||||||||||
| 177 | Graham TerMarsch (cpan@howlingfrog.com) | ||||||||||||
| 178 | |||||||||||||
| 179 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 180 | |||||||||||||
| 181 | C<Crypt::OpenToken> is Copyright (C) 2010, Socialtext, and is released under | ||||||||||||
| 182 | the Artistic-2.0 license. | ||||||||||||
| 183 | |||||||||||||
| 184 | =cut | ||||||||||||