| 1 | package Catalyst::Plugin::Authentication::Credential::GooglePlus; | ||
| 2 | |||
| 3 | use Crypt::OpenSSL::X509; | ||
| 4 | use JSON::WebToken; | ||
| 5 | use JSON::MaybeXS; | ||
| 6 | use MIME::Base64; | ||
| 7 | use LWP::Simple qw(get); | ||
| 8 | use Date::Parse qw(str2time); | ||
| 9 | use Try::Tiny; | ||
| 10 | |||
| 11 | use strictures 1; | ||
| 12 | |||
| 13 | our $VERSION = 0.1; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Catalyst::Authentication::Credential::GooglePlus - Authenticates a user using a | ||
| 18 | Google Plus token. | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | 'Plugin::Authentication' => { | ||
| 23 | default => { | ||
| 24 | credential => { | ||
| 25 | class => 'GooglePlus', | ||
| 26 | token_field => 'id_token', | ||
| 27 | public_cert_url => 'https://www.googleapis.com/oauth2/v1/certs', | ||
| 28 | }, | ||
| 29 | store => { | ||
| 30 | class => 'DBIx::Class', | ||
| 31 | user_model => 'DB::User', | ||
| 32 | ... | ||
| 33 | }, | ||
| 34 | }, | ||
| 35 | }, | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Retrieves Google's public certificates, and then retrieves the key from the | ||
| 40 | cert using L<Crypt::OpenSSL::X509>. Finally, uses the pubkey to decrypt a | ||
| 41 | Google token using L<JSON::WebToken>. | ||
| 42 | |||
| 43 | See https://github.com/errietta/Catalyst-Plugin-Authentication-Credential-GooglePlus-example | ||
| 44 | for an example. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my ($class, $config, $app, $realm) = @_; | ||
| 50 | $class = ref $class || $class; | ||
| 51 | |||
| 52 | my $self = { | ||
| 53 | %{ $config }, | ||
| 54 | %{ $realm->{config} }, | ||
| 55 | _app => $app, | ||
| 56 | _realm => $realm, | ||
| 57 | }; | ||
| 58 | |||
| 59 | # Google names it id_token | ||
| 60 | $self->{token_field} ||= "id_token"; | ||
| 61 | |||
| 62 | bless $self, $class; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head1 METHODS | ||
| 66 | |||
| 67 | =head2 authenticate | ||
| 68 | |||
| 69 | Retrieves a JSON web token from either $authinfo, or GET or POST query | ||
| 70 | parameters. If null, throws exception. | ||
| 71 | |||
| 72 | Otherwise, decodes (with L</decode>) the token, and calls find_user on the | ||
| 73 | given L<Catalyst::Authentication::Realm> object with the data retrieved from | ||
| 74 | decoding the token. | ||
| 75 | |||
| 76 | =head3 ARGUMENTS | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item $c | ||
| 81 | |||
| 82 | Catalyst object. | ||
| 83 | |||
| 84 | =item $realm | ||
| 85 | |||
| 86 | Catalyst::Authentication::Realm (or subclass) object. | ||
| 87 | |||
| 88 | =item $authinfo | ||
| 89 | |||
| 90 | Optional, authentication info that can contain the token. If not given, the | ||
| 91 | token is retrieved from GET or POST parameters. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head3 RETURNS | ||
| 96 | |||
| 97 | User found by calling L<Catalyst::Authentication::Realm/find_user> with the | ||
| 98 | decoded token's information, if any. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub authenticate { | ||
| 103 | my ($self, $c, $realm, $authinfo) = @_; | ||
| 104 | |||
| 105 | my $field = $self->{token_field}; | ||
| 106 | |||
| 107 | if (my $cache = $self->get_cache($c)) { | ||
| 108 | $self->{cache} ||= $cache; | ||
| 109 | } | ||
| 110 | |||
| 111 | my $id_token = $authinfo->{$field}; | ||
| 112 | |||
| 113 | $id_token ||= $c->req->method eq 'GET' ? | ||
| 114 | $c->req->query_params->{$field} : $c->req->body_params->{$field}; | ||
| 115 | |||
| 116 | unless ($id_token) { | ||
| 117 | Catalyst::Exception->throw("$field not specified."); | ||
| 118 | } | ||
| 119 | |||
| 120 | my $userinfo = $self->decode($id_token); | ||
| 121 | |||
| 122 | my $sub = $userinfo->{sub}; | ||
| 123 | my $openid = $userinfo->{openid_id}; | ||
| 124 | |||
| 125 | unless ($sub && $openid) { | ||
| 126 | Catalyst::Exception->throw( | ||
| 127 | 'Could not retrieve sub and openid from token! Is the token | ||
| 128 | correct? Token was ' . $id_token | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | return $realm->find_user($userinfo, $c); | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 retrieve_certs | ||
| 136 | |||
| 137 | Retrieves a pair of JSON-encoded certificates from the given $url (defaults to | ||
| 138 | Google's public cert url), and returns the decoded JSON object. | ||
| 139 | |||
| 140 | If a cache plugin is loaded, the certificate pair is cached; however one of the | ||
| 141 | certificates is expired, a new pair is fetched from $url. | ||
| 142 | |||
| 143 | =head3 ARGUMENTS | ||
| 144 | |||
| 145 | =over | ||
| 146 | |||
| 147 | =item url | ||
| 148 | |||
| 149 | Optional. Location where certificates are located. | ||
| 150 | |||
| 151 | This can also be configured as the | ||
| 152 | 'Authentication::Credential::Google::public_cert_url' key in your catalyst | ||
| 153 | configuration. | ||
| 154 | |||
| 155 | Defaults to https://www.googleapis.com/oauth2/v1/certs. | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =head3 RETURNS | ||
| 160 | |||
| 161 | Decoded JSON object containing certificates. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | sub retrieve_certs { | ||
| 166 | my ($self, $url) = @_; | ||
| 167 | |||
| 168 | my $c = $self->{_app}; | ||
| 169 | my $cached = 0; | ||
| 170 | my $certs; | ||
| 171 | my $cache; | ||
| 172 | |||
| 173 | $url ||= ( $self->{public_cert_url} || 'https://www.googleapis.com/oauth2/v1/certs' ); | ||
| 174 | |||
| 175 | if ( $cache = $self->{cache} ) { | ||
| 176 | if ($certs = $cache->get($self->{cache_key})) { | ||
| 177 | try { | ||
| 178 | $certs = decode_json($certs); | ||
| 179 | } catch { | ||
| 180 | Catalyst::Exception->throw("Could not decode cached JSON of certs: $_!"); | ||
| 181 | }; | ||
| 182 | |||
| 183 | foreach my $key (keys %$certs) { | ||
| 184 | my $cert = $certs->{$key}; | ||
| 185 | my $x509 = Crypt::OpenSSL::X509->new_from_string($cert); | ||
| 186 | |||
| 187 | if ($self->is_cert_expired($x509)) { | ||
| 188 | $cached = 0; | ||
| 189 | last; | ||
| 190 | } else { | ||
| 191 | $cached = 1; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | unless ($cached) { | ||
| 198 | my $certs_encoded = get($url); | ||
| 199 | |||
| 200 | unless ($certs_encoded) { | ||
| 201 | Catalyst::Exception->throw("Could not GET $url! Please check the value of your public_cert_url config!"); | ||
| 202 | } | ||
| 203 | |||
| 204 | try { | ||
| 205 | $certs = decode_json($certs_encoded); | ||
| 206 | } catch { | ||
| 207 | Catalyst::Exception->throw("Could not decode JSON cert content from $url, is URL correct?"); | ||
| 208 | }; | ||
| 209 | |||
| 210 | if ($cache) { | ||
| 211 | $cache->set($self->{cache_key}, $certs_encoded); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | return $certs; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 get_key_from_cert | ||
| 219 | |||
| 220 | Given a pair of certificates $certs (defaults to L</retrieve_certs>), | ||
| 221 | this function returns the public key of the cert identified by $kid. | ||
| 222 | |||
| 223 | =head3 ARGUMENTS | ||
| 224 | |||
| 225 | =over | ||
| 226 | |||
| 227 | =item $kid | ||
| 228 | |||
| 229 | Required. Index of the certificate hash $hash where the cert we want is | ||
| 230 | located. | ||
| 231 | |||
| 232 | =item $certs | ||
| 233 | |||
| 234 | Optional. A (hashref) pair of certificates. | ||
| 235 | It's retrieved using L</retrieve_certs> if not given, | ||
| 236 | or if the pair is expired. | ||
| 237 | |||
| 238 | =item $recursive | ||
| 239 | |||
| 240 | This will be set to true if this function calls itself to renew an expired | ||
| 241 | certificate. Used to prevent infinite recursion. | ||
| 242 | |||
| 243 | =back | ||
| 244 | |||
| 245 | =head3 RETURNS | ||
| 246 | |||
| 247 | Public key of certificate. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub get_key_from_cert { | ||
| 252 | my ($self, $kid, $certs, $recursive) = @_; | ||
| 253 | |||
| 254 | $certs ||= $self->retrieve_certs; | ||
| 255 | my $cert = $certs->{$kid}; | ||
| 256 | my $x509; | ||
| 257 | |||
| 258 | try { | ||
| 259 | $x509 = Crypt::OpenSSL::X509->new_from_string($cert); | ||
| 260 | } catch { | ||
| 261 | Catalyst::Exception->throw("Could not get public key from provided certificate, is the certificate valid?\nCert was:" . $cert ); | ||
| 262 | }; | ||
| 263 | |||
| 264 | if ($self->is_cert_expired($x509)) { | ||
| 265 | unless ($recursive) { | ||
| 266 | # If we ended up here, we were given | ||
| 267 | # an old $certs string from the user. | ||
| 268 | # Let's force getting another. | ||
| 269 | return $self->get_key_from_cert($kid, undef, 1); | ||
| 270 | } else { | ||
| 271 | Catalyst::Exception->throw("Something is very wrong; we were given | ||
| 272 | an expired cert and got another expired cert trying to get a | ||
| 273 | new one! \nCert was:" . $cert); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | return $x509->pubkey; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 is_cert_expired | ||
| 281 | |||
| 282 | Returns if a given L<Crypt::OpenSSL::X509> certificate is expired. | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub is_cert_expired { | ||
| 287 | my ($self, $x509) = @_; | ||
| 288 | |||
| 289 | my $expiry = str2time($x509->notAfter); | ||
| 290 | |||
| 291 | return time > $expiry; | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 decode | ||
| 295 | |||
| 296 | Returns the decoded information contained in a user's token. | ||
| 297 | |||
| 298 | =head3 ARGUMENTS | ||
| 299 | |||
| 300 | =over | ||
| 301 | |||
| 302 | =item $token | ||
| 303 | |||
| 304 | Required. The user's token from Google+. | ||
| 305 | |||
| 306 | =item $certs | ||
| 307 | |||
| 308 | Optional. A pair of public key certs retrieved from Google. | ||
| 309 | If not given, or if the certificates have expired, a new | ||
| 310 | pair of certificates is retrieved. | ||
| 311 | |||
| 312 | =item $pubkey | ||
| 313 | |||
| 314 | Optional. A public key string with which to decode the token. | ||
| 315 | If not given, the public key will be retrieved from $certs. | ||
| 316 | |||
| 317 | =back | ||
| 318 | |||
| 319 | =head2 RETURNS | ||
| 320 | |||
| 321 | Decoded JSON object from the decrypted token. | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub decode { | ||
| 326 | my ($self, $token, $certs, $pubkey) = @_; | ||
| 327 | |||
| 328 | unless ($pubkey) { | ||
| 329 | my $details; | ||
| 330 | |||
| 331 | try { | ||
| 332 | $details = decode_json( | ||
| 333 | MIME::Base64::decode_base64( | ||
| 334 | substr( $token, 0, CORE::index($token, '.') ) | ||
| 335 | ) | ||
| 336 | ); | ||
| 337 | } catch { | ||
| 338 | Catalyst::Exception->throw("Could not decode token, is the token valid? token is " . $token); | ||
| 339 | }; | ||
| 340 | |||
| 341 | my $kid = $details->{kid}; | ||
| 342 | |||
| 343 | unless ($kid) { | ||
| 344 | Catalyst::Exception->throw("Decoded token has no `kid` member; is token valid? token is " . $token); | ||
| 345 | } | ||
| 346 | |||
| 347 | $pubkey = $self->get_key_from_cert($kid, $certs); | ||
| 348 | } | ||
| 349 | |||
| 350 | my $result; | ||
| 351 | |||
| 352 | try { | ||
| 353 | $result = JSON::WebToken->decode($token, $pubkey); | ||
| 354 | } catch { | ||
| 355 | Catalyst::Exception->throw("Could not decode the given token with the public key! Token is " . $token . " and pubkey is " . $pubkey); | ||
| 356 | }; | ||
| 357 | |||
| 358 | return $result; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub get_cache { | ||
| 362 | my ($self, $c) = @_; | ||
| 363 | |||
| 364 | if ($self->{use_context_cache}) { | ||
| 365 | return $c->cache; | ||
| 366 | } else { | ||
| 367 | return $self->{cache}; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | =head1 CAVEATS | ||
| 372 | |||
| 373 | =over | ||
| 374 | |||
| 375 | =item | ||
| 376 | We currently have no automated (unit) tests.. | ||
| 377 | |||
| 378 | =item | ||
| 379 | The code that verifies the key from Google (L</get_key_from_cert> only checks | ||
| 380 | if the key has expired. It does not check that the key is signed by the right | ||
| 381 | people - i.e. a correctly formatted self-signed key would still work... | ||
| 382 | |||
| 383 | =back | ||
| 384 | |||
| 385 | =head1 AUTHOR | ||
| 386 | |||
| 387 | Errietta Kostala <e.kostala@shadowcat.co.uk> | ||
| 388 | |||
| 389 | =head1 CONTRIBUTORS | ||
| 390 | |||
| 391 | =over | ||
| 392 | |||
| 393 | =item | ||
| 394 | Matt S. Trout <mst@shadowcat.co.uk> | ||
| 395 | |||
| 396 | =item | ||
| 397 | Chris Jackson <c.jackson@shadowcat.co.uk> | ||
| 398 | |||
| 399 | =item | ||
| 400 | Jess Robinson <j.robinson@shadowcat.co.uk> | ||
| 401 | |||
| 402 | =back | ||
| 403 | |||
| 404 | =head1 SPONSORS | ||
| 405 | |||
| 406 | =over | ||
| 407 | |||
| 408 | =item | ||
| 409 | Shadowcat Systems LTD. (L<http://shadow.cat>) | ||
| 410 | |||
| 411 | =item | ||
| 412 | Tara L Andrews, Digital Humanities, University of Bern | ||
| 413 | |||
| 414 | =back | ||
| 415 | |||
| 416 | =head1 COPYRIGHT | ||
| 417 | |||
| 418 | Copyright (c) 2015 the Catalyst::Plugin::Authentication::Credential::GooglePlus | ||
| 419 | L</AUTHOR> and L</CONTRIBUTORS> as listed above. | ||
| 420 | |||
| 421 | =head1 LICENSE | ||
| 422 | |||
| 423 | This library is free software and may be distributed under the same terms as | ||
| 424 | perl itself. See L<http://dev.perl.org/licenses/>. | ||
| 425 | |||
| 426 | =cut | ||
| 427 | |||
| 428 | 1; |