Skip to content

Commit 03f5259

Browse files
author
John
committed
added clear token option, updated scopes for m365
1 parent 9b53514 commit 03f5259

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/OAuth.pm

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ use Time::Piece;
3838
our @EXPORT = qw( get_oauth );
3939

4040
sub get_oauth {
41-
my ($oauthuri, $oauthclientid, $dbh, $db_tx_support) = (@_);
41+
my ($oauthuri, $oauthclientid, $dbh, $db_tx_support, $clear_token) = (@_);
42+
43+
# clear token if requested
44+
if ($clear_token) {
45+
my $sql = qq{DELETE FROM oauth};
46+
$dbh->do($sql, undef);
47+
if ($dbh->errstr) {
48+
warn "$scriptname: $org: $id: Cannot invalidate OAuth tokens.\n";
49+
exit;
50+
}
51+
else {
52+
if ($db_tx_support) {
53+
$dbh->commit;
54+
if ($dbh->errstr) {
55+
warn "$scriptname: $org: $id: Cannot commit transaction.\n";
56+
}
57+
}
58+
}
59+
}
4260

4361
# check if valid oauth token exists
4462
my $sth = $dbh->prepare(qq{SELECT access_token, refresh_token, UNIX_TIMESTAMP(expire) AS expire, valid FROM oauth WHERE valid=1});
@@ -136,7 +154,7 @@ sub get_oauth {
136154

137155
# if it's m365, we need to request offline_access too
138156
if ($oauthuri =~ m/microsoft/) {
139-
$scope .= "%20offline_access";
157+
$scope .= "%20offline_access%20https%3A%2F%2Foutlook.office.com%2F.default";
140158
}
141159

142160
# send the device authorization request

report-parser.pl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ sub show_usage {
131131
$imapauth, $oauthclientid, $oauthuri,
132132
$imapdmarcfolder, $imapdmarcproc, $imapdmarcerr,
133133
$imaptlsfolder, $imaptlsproc, $imaptlserr,
134-
$tlsverify, $processInfo);
134+
$tlsverify, $processInfo,
135+
$clear_token);
135136

136137
# defaults
137138
$maxsize_xml = 50000;
@@ -142,6 +143,7 @@ sub show_usage {
142143
$dmarc_only = 1;
143144
$reports_replace = 0;
144145
$imapauth = 'simple';
146+
$clear_token = 0;
145147

146148
# used in messages
147149
my $scriptname = 'Open Report Parser';
@@ -198,7 +200,7 @@ sub show_usage {
198200
TS_MBOX_FILE => 3,
199201
TS_ZIP_FILE => 4,
200202
TS_JSON_FILE => 5 };
201-
GetOptions( \%options, 'd', 'r', 'x', 'j', 'm', 'e', 'i', 'z', 'delete', 'info', 'c' => \$conf_file );
203+
GetOptions( \%options, 'd', 'r', 'x', 'j', 'm', 'e', 'i', 'z', 'delete', 'info', 'c' => \$conf_file, 'clear' );
202204

203205
# locate conf file or die
204206
if ( -e $conf_file ) {
@@ -299,6 +301,7 @@ sub show_usage {
299301
if (exists $options{delete}) {$delete_reports = 1;}
300302
if (exists $options{info}) {$processInfo = 1;}
301303
if (exists $options{tls}) {$dmarc_only = -1;}
304+
if (exists $options{clear}) {$clear_token = 1;}
302305

303306
# Cludgy, but it lets us preserve filename for dbx_postgres.pl
304307
my $dbitype = 'mysql';
@@ -415,7 +418,7 @@ sub show_usage {
415418
elsif ($imapauth eq 'oauth2') {
416419
printDebug("using oauth2");
417420
# get the bearer token
418-
my $oauth2token = OAuth::get_oauth($oauthuri, $oauthclientid, $dbh, $db_tx_support);
421+
my $oauth2token = OAuth::get_oauth($oauthuri, $oauthclientid, $dbh, $db_tx_support, $clear_token);
419422

420423
# authenticate
421424
my $oauth_b64 = encode_base64("user=".$imapuser."\x01auth=Bearer ".$oauth2token."\x01\x01",'');

0 commit comments

Comments
 (0)