summaryrefslogtreecommitdiffstats
path: root/auth.cgi
diff options
context:
space:
mode:
authorDylan William Hardison <dylan@mozilla.com>2015-06-02 03:08:19 +0200
committerDylan William Hardison <dylan@hardison.net>2015-06-02 03:08:19 +0200
commit3cf3faf600249981e3903978b1501fffaabf7e0f (patch)
tree2f7c8a127f869d3b5d1f9011751c47e3ce334483 /auth.cgi
parentf2c52dff2711d6b61d7879f5f9384390873f52cc (diff)
downloadbugzilla-3cf3faf600249981e3903978b1501fffaabf7e0f.tar.gz
bugzilla-3cf3faf600249981e3903978b1501fffaabf7e0f.tar.xz
Bug 1163760: Backport upstream bug 1144468 to bmo to add authentication delegation
Diffstat (limited to 'auth.cgi')
-rwxr-xr-xauth.cgi88
1 files changed, 88 insertions, 0 deletions
diff --git a/auth.cgi b/auth.cgi
new file mode 100755
index 000000000..ad9017048
--- /dev/null
+++ b/auth.cgi
@@ -0,0 +1,88 @@
+#!/usr/bin/perl -wT
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+use 5.10.1;
+use strict;
+use warnings;
+
+use lib qw(. lib);
+
+use Bugzilla;
+use Bugzilla::Constants;
+use Bugzilla::Error;
+use Bugzilla::Hook;
+use Bugzilla::Util qw(trick_taint);
+use Bugzilla::Token qw(issue_auth_delegation_token check_auth_delegation_token);
+use Bugzilla::Mailer qw(MessageToMTA);
+
+use URI;
+use URI::QueryParam;
+
+Bugzilla->login(LOGIN_REQUIRED);
+
+ThrowUserError('auth_delegation_disabled') unless Bugzilla->params->{auth_delegation};
+
+my $cgi = Bugzilla->cgi;
+my $template = Bugzilla->template;
+my $user = Bugzilla->user;
+my $callback = $cgi->param('callback') or ThrowUserError("auth_delegation_missing_callback");
+my $description = $cgi->param('description') or ThrowUserError("auth_delegation_missing_description");
+
+trick_taint($callback);
+trick_taint($description);
+
+my $callback_uri = URI->new($callback);
+my $callback_base = $callback_uri->clone;
+$callback_base->query(undef);
+
+my $skip_confirmation = 0;
+my %args = ( skip_confirmation => \$skip_confirmation,
+ callback => $callback_uri,
+ description => $description,
+ callback_base => $callback_base );
+
+Bugzilla::Hook::process('auth_delegation_confirm', \%args);
+
+my $confirmed = lc($cgi->request_method) eq 'post' && $cgi->param('confirm');
+
+if ($confirmed || $skip_confirmation) {
+ my $token = $cgi->param('token');
+ unless ($skip_confirmation) {
+ ThrowUserError("auth_delegation_missing_token") unless $token;
+ trick_taint($token);
+
+ unless (check_auth_delegation_token($token, $callback)) {
+ ThrowUserError('auth_delegation_invalid_token',
+ { token => $token, callback => $callback });
+ }
+ }
+
+ my $new_key = Bugzilla::User::APIKey->create({
+ user_id => $user->id,
+ description => $description,
+ });
+ my $template = Bugzilla->template_inner($user->setting('lang'));
+ my $vars = { user => $user, new_key => $new_key };
+ my $message;
+ $template->process('email/new-api-key.txt.tmpl', $vars, \$message)
+ or ThrowTemplateError($template->error());
+
+ MessageToMTA($message);
+
+ $callback_uri->query_param(client_api_key => $new_key->api_key);
+ $callback_uri->query_param(client_api_login => $user->login);
+
+ print $cgi->redirect($callback_uri);
+}
+else {
+ $args{token} = issue_auth_delegation_token($callback);
+
+ print $cgi->header();
+ $template->process("account/auth/delegation.html.tmpl", \%args)
+ or ThrowTemplateError($template->error());
+}