|
| 1 | +#!/usr/bin/perl -wT |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +# |
| 6 | +# This Source Code Form is "Incompatible With Secondary Licenses", as |
| 7 | +# defined by the Mozilla Public License, v. 2.0. |
| 8 | + |
| 9 | +use 5.10.1; |
| 10 | +use strict; |
| 11 | +use warnings; |
| 12 | + |
| 13 | +use lib qw(. lib); |
| 14 | + |
| 15 | +use Bugzilla; |
| 16 | +use Bugzilla::Constants; |
| 17 | +use Bugzilla::Error; |
| 18 | +use Bugzilla::Hook; |
| 19 | +use Bugzilla::Util qw(trick_taint); |
| 20 | +use Bugzilla::Token qw(issue_auth_delegation_token check_auth_delegation_token); |
| 21 | +use Bugzilla::Mailer qw(MessageToMTA); |
| 22 | + |
| 23 | +use URI; |
| 24 | +use URI::QueryParam; |
| 25 | + |
| 26 | +Bugzilla->login(LOGIN_REQUIRED); |
| 27 | + |
| 28 | +ThrowUserError('auth_delegation_disabled') unless Bugzilla->params->{auth_delegation}; |
| 29 | + |
| 30 | +my $cgi = Bugzilla->cgi; |
| 31 | +my $template = Bugzilla->template; |
| 32 | +my $user = Bugzilla->user; |
| 33 | +my $callback = $cgi->param('callback') or ThrowUserError("auth_delegation_missing_callback"); |
| 34 | +my $description = $cgi->param('description') or ThrowUserError("auth_delegation_missing_description"); |
| 35 | + |
| 36 | +trick_taint($callback); |
| 37 | +trick_taint($description); |
| 38 | + |
| 39 | +my $callback_uri = URI->new($callback); |
| 40 | +my $callback_base = $callback_uri->clone; |
| 41 | +$callback_base->query(undef); |
| 42 | + |
| 43 | +my $skip_confirmation = 0; |
| 44 | +my %args = ( skip_confirmation => \$skip_confirmation, |
| 45 | + callback => $callback_uri, |
| 46 | + description => $description, |
| 47 | + callback_base => $callback_base ); |
| 48 | + |
| 49 | +Bugzilla::Hook::process('auth_delegation_confirm', \%args); |
| 50 | + |
| 51 | +my $confirmed = lc($cgi->request_method) eq 'post' && $cgi->param('confirm'); |
| 52 | + |
| 53 | +if ($confirmed || $skip_confirmation) { |
| 54 | + my $token = $cgi->param('token'); |
| 55 | + unless ($skip_confirmation) { |
| 56 | + ThrowUserError("auth_delegation_missing_token") unless $token; |
| 57 | + trick_taint($token); |
| 58 | + |
| 59 | + unless (check_auth_delegation_token($token, $callback)) { |
| 60 | + ThrowUserError('auth_delegation_invalid_token', |
| 61 | + { token => $token, callback => $callback }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + my $new_key = Bugzilla::User::APIKey->create({ |
| 66 | + user_id => $user->id, |
| 67 | + description => $description, |
| 68 | + }); |
| 69 | + my $template = Bugzilla->template_inner($user->setting('lang')); |
| 70 | + my $vars = { user => $user, new_key => $new_key }; |
| 71 | + my $message; |
| 72 | + $template->process('email/new-api-key.txt.tmpl', $vars, \$message) |
| 73 | + or ThrowTemplateError($template->error()); |
| 74 | + |
| 75 | + MessageToMTA($message); |
| 76 | + |
| 77 | + $callback_uri->query_param(client_api_key => $new_key->api_key); |
| 78 | + $callback_uri->query_param(client_api_login => $user->login); |
| 79 | + |
| 80 | + print $cgi->redirect($callback_uri); |
| 81 | +} |
| 82 | +else { |
| 83 | + $args{token} = issue_auth_delegation_token($callback); |
| 84 | + |
| 85 | + print $cgi->header(); |
| 86 | + $template->process("account/auth/delegation.html.tmpl", \%args) |
| 87 | + or ThrowTemplateError($template->error()); |
| 88 | +} |
0 commit comments