Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl -w |
|
|
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/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') // ''; |
51 |
my $restricttodomain = C4::Context->preference('GoogleOAuth2Domain') // ''; |
52 |
# protocol is assumed in OPACBaseURL see bug 5010. |
53 |
my $redirecturl = $host . '/cgi-bin/koha/svc/googleoauth2'; |
54 |
my $issuer = 'accounts.google.com'; |
55 |
my $clientid = C4::Context->preference('GoogleOAuth2ClientID'); |
56 |
my $clientsecret = C4::Context->preference('GoogleOAuth2ClientSecret'); |
57 |
|
58 |
my $query = new CGI; |
59 |
|
60 |
sub loginfailed { |
61 |
my $query = shift; |
62 |
my $reason = shift; |
63 |
$query->delete('code'); |
64 |
$query->param( 'OAuth2Failed' => $reason ); |
65 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
66 |
{ |
67 |
template_name => 'opac-user.tmpl', |
68 |
query => $query, |
69 |
type => 'opac', |
70 |
authnotrequired => 0, |
71 |
flagsrequired => { borrow => 1 }, |
72 |
} |
73 |
); |
74 |
$template->param( 'invalidOAuth2Login' => $reason ); |
75 |
$template->param( 'loginprompt' => 1 ); |
76 |
output_html_with_http_headers $query, $cookie, $template->output; |
77 |
} |
78 |
|
79 |
#die $query->param('code'); |
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, $sessionID ) = |
115 |
checkauth( $query, 1, { borrow => 1 }, 'opac', $email ); |
116 |
if ($userid) { # A valid user has logged in |
117 |
if ( ($restricttodomain ne '' ) |
118 |
&& (index($email, $restricttodomain) == -1) ) |
119 |
{ |
120 |
loginfailed( $query, |
121 |
'The email you have used is not valid for this library. Email addresses should conclude with ' . $restricttodomain . ' .' |
122 |
); |
123 |
} |
124 |
else { |
125 |
print $query->redirect( |
126 |
-uri => '/cgi-bin/koha/opac-user.pl', |
127 |
-cookie => $cookie |
128 |
); |
129 |
} |
130 |
} |
131 |
else { |
132 |
loginfailed( $query, |
133 |
'The email address you are trying to use is not associated with a borrower in this library.' |
134 |
); |
135 |
} |
136 |
} |
137 |
else { # something went wrong with getting appropriate credentials |
138 |
loginfailed( $query, |
139 |
'Failed to get proper credentials from google.' ); |
140 |
} |
141 |
} |
142 |
else { # Failed to get ID Token |
143 |
loginfailed( $query, |
144 |
'An authentication error occurred. (Error: No ID Token was supplied)' |
145 |
); |
146 |
} |
147 |
|
148 |
} |
149 |
else { |
150 |
my $prompt = ''; |
151 |
$prompt = $query->param('reauthenticate') |
152 |
unless not( defined $query->param('reauthenticate') ); |
153 |
|
154 |
my $authorisationurl = |
155 |
'https://accounts.google.com/o/oauth2/auth?' |
156 |
. 'response_type=code&' |
157 |
. 'redirect_uri=' |
158 |
. escape($redirecturl) . '&' |
159 |
. 'client_id=' |
160 |
. escape($clientid) . '&' |
161 |
. 'scope=' |
162 |
. escape($scope) . '&'; |
163 |
if ( $query->param('reauthenticate') ) { |
164 |
$authorisationurl .= 'prompt=' . escape( $query->param('reauthenticate') ); |
165 |
} |
166 |
print $query->redirect($authorisationurl); |
167 |
} |