-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogleOAuth.pm
102 lines (76 loc) · 2.2 KB
/
GoogleOAuth.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package GoogleOAuth;
#!/usr/bin/env perl
#
# François Serman ([email protected])
use warnings;
use strict;
BEGIN {
require Exporter;
our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT = qw(generate_OAuth2_token authorize_token refresh_token generate_OAuth2_string);
}
my $BASE_URL = 'https://accounts.google.com';
my $REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
sub encode_params
{
my $values = $_[0];
my @stack;
foreach (keys(%{$values}))
{
push(@stack, sprintf("%s=%s", $_, $values->{$_}));
}
join('&', @stack);
}
sub accounts_url
{
$BASE_URL . '/' . $_[0];
}
sub generate_OAuth2_token
{
my ($client_id) = $_[0];
my %params = ('client_id', $client_id,
'redirect_uri', $REDIRECT_URI,
'scope', 'https://mail.google.com/', # HARDCODED
'response_type', 'code');
accounts_url('o/oauth2/auth') . '?' . encode_params(\%params);
}
sub authorize_token
{
use LWP::Simple;
use JSON;
my ($client_id, $client_secret, $authorization_code) = @_;
my $url = accounts_url('o/oauth2/token');
my $browser = LWP::UserAgent->new;
my $response = $browser->post($url, [ 'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $authorization_code,
'redirect_uri' => $REDIRECT_URI,
'grant_type' => 'authorization_code' ]);
die "Can't post $url -- ", $response->status_line
unless $response->is_success;
decode_json ($response->content);
}
sub refresh_token
{
use LWP::Simple;
use JSON;
my ($client_id, $client_secret, $refresh_token) = @_;
my $url = accounts_url('o/oauth2/token');
my $browser = LWP::UserAgent->new;
my $response = $browser->post($url, [ 'client_id' => $client_id,
'client_secret' => $client_secret,
'refresh_token' => $refresh_token,
'grant_type' => 'refresh_token' ]);
die "Can't post $url -- ", $response->status_line
unless $response->is_success;
decode_json ($response->content);
}
sub generate_OAuth2_string
{
use MIME::Base64;
my ($username, $access_token) = @_;
encode_base64(sprintf("user=%s\1auth=Bearer %s\1\1", $username, $access_token), ''); # Otherwize a linefeed comes
}
1;
__END__