| 1 | # Copyrights 2013-2019 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution Net-OAuth2. Meta-POD processed | ||
| 6 | # with OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package Net::OAuth2::AccessToken; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '0.65'; | ||
| 12 | |||
| 13 | use warnings; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | our $VERSION; # to be able to test in devel environment | ||
| 17 | |||
| 18 | use JSON qw/encode_json/; | ||
| 19 | use URI::Escape qw/uri_escape/; | ||
| 20 | use Encode qw/find_encoding/; | ||
| 21 | |||
| 22 | # Attributes to be saved to preserve the session. | ||
| 23 | my @session = qw/access_token token_type refresh_token expires_at | ||
| 24 | scope state auto_refresh/; | ||
| 25 | |||
| 26 | # This class name is kept for backwards compatibility: a better name | ||
| 27 | # would have been: Net::OAuth2::Session, with a ::Token::Bearer split-off. | ||
| 28 | |||
| 29 | # In the future, most of this functionality will probably need to be | ||
| 30 | # split-off in a base class ::Token, to be shared with a new extension | ||
| 31 | # which supports HTTP-MAC tokens as proposed by ietf dragt | ||
| 32 | # http://datatracker.ietf.org/doc/draft-ietf-oauth-v2-http-mac/ | ||
| 33 | |||
| 34 | |||
| 35 | sub new(@) { my $class = shift; (bless {}, $class)->init({@_}) } | ||
| 36 | |||
| 37 | sub init($) | ||
| 38 | { my ($self, $args) = @_; | ||
| 39 | |||
| 40 | $self->{NOA_expires_at} = $args->{expires_at} | ||
| 41 | || ($args->{expires_in} ? time()+$args->{expires_in} : undef); | ||
| 42 | |||
| 43 | # client is the pre-v0.50 name | ||
| 44 | my $profile = $self->{NOA_profile} = $args->{profile} || $args->{client} | ||
| 45 | or die "::AccessToken needs profile object"; | ||
| 46 | |||
| 47 | $self->{NOA_access_token} = $args->{access_token}; | ||
| 48 | $self->{NOA_refresh_token} = $args->{refresh_token}; | ||
| 49 | $self->{NOA_refresh_always}= $args->{refresh_always}; | ||
| 50 | $self->{NOA_scope} = $args->{scope}; | ||
| 51 | $self->{NOA_state} = $args->{state}; | ||
| 52 | $self->{NOA_hd} = $args->{hd}; | ||
| 53 | $self->{NOA_token_type} = $args->{token_type}; | ||
| 54 | $self->{NOA_auto_refresh} = $args->{auto_refresh}; | ||
| 55 | $self->{NOA_changed} = $args->{changed}; | ||
| 56 | |||
| 57 | $self->{NOA_error} = $args->{error}; | ||
| 58 | $self->{NOA_error_uri} = $args->{error_uri}; | ||
| 59 | $self->{NOA_error_descr} = $args->{error_description} || $args->{error}; | ||
| 60 | |||
| 61 | $self->{NOA_attr} = $args; | ||
| 62 | $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub session_thaw($%) | ||
| 67 | { my ($class, $session) = (shift, shift); | ||
| 68 | # we can use $session->{net_oauth2_version} to upgrade the info | ||
| 69 | $class->new(%$session, @_); | ||
| 70 | } | ||
| 71 | |||
| 72 | #-------------- | ||
| 73 | |||
| 74 | sub token_type() {shift->{NOA_token_type}} | ||
| 75 | sub scope() {shift->{NOA_scope}} | ||
| 76 | sub state() {shift->{NOA_state}} | ||
| 77 | sub hd() {shift->{NOA_hd}} | ||
| 78 | sub profile() {shift->{NOA_profile}} | ||
| 79 | |||
| 80 | |||
| 81 | sub attribute($) { $_[0]->{NOA_attr}{$_[1]} } | ||
| 82 | |||
| 83 | |||
| 84 | sub changed(;$) | ||
| 85 | { my $s = shift; @_ ? $s->{NOA_changed} = shift : $s->{NOA_changed} } | ||
| 86 | |||
| 87 | |||
| 88 | sub access_token() | ||
| 89 | { my $self = shift; | ||
| 90 | |||
| 91 | if($self->expired) | ||
| 92 | { delete $self->{NOA_access_token}; | ||
| 93 | $self->{NOA_changed} = 1; | ||
| 94 | $self->refresh if $self->auto_refresh; | ||
| 95 | } | ||
| 96 | elsif($self->refresh_always) | ||
| 97 | { $self->refresh; | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->{NOA_access_token}; | ||
| 101 | } | ||
| 102 | |||
| 103 | #--------------- | ||
| 104 | |||
| 105 | sub error() {shift->{NOA_error}} | ||
| 106 | sub error_uri() {shift->{NOA_error_uri}} | ||
| 107 | sub error_description() {shift->{NOA_error_descr}} | ||
| 108 | |||
| 109 | #--------------- | ||
| 110 | |||
| 111 | sub refresh_token() {shift->{NOA_refresh_token}} | ||
| 112 | sub refresh_always() {shift->{NOA_refresh_always}} | ||
| 113 | sub auto_refresh() {shift->{NOA_auto_refresh}} | ||
| 114 | |||
| 115 | |||
| 116 | sub expires_at() { shift->{NOA_expires_at} } | ||
| 117 | |||
| 118 | |||
| 119 | sub expires_in() { shift->expires_at - time() } | ||
| 120 | |||
| 121 | |||
| 122 | sub expired(;$) | ||
| 123 | { my ($self, $after) = @_; | ||
| 124 | my $when = $self->expires_at or return; | ||
| 125 | $after = 15 unless defined $after; | ||
| 126 | $when < time() + $after; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub update_token($$$;$) | ||
| 131 | { my ($self, $token, $type, $exp, $refresh) = @_; | ||
| 132 | $self->{NOA_access_token} = $token; | ||
| 133 | $self->{NOA_token_type} = $type if $type; | ||
| 134 | $self->{NOA_expires_at} = $exp; | ||
| 135 | |||
| 136 | $self->{NOA_refresh_token} = $refresh | ||
| 137 | if defined $refresh; | ||
| 138 | |||
| 139 | $token; | ||
| 140 | } | ||
| 141 | |||
| 142 | #-------------- | ||
| 143 | |||
| 144 | sub to_json() | ||
| 145 | { my $self = shift; | ||
| 146 | encode_json $self->session_freeze; | ||
| 147 | } | ||
| 148 | *to_string = \&to_json; # until v0.50 | ||
| 149 | |||
| 150 | |||
| 151 | sub session_freeze(%) | ||
| 152 | { my ($self, %args) = @_; | ||
| 153 | my %data = (net_oauth2_version => $VERSION); | ||
| 154 | defined $self->{"NOA_$_"} && ($data{$_} = $self->{"NOA_$_"}) for @session; | ||
| 155 | $self->changed(0); | ||
| 156 | \%data; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | sub refresh() | ||
| 161 | { my $self = shift; | ||
| 162 | $self->profile->update_access_token($self); | ||
| 163 | } | ||
| 164 | |||
| 165 | #-------------- | ||
| 166 | |||
| 167 | sub request{ my $s = shift; $s->profile->request_auth($s, @_) } | ||
| 168 | sub get { my $s = shift; $s->profile->request_auth($s, 'GET', @_) } | ||
| 169 | sub post { my $s = shift; $s->profile->request_auth($s, 'POST', @_) } | ||
| 170 | sub delete { my $s = shift; $s->profile->request_auth($s, 'DELETE', @_) } | ||
| 171 | sub put { my $s = shift; $s->profile->request_auth($s, 'PUT', @_) } | ||
| 172 | |||
| 173 | 1; |