| 1 | package Net::Simplify::AccessToken; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Net::Simplify::AccessToken - Simplify Commerce OAuth access token | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Net::Simplify; | ||
| 10 | |||
| 11 | my $auth_code = "YOUR AUTH CODE"; | ||
| 12 | my $redirect_uri = "YOUR REDIRECT URI"; | ||
| 13 | |||
| 14 | my $token = Net::Simplify::AccessToken->create($auth_code, $redirect_uri); | ||
| 15 | printf "Access token %s\n", $token->access_token; | ||
| 16 | |||
| 17 | # Create Authentication object using the access token | ||
| 18 | my $auth = Net::Simplify::Authentication->create({ | ||
| 19 | public_key => 'YOUR PUBLIC_KEY', | ||
| 20 | private_key => 'YOUR PRIVATE KEY', | ||
| 21 | access_token => $token->access_token | ||
| 22 | }); | ||
| 23 | |||
| 24 | # Create a payment using the OAuth credentials | ||
| 25 | my $payment = Net::Simplify::Payment->create({...}, $auth); | ||
| 26 | |||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Access tokens provide a way of performing operations on behalf of another user. To create | ||
| 31 | an access token an authentication code is required. Once the access token has been created | ||
| 32 | the access token value can be used to construct an L<Net::Simplify::Authentication> | ||
| 33 | object which can be passed to API calls. | ||
| 34 | |||
| 35 | =head2 METHODS | ||
| 36 | |||
| 37 | =head3 create($auth_code, $redirect_uri, $auth) | ||
| 38 | |||
| 39 | Creates an AccessToken object. The parameters are: | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item C<$auth_code> | ||
| 44 | |||
| 45 | The authentication code obtained during the OAuth login process. | ||
| 46 | |||
| 47 | =item C<$redirect_uri> | ||
| 48 | |||
| 49 | The redirect URI for the OAuth login process. This must match the registered value. | ||
| 50 | |||
| 51 | =item C<$auth> | ||
| 52 | |||
| 53 | Authentication object for accessing the API. If no value is passed the global keys | ||
| 54 | C<$Net::Simplify::public_key> and C<$Net::Simplify::private_key> are used. | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | =head3 refresh() | ||
| 59 | |||
| 60 | Refresh the token obtaining new access and refresh tokens. Authentication is done | ||
| 61 | using the same credentials used when the AccessToken was created. | ||
| 62 | |||
| 63 | =head3 revoke() | ||
| 64 | |||
| 65 | Revokes the access token. Authentication is done using the same credentials used when the AccessToken was created. | ||
| 66 | |||
| 67 | =head3 access_token($value) | ||
| 68 | |||
| 69 | Returns the current value of the access token. | ||
| 70 | If the $value parameter is passed in it is used to set the value of the access token. | ||
| 71 | |||
| 72 | |||
| 73 | =head3 refresh_token($value) | ||
| 74 | |||
| 75 | Returns the current value of the refresh token. | ||
| 76 | If the $value parameter is passed in it is used to set the value of the refresh token. | ||
| 77 | |||
| 78 | =head3 expires_in($value) | ||
| 79 | |||
| 80 | |||
| 81 | Returns the period in seconds since the access token was created or refreshed to when | ||
| 82 | the token expires. | ||
| 83 | If the $value parameter is passed in it is used to set the value of the expiry. | ||
| 84 | |||
| 85 | =head1 SEE ALSO | ||
| 86 | |||
| 87 | L<Net::Simplify>, | ||
| 88 | L<Net::Simplify::Authentication>, | ||
| 89 | L<http://www.simplify.com> | ||
| 90 | |||
| 91 | =head1 VERSION | ||
| 92 | |||
| 93 | 1.6.0 | ||
| 94 | |||
| 95 | =head1 LICENSE | ||
| 96 | |||
| 97 | Copyright (c) 2013 - 2022 MasterCard International Incorporated | ||
| 98 | All rights reserved. | ||
| 99 | |||
| 100 | Redistribution and use in source and binary forms, with or without modification, are | ||
| 101 | permitted provided that the following conditions are met: | ||
| 102 | |||
| 103 | Redistributions of source code must retain the above copyright notice, this list of | ||
| 104 | conditions and the following disclaimer. | ||
| 105 | Redistributions in binary form must reproduce the above copyright notice, this list of | ||
| 106 | conditions and the following disclaimer in the documentation and/or other materials | ||
| 107 | provided with the distribution. | ||
| 108 | Neither the name of the MasterCard International Incorporated nor the names of its | ||
| 109 | contributors may be used to endorse or promote products derived from this software | ||
| 110 | without specific prior written permission. | ||
| 111 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
| 112 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 113 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| 114 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 115 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
| 116 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| 117 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
| 118 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 119 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 120 | SUCH DAMAGE. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | use 5.006; | ||
| 125 | use strict; | ||
| 126 | use warnings FATAL => 'all'; | ||
| 127 | |||
| 128 | use Net::Simplify::Domain; | ||
| 129 | |||
| 130 | our @ISA = qw(Net::Simplify::Domain); | ||
| 131 | |||
| 132 | use Carp; | ||
| 133 | |||
| 134 | sub create { | ||
| 135 | my ($class, $auth_code, $redirect_uri, $auth) = @_; | ||
| 136 | |||
| 137 | my $params = { | ||
| 138 | grant_type => 'authorization_code', | ||
| 139 | code => $auth_code, | ||
| 140 | redirect_uri => $redirect_uri | ||
| 141 | }; | ||
| 142 | |||
| 143 | $auth = Net::Simplify::SimplifyApi->get_authentication($auth); | ||
| 144 | my $result = Net::Simplify::SimplifyApi->send_auth_request($params, 'token', $auth); | ||
| 145 | |||
| 146 | $class->SUPER::new($result, $auth); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub refresh { | ||
| 150 | my ($self) = @_; | ||
| 151 | |||
| 152 | my $auth = $self->{_authentication}; | ||
| 153 | |||
| 154 | my $rt = $self->{refresh_token}; | ||
| 155 | if (!defined $rt) { | ||
| 156 | croak(Net::Simplify::IllegalArgumentException->new("refresh token value is missing from access token object")); | ||
| 157 | } | ||
| 158 | |||
| 159 | my $params = { | ||
| 160 | grant_type => 'refresh_token', | ||
| 161 | refresh_token => $rt | ||
| 162 | }; | ||
| 163 | |||
| 164 | my $result = Net::Simplify::SimplifyApi->send_auth_request($params, 'token', $auth); | ||
| 165 | |||
| 166 | $self->merge($result); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub revoke { | ||
| 170 | my ($self) = @_; | ||
| 171 | |||
| 172 | my $auth = $self->{_authentication}; | ||
| 173 | |||
| 174 | my $at = $self->{access_token}; | ||
| 175 | if (!defined $at) { | ||
| 176 | croak(Net::Simplify::IllegalArgumentException->new("access token value is missing from access token object")); | ||
| 177 | } | ||
| 178 | |||
| 179 | my $params = { | ||
| 180 | token => $at | ||
| 181 | }; | ||
| 182 | |||
| 183 | my $result = Net::Simplify::SimplifyApi->send_auth_request($params, 'revoke', $auth); | ||
| 184 | |||
| 185 | $self->clear(); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub access_token { | ||
| 189 | my ($self, $v) = @_; | ||
| 190 | |||
| 191 | $self->{access_token} = $v if defined $v; | ||
| 192 | |||
| 193 | $self->{access_token}; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub refresh_token { | ||
| 197 | my ($self, $v) = @_; | ||
| 198 | |||
| 199 | $self->{refresh_token} = $v if defined $v; | ||
| 200 | |||
| 201 | $self->{refresh_token}; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub expires_in { | ||
| 205 | my ($self, $v) = @_; | ||
| 206 | |||
| 207 | $self->{expires_in} = $v if defined $v; | ||
| 208 | |||
| 209 | $self->{expires_in}; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | 1; |