From 95433d80b32a818eae4e26c2814d7b9f46ad37cf Mon Sep 17 00:00:00 2001
From: Nicholas van Oudtshoorn
Date: Thu, 10 Oct 2013 17:04:46 +0800
Subject: [PATCH] Bug 10988 [ENH] Allow login via Google OAuth2
Content-Type: text/plain; charset="utf-8"
This patch fixes a minor typo in the previous one regarding the oauth2 redirect uri
Adds googleoauth2 login to koha. Adds applicable system preferences Modifies templates to support oauth2 login failures changes the login links to use googleoauth2 when oauth2 is enabled.
Test Plan:
1) Apply this patch
2) Update the database (this is the bit I'm not sure about - I've added the SQL into the atomicupdates directory - is this right?)
3) Create a web app in the google cloud console:
- Go to https://cloud.google.com/console
- Create a project, and give it some details
- Open the Project by clicking on it
- Under APIs & auth menu, open "Registered Apps" and click "Register App"
- Give it a name and make sure you select "Web app", click ok
- Under OAuth 2.0 Client ID,
- under web origin, type
- in the redirect uri enter /cgi-bin/koha/svc/googleoauth2
- click Generate
4) Set the GoogleOAuth2ClientID and GoogleOAuth2ClientSecret according to the values generated above
5) Test that clicking login intiates an oauth2 login
6) Try logging in with an email that is not registered to a library user. It should fail and offer either to retry or to login manually.
7) Try logging in with an email that is registered to a library user. You should be logged in.
---
C4/Auth.pm | 10 +-
.../atomicupdate/bug_10988_add_GoogleOAuth.sql | 2 +
installer/data/mysql/sysprefs.sql | 3 +
.../prog/en/modules/admin/preferences/admin.pref | 15 +++
koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc | 6 +-
koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc | 6 +-
koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt | 9 ++
opac/svc/googleoauth2 | 126 +++++++++++++++++++++
8 files changed, 174 insertions(+), 3 deletions(-)
create mode 100644 installer/data/mysql/atomicupdate/bug_10988_add_GoogleOAuth.sql
create mode 100755 opac/svc/googleoauth2
diff --git a/C4/Auth.pm b/C4/Auth.pm
index d73edaf..1dc0bf1 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -34,7 +34,7 @@ use POSIX qw/strftime/;
use List::MoreUtils qw/ any /;
# use utf8;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $ldap $cas $caslogout);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $googleoauth2 $ldap $cas $caslogout);
BEGIN {
sub psgi_env { any { /^psgi\./ } keys %ENV }
@@ -52,6 +52,7 @@ BEGIN {
ParseSearchHistoryCookie
);
%EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
+ $googleoauth2= C4::Context->preference('GoogleOAuth2');
$ldap = C4::Context->config('useldapserver') || 0;
$cas = C4::Context->preference('casAuthentication');
$caslogout = C4::Context->preference('casLogout');
@@ -394,6 +395,7 @@ sub get_template_and_user {
BranchesLoop => GetBranchesLoop($opac_name),
BranchCategoriesLoop => GetBranchCategories( 'searchdomain', 1, $opac_name ),
CalendarFirstDayOfWeek => (C4::Context->preference("CalendarFirstDayOfWeek") eq "Sunday")?0:1,
+ GoogleOAuth2 => (C4::Context->preference("GoogleOAuth2")),
LibraryName => "" . C4::Context->preference("LibraryName"),
LibraryNameTitle => "" . $LibraryNameTitle,
LoginBranchname => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"",
@@ -1078,6 +1080,12 @@ sub checkauth {
invalidCasLogin => $info{'invalidCasLogin'}
);
}
+ if ($googleoauth2) {
+ if ($query->param("OAuth2Failed")) {
+ my $reason = $query->param('OAuth2Failed');
+ $template->param(invalidOAuth2Login => $reason);
+ }
+ }
my $self_url = $query->url( -absolute => 1 );
$template->param(
diff --git a/installer/data/mysql/atomicupdate/bug_10988_add_GoogleOAuth.sql b/installer/data/mysql/atomicupdate/bug_10988_add_GoogleOAuth.sql
new file mode 100644
index 0000000..219d472
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_10988_add_GoogleOAuth.sql
@@ -0,0 +1,2 @@
+-- Adding GoogleOAuth2 support
+INSERT INTO `systempreferences` (`variable`, `value`, `options`, `explanation`, `type`) VALUES ('GoogleOAuth2', '0', NULL, 'if ON, allows the use of Google OAuth2 for login', 'YesNo'), ('GoogleOAuth2ClientID', '', NULL, 'Client ID for the web app registered with google', 'Free'), ('GoogleOAuth2ClientSecret', '', NULL, 'Client Secret for the web app registered with google', 'Free');
diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql
index 4b0f364..3445025 100644
--- a/installer/data/mysql/sysprefs.sql
+++ b/installer/data/mysql/sysprefs.sql
@@ -115,6 +115,9 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
('FRBRizeEditions','0','','If ON, Koha will query one or more ISBN web services for associated ISBNs and display an Editions tab on the details pages','YesNo'),
('gist','0','','Default Goods and Services tax rate NOT in %, but in numeric form (0.12 for 12%), set to 0 to disable GST','Integer'),
('GoogleJackets','0',NULL,'if ON, displays jacket covers from Google Books API','YesNo'),
+('GoogleOAuth2', '0', NULL, 'if ON, allows the use of Google OAuth2 for login', 'YesNo'),
+('GoogleOAuth2ClientID', '', NULL, 'Client ID for the web app registered with google', 'Free'),
+('GoogleOAuth2ClientSecret', '', NULL, 'Client Secret for the web app registered with google', 'Free'),
('hidelostitems','0','','If ON, disables display of\"lost\" items in OPAC.','YesNo'),
('HidePatronName','0','','If this is switched on, patron\'s cardnumber will be shown instead of their name on the holds and catalog screens','YesNo'),
('hide_marc','0',NULL,'If ON, disables display of MARC fields, subfield codes & indicators (still shows data)','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref
index 1215ac1..14b53e9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref
@@ -113,3 +113,18 @@ Administration:
Solr: Solr
Zebra: Zebra
- is the search engine used.
+ Google OAuth2:
+ -
+ - pref: GoogleOAuth2
+ choices:
+ yes: Use
+ no: "Don't Use"
+ - Google OAuth2 login.
+ - You will need to select OAuth2 when creating an app in the google cloud console, and set the web origin to your_opac_url and the redirect url to your_opac_url/cgi-bin/koha/svc/oauthlogin .
+
+ -
+ - Google OAuth2 Client ID
+ - pref: GoogleOAuth2ClientID
+ -
+ - Google OAuth2 Client Secret
+ - pref: GoogleOAuth2ClientSecret
diff --git a/koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc b/koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc
index 42d9576..f832cc4 100644
--- a/koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc
+++ b/koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc
@@ -55,7 +55,11 @@
[% IF ( opacuserlogin ) %]
[% END %]
+[% IF ( invalidOAuth2Login ) %]
+Automatic Login
+Sorry, your automatic login failed. [% invalidOAuth2Login %]
+Please note that automatic login will only work if you are using the email address registered with this library.
+If you want to, you can try to login using a differen account
+
Local login
+If you can't login automatically, you can still login in manually:
+
+[% END %]