|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# Copyright vanoudt@gmail.com 2014 |
| 3 |
# Based on persona code from chris@bigballofwax.co.nz 2013 |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
# |
| 20 |
# |
| 21 |
# Basic OAuth2 authentication for google goes like this First: |
| 22 |
# get your clientid, clientsecret from google. At this stage, tell |
| 23 |
# google that your redirect url is /cgi-bin/koha/svc/oauthlogin |
| 24 |
# |
| 25 |
# The first thing that happens when this script is called is |
| 26 |
# that one gets redirected to an authentication url from google |
| 27 |
# |
| 28 |
# If successful, that then redirects back to this script, setting |
| 29 |
# a CODE parameter which we use to look up a json authentication |
| 30 |
# token. This token includes an encrypted json id_token, which we |
| 31 |
# round-trip back to google to decrypt. Finally, we can extract |
| 32 |
# the email address from this. |
| 33 |
# |
| 34 |
# There is some room for improvement here. In particular, Google |
| 35 |
# recommends verifying and decrypting the id_token locally, which |
| 36 |
# means caching some information and updating it daily. But that |
| 37 |
# would make things a lot faster |
| 38 |
|
| 39 |
use Modern::Perl; |
| 40 |
use CGI qw ( -utf8 escape ); |
| 41 |
use C4::Auth; |
| 42 |
use C4::Context; |
| 43 |
use C4::Output; |
| 44 |
|
| 45 |
use LWP::UserAgent; |
| 46 |
use HTTP::Request::Common qw{ POST }; |
| 47 |
use JSON; |
| 48 |
|
| 49 |
my $scope = 'openid email'; |
| 50 |
my $host = C4::Context->preference('OPACBaseURL') // q{}; |
| 51 |
my $restricttodomain = C4::Context->preference('GoogleOAuth2Domain') // q{}; |
| 52 |
|
| 53 |
# protocol is assumed in OPACBaseURL see bug 5010. |
| 54 |
my $redirecturl = $host . '/cgi-bin/koha/svc/auth/googleoauth2'; |
| 55 |
my $issuer = 'accounts.google.com'; |
| 56 |
my $clientid = C4::Context->preference('GoogleOAuth2ClientID'); |
| 57 |
my $clientsecret = C4::Context->preference('GoogleOAuth2ClientSecret'); |
| 58 |
|
| 59 |
my $query = CGI->new; |
| 60 |
|
| 61 |
sub loginfailed { |
| 62 |
my $cgi_query = shift; |
| 63 |
my $reason = shift; |
| 64 |
$cgi_query->delete('code'); |
| 65 |
$cgi_query->param( 'OAuth2Failed' => $reason ); |
| 66 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
| 67 |
{ |
| 68 |
template_name => 'opac-user.tt', |
| 69 |
query => $cgi_query, |
| 70 |
type => 'opac', |
| 71 |
authnotrequired => 0, |
| 72 |
} |
| 73 |
); |
| 74 |
$template->param( 'invalidOAuth2Login' => $reason ); |
| 75 |
$template->param( 'loginprompt' => 1 ); |
| 76 |
output_html_with_http_headers $cgi_query, $cookie, $template->output; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( defined $query->param('error') ) { |
| 81 |
loginfailed( $query, |
| 82 |
'An authentication error occurred. (Error:' |
| 83 |
. $query->param('error') |
| 84 |
. ')' ); |
| 85 |
} |
| 86 |
elsif ( defined $query->param('code') ) { |
| 87 |
my $code = $query->param('code'); |
| 88 |
my $ua = LWP::UserAgent->new(); |
| 89 |
my $request = POST( |
| 90 |
'https://accounts.google.com/o/oauth2/token', |
| 91 |
[ |
| 92 |
code => $code, |
| 93 |
client_id => $clientid, |
| 94 |
client_secret => $clientsecret, |
| 95 |
redirect_uri => $redirecturl, |
| 96 |
grant_type => 'authorization_code', |
| 97 |
$scope => $scope |
| 98 |
] |
| 99 |
); |
| 100 |
my $response = $ua->request($request)->decoded_content; |
| 101 |
my $json = decode_json($response); |
| 102 |
if ( exists( $json->{'id_token'} ) ) { |
| 103 |
$request = POST( 'https://www.googleapis.com/oauth2/v1/tokeninfo', |
| 104 |
[ id_token => $json->{'id_token'} ] ); |
| 105 |
$response = $ua->request($request)->decoded_content; |
| 106 |
$json = decode_json($response); |
| 107 |
|
| 108 |
# Confirm (as google suggests) that the issuer and audience are what we expect them to be |
| 109 |
if ( ( $json->{'issuer'} eq $issuer ) |
| 110 |
&& ( $json->{'audience'} eq $clientid ) |
| 111 |
&& exists( $json->{'email'} ) ) |
| 112 |
{ |
| 113 |
my $email = $json->{'email'}; |
| 114 |
my ( $userid, $cookie, $session_id ) = |
| 115 |
checkauth( $query, 1, { }, 'opac', $email ); |
| 116 |
if ($userid) { # A valid user has logged in |
| 117 |
if ( ( $restricttodomain ne q{} ) |
| 118 |
&& ( index( $email, $restricttodomain ) < 0 ) ) |
| 119 |
{ |
| 120 |
loginfailed( $query, |
| 121 |
'The email you have used is not valid for this library. Email addresses should conclude with ' |
| 122 |
. $restricttodomain |
| 123 |
. ' .' ); |
| 124 |
} |
| 125 |
else { |
| 126 |
print $query->redirect( |
| 127 |
-uri => '/cgi-bin/koha/opac-user.pl', |
| 128 |
-cookie => $cookie |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
else { |
| 133 |
loginfailed( $query, |
| 134 |
'The email address you are trying to use is not associated with a borrower in this library.' |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
else { # something went wrong with getting appropriate credentials |
| 139 |
loginfailed( $query, |
| 140 |
'Failed to get proper credentials from google.' ); |
| 141 |
} |
| 142 |
} |
| 143 |
else { # Failed to get ID Token |
| 144 |
loginfailed( $query, |
| 145 |
'An authentication error occurred. (Error: No ID Token was supplied)' |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
else { |
| 151 |
my $prompt = $query->param('reauthenticate') // q{}; |
| 152 |
|
| 153 |
my $authorisationurl = |
| 154 |
'https://accounts.google.com/o/oauth2/auth?' |
| 155 |
. 'response_type=code&' |
| 156 |
. 'redirect_uri=' |
| 157 |
. escape($redirecturl) . q{&} |
| 158 |
. 'client_id=' |
| 159 |
. escape($clientid) . q{&} |
| 160 |
. 'scope=' |
| 161 |
. escape($scope) . q{&}; |
| 162 |
if ( $prompt || ( defined $prompt && length $prompt > 0 ) ) { |
| 163 |
$authorisationurl .= 'prompt=' . escape($prompt); |
| 164 |
} |
| 165 |
print $query->redirect($authorisationurl); |
| 166 |
} |