1 package stemmaweb::Authentication::Credential::Google;
3 use Crypt::OpenSSL::X509;
8 use LWP::Simple qw(get);
9 use Date::Parse qw(str2time);
17 stemmaweb::Authentication::Google - JSON Web Token handler for Google tokens.
21 Retrieves Google's public certificates, and then retrieves the key from the
22 cert using L<Crypt::OpenSSL::X509>. Finally, uses the pubkey to decrypt a
23 Google token using L<JSON::WebToken>.
28 my ($class, $config, $app, $realm) = @_;
29 $class = ref $class || $class;
41 my ($self, $c, $realm, $authinfo) =@_;
43 my $id_token = $authinfo->{id_token};
44 $id_token ||= $c->req->method eq 'GET' ?
45 $c->req->query_params->{id_token} : $c->req->body_params->{id_token};
48 Catalyst::Exception->throw("id_token not specified.");
51 my $userinfo = $self->decode($id_token);
53 my $sub = $userinfo->{sub};
54 my $openid = $userinfo->{openid_id};
56 if (!$sub || !$openid) {
57 Catalyst::Exception->throw(
58 'Could not retrieve sub and openid from token! Is the token
63 return $realm->find_user($userinfo, $c);
70 Retrieves a pair of JSON-encoded certificates from the given URL (defaults to
71 Google's public cert url), and returns the decoded JSON object.
79 Optional. Location where certificates are located.
80 Defaults to https://www.googleapis.com/oauth2/v1/certs.
86 Decoded JSON object containing certificates.
91 my ($self, $url) = @_;
93 my $c = $self->{_app};
98 $url ||= ( $c->config->{'Authentication::Credential::Google'}->{public_cert_url} || 'https://www.googleapis.com/oauth2/v1/certs' );
100 if ( ($c->registered_plugins('Catalyst::Plugin::Cache')) && ($cache = $c->cache) ) {
101 if ($certs = $cache->get('certs')) {
102 $certs = decode_json($certs);
104 foreach my $key (keys %$certs) {
105 my $cert = $certs->{$key};
106 my $x509 = Crypt::OpenSSL::X509->new_from_string($cert);
108 if ($self->is_cert_expired($x509)) {
119 my $certs_encoded = get($url);
122 $cache->set('certs', $certs_encoded);
125 $certs = decode_json($certs_encoded);
131 =head2 get_key_from_cert
133 Given a pair of certificates $certs (defaults to L</retrieve_certs>),
134 this function returns the public key of the cert identified by $kid.
142 Required. Index of the certificate hash $hash where the cert we want is
147 Optional. A (hashref) pair of certificates.
148 It's retrieved using L</retrieve_certs> if not given,
149 or if the pair is expired.
155 Public key of certificate.
159 sub get_key_from_cert {
160 my ($self, $kid, $certs) = @_;
162 $certs ||= $self->retrieve_certs;
163 my $cert = $certs->{$kid};
164 my $x509 = Crypt::OpenSSL::X509->new_from_string($cert);
166 if ($self->is_cert_expired($x509)) {
167 # If we ended up here, we were given
168 # an old $certs string from the user.
169 # Let's force getting another.
170 return $self->get_key_from_cert($kid);
173 return $x509->pubkey;
176 =head2 is_cert_expired
178 Returns if a given L<Crypt::OpenSSL::X509> certificate is expired.
182 sub is_cert_expired {
183 my ($self, $x509) = @_;
185 my $expiry = str2time($x509->notAfter);
187 return time > $expiry;
192 Returns the decoded information contained in a user's token.
200 Required. The user's token from Google+.
204 Optional. A public key string with which to decode the token.
205 If not given, the public key will be retrieved from $certs.
209 Optional. A pair of public key certs retrieved from Google.
210 If not given, or if the certificates have expired, a new
211 pair of certificates is retrieved.
217 Decoded JSON object from the decrypted token.
222 my ($self, $token, $certs, $pubkey) = @_;
225 my $details = decode_json(
226 MIME::Base64::decode_base64(
227 substr( $token, 0, CORE::index($token, '.') )
231 my $kid = $details->{kid};
232 $pubkey = $self->get_key_from_cert($kid, $certs);
235 return JSON::WebToken->decode($token, $pubkey);
240 Errietta Kostala <e.kostala@shadowcat.co.uk>
244 This library is free software. You can redistribute it and/or modify
245 it under the same terms as Perl itself.