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') // ''; |
51 |
my $restricttodomain = C4::Context->preference('GoogleOAuth2Domain') // ''; |
52 |
|
53 |
# protocol is assumed in OPACBaseURL see bug 5010. |
54 |
my $redirecturl = $host . '/cgi-bin/koha/svc/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 = new CGI; |
60 |
|
61 |
sub loginfailed { |
62 |
my $query = shift; |
63 |
my $reason = shift; |
64 |
$query->delete('code'); |
65 |
$query->param( 'OAuth2Failed' => $reason ); |
66 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
67 |
{ |
68 |
template_name => 'opac-user.tt', |
69 |
query => $query, |
70 |
type => 'opac', |
71 |
authnotrequired => 0, |
72 |
flagsrequired => { borrow => 1 }, |
73 |
} |
74 |
); |
75 |
$template->param( 'invalidOAuth2Login' => $reason ); |
76 |
$template->param( 'loginprompt' => 1 ); |
77 |
output_html_with_http_headers $query, $cookie, $template->output; |
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, $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 ' |
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 = ''; |
152 |
$prompt = $query->param('reauthenticate') |
153 |
unless not( defined $query->param('reauthenticate') ); |
154 |
|
155 |
my $authorisationurl = |
156 |
'https://accounts.google.com/o/oauth2/auth?' |
157 |
. 'response_type=code&' |
158 |
. 'redirect_uri=' |
159 |
. escape($redirecturl) . '&' |
160 |
. 'client_id=' |
161 |
. escape($clientid) . '&' |
162 |
. 'scope=' |
163 |
. escape($scope) . '&'; |
164 |
if ( $query->param('reauthenticate') ) { |
165 |
$authorisationurl .= |
166 |
'prompt=' . escape( $query->param('reauthenticate') ); |
167 |
} |
168 |
print $query->redirect($authorisationurl); |
169 |
} |