View | Details | Raw Unified | Return to bug 38338
Collapse All | Expand All

(-)a/misc/maintenance/xoauth2_email_bootstrap.pl (-1 / +131 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
#
4
# This file is part of Koha
5
#
6
# Koha is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Koha is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Koha; if not, see <https://www.gnu.org/licenses>.
18
#
19
20
use Modern::Perl;
21
use LWP::UserAgent;
22
use URI;
23
use JSON qw(decode_json);
24
use Getopt::Long;
25
26
=head1 SYNOPSIS
27
28
perl xoauth2_email_bootstrap.pl --conf oauth2_config.json
29
30
oauth2_config.json must contain the following keys:
31
    client_id,
32
    client_secret,
33
    scope,
34
    redirect_uri,
35
    auth_endpoint,
36
    token_endpoint
37
38
=cut
39
40
my $client_id;
41
my $client_secret;
42
my $scope;
43
my $redirect_uri;
44
my $auth_endpoint;
45
my $token_endpoint;
46
47
my $config_filename = "";
48
GetOptions(
49
    "config=s" => \$config_filename,
50
) or die("Error in command line arguments\n");
51
52
if ($config_filename) {
53
    my $config = parse_config($config_filename);
54
    if ($config) {
55
        $client_id      = $config->{client_id};
56
        $client_secret  = $config->{client_secret};
57
        $scope          = $config->{scope};
58
        $redirect_uri   = $config->{redirect_uri};
59
        $auth_endpoint  = $config->{auth_endpoint};
60
        $token_endpoint = $config->{token_endpoint};
61
    }
62
}
63
64
die "No auth_endpoint"  unless $auth_endpoint;
65
die "No token_endpoint" unless $token_endpoint;
66
die "No client_id"      unless $client_id;
67
die "No client_secret"  unless $client_secret;
68
die "No scope"          unless $scope;
69
die "No redirect_uri"   unless $redirect_uri;
70
71
# Do authorizaton code request
72
my $auth_uri = URI->new($auth_endpoint);
73
$auth_uri->query_form(
74
    response_type => 'code',
75
    client_id     => $client_id,
76
    redirect_uri  => $redirect_uri,
77
    scope         => $scope,
78
);
79
80
print "\nGo to this URL in your browser:\n\n";
81
print "$auth_uri\n\n";
82
83
print "After following the workflow in your browser, ";
84
print "paste the authorization code from the 'code' parameter here:\n> ";
85
chomp( my $code = <STDIN> );
86
87
die "No code provided\n" unless $code;
88
89
# Exchange code for tokens
90
my $ua = LWP::UserAgent->new(
91
    timeout => 20,
92
);
93
94
my $res = $ua->post(
95
    $token_endpoint,
96
    {
97
        grant_type    => 'authorization_code',
98
        code          => $code,
99
        client_id     => $client_id,
100
        client_secret => $client_secret,
101
        redirect_uri  => $redirect_uri,
102
    }
103
);
104
105
die "Token request failed: " . $res->status_line . "\n" . $res->decoded_content
106
    unless $res->is_success;
107
108
my $token = decode_json( $res->decoded_content );
109
110
print "\n=== TOKENS ===\n";
111
print "Access token:\n$token->{access_token}\n\n";
112
print "Refresh token:\n$token->{refresh_token}\n\n";
113
print "Expires in:\n$token->{expires_in} seconds\n\n";
114
115
sub parse_config {
116
    my ($filename) = @_;
117
    if ( $filename && -f $filename ) {
118
        my $opened = open( my $fh, '<', $filename );
119
        if ($opened) {
120
            local $/;
121
            my $config_json = <$fh>;
122
            if ($config_json) {
123
                my $parsed_config = decode_json($config_json);
124
                if ($parsed_config) {
125
                    return $parsed_config;
126
                }
127
            }
128
        }
129
    }
130
    return;
131
}

Return to bug 38338