From 49495810ea07c10504c894fbf979568af7363f14 Mon Sep 17 00:00:00 2001 From: Gervase Markham Date: Wed, 4 Apr 2012 13:00:01 -0400 Subject: Bug 672841 - Make BMO support BrowserID r=dkl --- extensions/BrowserID/Config.pm | 43 ++++++++ extensions/BrowserID/Extension.pm | 52 +++++++++ extensions/BrowserID/TODO | 19 ++++ extensions/BrowserID/lib/Login.pm | 122 +++++++++++++++++++++ .../auth/login-additional_methods.html.tmpl | 23 ++++ .../auth/login-small-additional_methods.html.tmpl | 20 ++++ 6 files changed, 279 insertions(+) create mode 100644 extensions/BrowserID/Config.pm create mode 100644 extensions/BrowserID/Extension.pm create mode 100644 extensions/BrowserID/TODO create mode 100644 extensions/BrowserID/lib/Login.pm create mode 100644 extensions/BrowserID/template/en/default/hook/account/auth/login-additional_methods.html.tmpl create mode 100644 extensions/BrowserID/template/en/default/hook/account/auth/login-small-additional_methods.html.tmpl (limited to 'extensions/BrowserID') diff --git a/extensions/BrowserID/Config.pm b/extensions/BrowserID/Config.pm new file mode 100644 index 000000000..a55ea8ff0 --- /dev/null +++ b/extensions/BrowserID/Config.pm @@ -0,0 +1,43 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the BrowserID Bugzilla Extension. +# +# The Initial Developer of the Original Code is the Mozilla Foundation. +# Portions created by the Initial Developer are Copyright (C) 2011 the +# Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Gervase Markham + +package Bugzilla::Extension::BrowserID; +use strict; + +use constant NAME => 'BrowserID'; + +use constant REQUIRED_MODULES => [ + { + package => 'JSON', + module => 'JSON', + version => 0, + }, + { + package => 'libwww-perl', + module => 'LWP::UserAgent', + version => 0, + }, +]; + +use constant OPTIONAL_MODULES => [ +]; + +__PACKAGE__->NAME; diff --git a/extensions/BrowserID/Extension.pm b/extensions/BrowserID/Extension.pm new file mode 100644 index 000000000..b132ea503 --- /dev/null +++ b/extensions/BrowserID/Extension.pm @@ -0,0 +1,52 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the BrowserID Bugzilla Extension. +# +# The Initial Developer of the Original Code is the Mozilla Foundation. +# Portions created by the Initial Developer are Copyright (C) 2011 the +# Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Gervase Markham + +package Bugzilla::Extension::BrowserID; +use strict; +use base qw(Bugzilla::Extension); + +our $VERSION = '0.01'; + +sub auth_login_methods { + my ($self, $args) = @_; + my $modules = $args->{'modules'}; + if (exists($modules->{'BrowserID'})) { + $modules->{'BrowserID'} = 'Bugzilla/Extension/BrowserID/Login.pm'; + } +} + +sub config_modify_panels { + my ($self, $args) = @_; + my $panels = $args->{'panels'}; + my $auth_panel_params = $panels->{'auth'}->{'params'}; + + my ($user_info_class) = + grep { $_->{'name'} eq 'user_info_class' } @$auth_panel_params; + + if ($user_info_class) { + # XXX Bugzilla::Auth::Login::Stack doesn't let a hard failure stop the + # login process :-(( We put it in both ways round for now, for testing. + push(@{ $user_info_class->{'choices'} }, "CGI,BrowserID"); + push(@{ $user_info_class->{'choices'} }, "BrowserID,CGI"); + } +} + +__PACKAGE__->NAME; diff --git a/extensions/BrowserID/TODO b/extensions/BrowserID/TODO new file mode 100644 index 000000000..ac94a3c42 --- /dev/null +++ b/extensions/BrowserID/TODO @@ -0,0 +1,19 @@ +ToDo: + +* Cache the LWP::UserAgent in Login.pm? + +* Fix Bugzilla::Auth::Login::Stack to allow failure part way down the chain + (currently, it seems that both CGI and BrowserID have to be last in order + to report login failures correctly.) + +* JS inclusions noticeably slow page load. Do we want a local copy of + browserid.js? Do the browserid folks object to that? How can we get good + performance? How can we avoid including it in every logged-in page? Can we + do demand loading onclick, and/or load-on-reveal? + +* Fix -8px margin-bottom hack in login-small-additional_methods.html.tmpl + + + + + diff --git a/extensions/BrowserID/lib/Login.pm b/extensions/BrowserID/lib/Login.pm new file mode 100644 index 000000000..eedc85d09 --- /dev/null +++ b/extensions/BrowserID/lib/Login.pm @@ -0,0 +1,122 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the BrowserID Bugzilla Extension. +# +# The Initial Developer of the Original Code is the Mozilla Foundation. +# Portions created by the Initial Developer are Copyright (C) 2011 the +# Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Gervase Markham + +package Bugzilla::Extension::BrowserID::Login; +use strict; +use base qw(Bugzilla::Auth::Login); + +use Bugzilla::Constants; +use Bugzilla::Util; + +use JSON; +use LWP::UserAgent; + +use constant requires_verification => 0; +use constant is_automatic => 1; + +sub get_login_info { + my ($self) = @_; + + my $cgi = Bugzilla->cgi; + my $assertion = $cgi->param("browserid_assertion"); + # Avoid the assertion being copied into any 'echoes' of the current URL + # in the page. + $cgi->delete('browserid_assertion'); + + if (!$assertion) { + return { failure => AUTH_NODATA }; + } + + my $urlbase = new URI(correct_urlbase()); + my $audience = $urlbase->scheme . "://" . $urlbase->host_port; + + my $ua = new LWP::UserAgent(); + + my $info = { 'status' => 'browserid-server-broken' }; + eval { + my $response = $ua->post("https://browserid.org/verify", + [assertion => $assertion, + audience => $audience]); + + $info = decode_json($response->content()); + }; + + # XXX Add 120 secs because 'expires' is currently broken in deployed + # BrowserID server - it returns exact current time, so is immediately + # expired! This should be fixed soon. + if ($info->{'status'} eq "okay" && + $info->{'audience'} eq $audience && + (($info->{'expires'} / 1000) + 120) > time()) + { + my $login_data = { + 'username' => $info->{'email'} + }; + + my $result = + Bugzilla::Auth::Verify->create_or_update_user($login_data); + return $result if $result->{'failure'}; + + my $user = $result->{'user'}; + + # BrowserID logins are currently restricted to less powerful accounts - + # the most you can have is 'editbugs'. This is while the technology + # is maturing. So we need to check that the user doesn't have 'too + # many permissions' to log in this way. + # + # If a newly-created account has too many permissions, this code will + # create an account for them and then fail their login. Which isn't + # great, but they can still use normal-Bugzilla-login password + # recovery. + my @safe_groups = ('everyone', 'canconfirm', 'editbugs'); + foreach my $group (@{ $user->groups() }) { + if (!grep { $group->name eq $_ } @safe_groups) { + return { failure => AUTH_LOGINFAILED }; + } + } + + $login_data->{'user'} = $user; + $login_data->{'user_id'} = $user->id; + + return $login_data; + } + else { + return { failure => AUTH_LOGINFAILED }; + } +} + +# Pinched from Bugzilla::Auth::Login::CGI +sub fail_nodata { + my ($self) = @_; + my $cgi = Bugzilla->cgi; + my $template = Bugzilla->template; + + if (Bugzilla->usage_mode != USAGE_MODE_BROWSER) { + ThrowUserError('login_required'); + } + + print $cgi->header(); + $template->process("account/auth/login.html.tmpl", + { 'target' => $cgi->url(-relative=>1) }) + || ThrowTemplateError($template->error()); + exit; +} + +1; diff --git a/extensions/BrowserID/template/en/default/hook/account/auth/login-additional_methods.html.tmpl b/extensions/BrowserID/template/en/default/hook/account/auth/login-additional_methods.html.tmpl new file mode 100644 index 000000000..4d8d8d3f0 --- /dev/null +++ b/extensions/BrowserID/template/en/default/hook/account/auth/login-additional_methods.html.tmpl @@ -0,0 +1,23 @@ +[% IF Param('user_info_class').split(',').contains('BrowserID') %] + + + + +

+Or, you could login using BrowserID: + +

+[% END %] \ No newline at end of file diff --git a/extensions/BrowserID/template/en/default/hook/account/auth/login-small-additional_methods.html.tmpl b/extensions/BrowserID/template/en/default/hook/account/auth/login-small-additional_methods.html.tmpl new file mode 100644 index 000000000..12bff015b --- /dev/null +++ b/extensions/BrowserID/template/en/default/hook/account/auth/login-small-additional_methods.html.tmpl @@ -0,0 +1,20 @@ +[% IF Param('user_info_class').split(',').contains('BrowserID') %] + + + + + or +[% END %] \ No newline at end of file -- cgit v1.2.3-24-g4f1b